pax_global_header00006660000000000000000000000064122210016030014475gustar00rootroot0000000000000052 comment=b16bdac170303283aabe2eb6cc2086c2dcfdb860 cajun-jsonapi-cajun-2.0.3/000077500000000000000000000000001222100160300153365ustar00rootroot00000000000000cajun-jsonapi-cajun-2.0.3/Makefile000066400000000000000000000001331222100160300167730ustar00rootroot00000000000000EXE_NAME = ./test.out $(EXE_NAME): test.cpp g++ -o $@ $^ clean: -rm $(EXE_NAME) cajun-jsonapi-cajun-2.0.3/Readme.txt000066400000000000000000000036741222100160300173060ustar00rootroot00000000000000CAJUN* is a C++ API for the JSON object interchange format. JSON is like XML, except it doesn't suck**. It is specifically designed for representing (in plain text format) structures familiar to software engineers: booleans, numerics, strings, arrays, and objects (i.e. name/value pairs, associative array, etc.); it humbly leaves text markup to XML. It is ideal for storing persistent application data, such as configuration or user data files. Too many JSON parsers I've seen suffer from overly complex designs and confusing interfaces, so in true software engineer form, I thought I could do better. The goal of JSON was to create an simple, "minimalist" interface while sacrificing absolutely no power or flexibility. The STL containers, while not without their violations of that spirit, served as an inspiration. The end result is (IMHO) an interface that should be immediately intuitive to anyone familiar with C++ and the Standard Library containers. It can best be described as working with an "element", where an element may consist of: * String (mimics std::string) * Numeric (double) * Boolean (bool) * Array (std::vector) * Object (unsorted std::map) * UnknownElement - like boost::any, but restricted to types below. Used to aggregate elements within Objects & Arrays, and for reading documents of unknown structure As with any design, sacrifices were made with CAJUN. Most situations I've encountered where JSON is well-suited (reading & writing application configuration and data files) are not typically performance bottlenecks, so simplicity, safety & flexibility were favored over raw speed. The end result is a library with simple, typesafe classes, no memory-management burden on the user, and exception-based error reporting. * C++ API for JSON. A pint on me for who ever comes up with a good meaning for "UN". ** To be fair, XML doesn't suck intentionally, it is just often used inappropriately.cajun-jsonapi-cajun-2.0.3/ReleaseNotes.txt000066400000000000000000000053371222100160300205000ustar00rootroot000000000000002.0.3 (2013-09-26) * fix for UTF-8 support on Linux builds * add spec file for RPM packaging 2.0.2 (12/04/2011) * Fixed exception text construction bug (pointer + offset, instead of std::string + std::string) * Fixed crash in UnknownElement::operator= * Fixed bug where object member names couldn't contain special characters * Fixed crash when parsing an incomplete document * Cosmetic changes to parser * Added proper BSD license text 2.0.1 (11/17/2009) * A couple of Reader functions not inlined, sometimes resulting in linker duplicate symbols. Oops. 2.0.0 (11/14/2009) * Redesign/simplicification of the element class relationships: * Element/Element_T base class or *Imp classes eliminated. Originally necessary for aggregation by Object/Array, but now unnecessary with UnknownElement type * json_cast<> functions eliminated. Static type safety relaxed, allowing more concise document data extraction code (dynamic type safety still completely maintained). * Quick-Interpreter/-Builder classes eliminated. Equivalent functionality now in "UnknownElement", but now more accessible In summary, simpler design, less code in library, less code necessary to utilize library. See test app for many more new examples. * Entire library is now inlined. Bound to be controversial, but... * Modern compilers should eliminate redundant object code * Fixes problems associated with different runtime libraries, library debug information, security & debug iterator compile-time options under MSVC++, among other things. * Simply include the appropriate file & go - no linker settings to mess with. * Added 64-bit build targets for MSVC 8/9 test app, just because. * Scan/Parse exceptions moved into Reader class scope, and Parse exceptions fixed to always include bad token string * A few more random bug fixes * Tested under: * MSVC++ 2005 * MSVC++ 2008 * GCC 4.4.0 1.1.0 (08/30/2009) * Implemented operator == for all element types * Added makefile for building with g++ (thanks George Morgan). * Fixed a few compiler errors on non-Visual Studio compilers (my C++ wasn't as ANSI as I thought...) * Support for (non-standard) comments REMOVED * Support for Visual Studio 7.1 (2003) REMOVED * Fixed the "Unexpected token..." exception string (was gibberish) * Improvements to the QuickInterpreter & QuickBuilder interfaces * Elements now sanity-check themselves during operations and throw an exception accordingly, for example if an Object gets tricked into thinking it's an Array (reinterpret_cast, reading a document with an Array root element into an Object, etc) * Other random minor bug fixes & general cleanup 1.0.0 (01/31/2009) * Initial release! Remaining work: better documentation, better test/sample app, yada yada cajun-jsonapi-cajun-2.0.3/cajun-jsonapi.spec000066400000000000000000000026701222100160300207600ustar00rootroot00000000000000# cajun only ships headers, so no debuginfo package is needed %define debug_package %{nil} Summary: A cross-platform C++ header library for JSON Name: cajun-jsonapi Version: 2.0.3 Release: 1%{?dist} URL: https://github.com/cajun-jsonapi/cajun-jsonapi Source0: https://github.com/cajun-jsonapi/cajun-jsonapi/archive/cajun-jsonapi-%{version}.tar.gz License: BSD Group: System Environment/Libraries %description CAJUN is a C++ API for the JSON data interchange format with an emphasis on an intuitive, concise interface. The library provides JSON types and operations that mimic standard C++ as closely as possible in concept and design. %package devel Group: Development/Libraries Summary: Header files for cajun %description devel Header files you can use to develop applications with cajun. CAJUN is a C++ API for the JSON data interchange format with an emphasis on an intuitive, concise interface. The library provides JSON types and operations that mimic standard C++ as closely as possible in concept and design. %prep %setup -q %build %install install -d -m755 $RPM_BUILD_ROOT/%{_includedir}/cajun/json install -p -m644 json/* $RPM_BUILD_ROOT/%{_includedir}/cajun/json %check make %{?_smp_mflags} %files devel %defattr(-,root,root,-) %doc Readme.txt ReleaseNotes.txt %dir %{_includedir}/cajun %dir %{_includedir}/cajun/json %{_includedir}/cajun/json/* %changelog * Thu Sep 26 2013 Daniel Pocock - 2.0.3-1 - Initial spec file cajun-jsonapi-cajun-2.0.3/json/000077500000000000000000000000001222100160300163075ustar00rootroot00000000000000cajun-jsonapi-cajun-2.0.3/json/elements.h000066400000000000000000000214061222100160300202770ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #pragma once #include #include #include #include /* TODO: * better documentation (doxygen?) * Unicode support * parent element accessors */ namespace json { namespace Version { enum { MAJOR = 2 }; enum { MINOR = 0 }; enum {ENGINEERING = 2 }; } ///////////////////////////////////////////////// // forward declarations (more info further below) class Visitor; class ConstVisitor; template class TrivialType_T; typedef TrivialType_T Number; typedef TrivialType_T Boolean; typedef TrivialType_T String; class Object; class Array; class Null; ///////////////////////////////////////////////////////////////////////// // Exception - base class for all JSON-related runtime errors class Exception : public std::runtime_error { public: Exception(const std::string& sMessage); }; ///////////////////////////////////////////////////////////////////////// // UnknownElement - provides a typesafe surrogate for any of the JSON- // sanctioned element types. This class allows the Array and Object // class to effectively contain a heterogeneous set of child elements. // The cast operators provide convenient implicit downcasting, while // preserving dynamic type safety by throwing an exception during a // a bad cast. // The object & array element index operators (operators [std::string] // and [size_t]) provide convenient, quick access to child elements. // They are a logical extension of the cast operators. These child // element accesses can be chained together, allowing the following // (when document structure is well-known): // String str = objInvoices[1]["Customer"]["Company"]; class UnknownElement { public: UnknownElement(); UnknownElement(const UnknownElement& unknown); UnknownElement(const Object& object); UnknownElement(const Array& array); UnknownElement(const Number& number); UnknownElement(const Boolean& boolean); UnknownElement(const String& string); UnknownElement(const Null& null); ~UnknownElement(); UnknownElement& operator = (const UnknownElement& unknown); // implicit cast to actual element type. throws on failure operator const Object& () const; operator const Array& () const; operator const Number& () const; operator const Boolean& () const; operator const String& () const; operator const Null& () const; // implicit cast to actual element type. *converts* on failure, and always returns success operator Object& (); operator Array& (); operator Number& (); operator Boolean& (); operator String& (); operator Null& (); // provides quick access to children when real element type is object UnknownElement& operator[] (const std::string& key); const UnknownElement& operator[] (const std::string& key) const; // provides quick access to children when real element type is array UnknownElement& operator[] (size_t index); const UnknownElement& operator[] (size_t index) const; // implements visitor pattern void Accept(ConstVisitor& visitor) const; void Accept(Visitor& visitor); // tests equality. first checks type, then value if possible bool operator == (const UnknownElement& element) const; private: class Imp; template class Imp_T; class CastVisitor; class ConstCastVisitor; template class CastVisitor_T; template class ConstCastVisitor_T; template const ElementTypeT& CastTo() const; template ElementTypeT& ConvertTo(); Imp* m_pImp; }; ///////////////////////////////////////////////////////////////////////////////// // Array - mimics std::deque. The array contents are effectively // heterogeneous thanks to the ElementUnknown class. push_back has been replaced // by more generic insert functions. class Array { public: typedef std::deque Elements; typedef Elements::iterator iterator; typedef Elements::const_iterator const_iterator; iterator Begin(); iterator End(); const_iterator Begin() const; const_iterator End() const; iterator Insert(const UnknownElement& element, iterator itWhere); iterator Insert(const UnknownElement& element); iterator Erase(iterator itWhere); void Resize(size_t newSize); void Clear(); size_t Size() const; bool Empty() const; UnknownElement& operator[] (size_t index); const UnknownElement& operator[] (size_t index) const; bool operator == (const Array& array) const; private: Elements m_Elements; }; ///////////////////////////////////////////////////////////////////////////////// // Object - mimics std::map. The member value // contents are effectively heterogeneous thanks to the UnknownElement class class Object { public: struct Member { Member(const std::string& nameIn = std::string(), const UnknownElement& elementIn = UnknownElement()); bool operator == (const Member& member) const; std::string name; UnknownElement element; }; typedef std::list Members; // map faster, but does not preserve order typedef Members::iterator iterator; typedef Members::const_iterator const_iterator; bool operator == (const Object& object) const; iterator Begin(); iterator End(); const_iterator Begin() const; const_iterator End() const; size_t Size() const; bool Empty() const; iterator Find(const std::string& name); const_iterator Find(const std::string& name) const; iterator Insert(const Member& member); iterator Insert(const Member& member, iterator itWhere); iterator Erase(iterator itWhere); void Clear(); UnknownElement& operator [](const std::string& name); const UnknownElement& operator [](const std::string& name) const; private: class Finder; Members m_Members; }; ///////////////////////////////////////////////////////////////////////////////// // TrivialType_T - class template for encapsulates a simple data type, such as // a string, number, or boolean. Provides implicit const & noncost cast operators // for that type, allowing "DataTypeT type = trivialType;" template class TrivialType_T { public: TrivialType_T(const DataTypeT& t = DataTypeT()); operator DataTypeT&(); operator const DataTypeT&() const; DataTypeT& Value(); const DataTypeT& Value() const; bool operator == (const TrivialType_T& trivial) const; private: DataTypeT m_tValue; }; ///////////////////////////////////////////////////////////////////////////////// // Null - doesn't do much of anything but satisfy the JSON spec. It is the default // element type of UnknownElement class Null { public: bool operator == (const Null& trivial) const; }; } // End namespace #include "elements.inl" cajun-jsonapi-cajun-2.0.3/json/elements.inl000066400000000000000000000324241222100160300206340ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #include "visitor.h" #include "reader.h" #include #include #include /* TODO: * better documentation */ namespace json { inline Exception::Exception(const std::string& sMessage) : std::runtime_error(sMessage) {} ///////////////////////// // UnknownElement members class UnknownElement::Imp { public: virtual ~Imp() {} virtual Imp* Clone() const = 0; virtual bool Compare(const Imp& imp) const = 0; virtual void Accept(ConstVisitor& visitor) const = 0; virtual void Accept(Visitor& visitor) = 0; }; template class UnknownElement::Imp_T : public UnknownElement::Imp { public: Imp_T(const ElementTypeT& element) : m_Element(element) {} virtual Imp* Clone() const { return new Imp_T(*this); } virtual void Accept(ConstVisitor& visitor) const { visitor.Visit(m_Element); } virtual void Accept(Visitor& visitor) { visitor.Visit(m_Element); } virtual bool Compare(const Imp& imp) const { ConstCastVisitor_T castVisitor; imp.Accept(castVisitor); return castVisitor.m_pElement && m_Element == *castVisitor.m_pElement; } private: ElementTypeT m_Element; }; class UnknownElement::ConstCastVisitor : public ConstVisitor { virtual void Visit(const Array& array) {} virtual void Visit(const Object& object) {} virtual void Visit(const Number& number) {} virtual void Visit(const String& string) {} virtual void Visit(const Boolean& boolean) {} virtual void Visit(const Null& null) {} }; template class UnknownElement::ConstCastVisitor_T : public ConstCastVisitor { public: ConstCastVisitor_T() : m_pElement(0) {} virtual void Visit(const ElementTypeT& element) { m_pElement = &element; } // we don't know what this is, but it overrides one of the base's no-op functions const ElementTypeT* m_pElement; }; class UnknownElement::CastVisitor : public Visitor { virtual void Visit(Array& array) {} virtual void Visit(Object& object) {} virtual void Visit(Number& number) {} virtual void Visit(String& string) {} virtual void Visit(Boolean& boolean) {} virtual void Visit(Null& null) {} }; template class UnknownElement::CastVisitor_T : public CastVisitor { public: CastVisitor_T() : m_pElement(0) {} virtual void Visit(ElementTypeT& element) { m_pElement = &element; } // we don't know what this is, but it overrides one of the base's no-op functions ElementTypeT* m_pElement; }; inline UnknownElement::UnknownElement() : m_pImp( new Imp_T( Null() ) ) {} inline UnknownElement::UnknownElement(const UnknownElement& unknown) : m_pImp( unknown.m_pImp->Clone()) {} inline UnknownElement::UnknownElement(const Object& object) : m_pImp( new Imp_T(object) ) {} inline UnknownElement::UnknownElement(const Array& array) : m_pImp( new Imp_T(array) ) {} inline UnknownElement::UnknownElement(const Number& number) : m_pImp( new Imp_T(number) ) {} inline UnknownElement::UnknownElement(const Boolean& boolean) : m_pImp( new Imp_T(boolean) ) {} inline UnknownElement::UnknownElement(const String& string) : m_pImp( new Imp_T(string) ) {} inline UnknownElement::UnknownElement(const Null& null) : m_pImp( new Imp_T(null) ) {} inline UnknownElement::~UnknownElement() { delete m_pImp; } inline UnknownElement::operator const Object& () const { return CastTo(); } inline UnknownElement::operator const Array& () const { return CastTo(); } inline UnknownElement::operator const Number& () const { return CastTo(); } inline UnknownElement::operator const Boolean& () const { return CastTo(); } inline UnknownElement::operator const String& () const { return CastTo(); } inline UnknownElement::operator const Null& () const { return CastTo(); } inline UnknownElement::operator Object& () { return ConvertTo(); } inline UnknownElement::operator Array& () { return ConvertTo(); } inline UnknownElement::operator Number& () { return ConvertTo(); } inline UnknownElement::operator Boolean& () { return ConvertTo(); } inline UnknownElement::operator String& () { return ConvertTo(); } inline UnknownElement::operator Null& () { return ConvertTo(); } inline UnknownElement& UnknownElement::operator = (const UnknownElement& unknown) { // always check for this if (&unknown != this) { // we might be copying from a subtree of ourselves. delete the old imp // only after the clone operation is complete. yes, this could be made // more efficient, but isn't worth the complexity Imp* pOldImp = m_pImp; m_pImp = unknown.m_pImp->Clone(); delete pOldImp; } return *this; } inline UnknownElement& UnknownElement::operator[] (const std::string& key) { // the people want an object. make us one if we aren't already Object& object = ConvertTo(); return object[key]; } inline const UnknownElement& UnknownElement::operator[] (const std::string& key) const { // throws if we aren't an object const Object& object = CastTo(); return object[key]; } inline UnknownElement& UnknownElement::operator[] (size_t index) { // the people want an array. make us one if we aren't already Array& array = ConvertTo(); return array[index]; } inline const UnknownElement& UnknownElement::operator[] (size_t index) const { // throws if we aren't an array const Array& array = CastTo(); return array[index]; } template const ElementTypeT& UnknownElement::CastTo() const { ConstCastVisitor_T castVisitor; m_pImp->Accept(castVisitor); if (castVisitor.m_pElement == 0) throw Exception("Bad cast"); return *castVisitor.m_pElement; } template ElementTypeT& UnknownElement::ConvertTo() { CastVisitor_T castVisitor; m_pImp->Accept(castVisitor); if (castVisitor.m_pElement == 0) { // we're not the right type. fix it & try again *this = ElementTypeT(); m_pImp->Accept(castVisitor); } return *castVisitor.m_pElement; } inline void UnknownElement::Accept(ConstVisitor& visitor) const { m_pImp->Accept(visitor); } inline void UnknownElement::Accept(Visitor& visitor) { m_pImp->Accept(visitor); } inline bool UnknownElement::operator == (const UnknownElement& element) const { return m_pImp->Compare(*element.m_pImp); } ////////////////// // Object members inline Object::Member::Member(const std::string& nameIn, const UnknownElement& elementIn) : name(nameIn), element(elementIn) {} inline bool Object::Member::operator == (const Member& member) const { return name == member.name && element == member.element; } class Object::Finder : public std::unary_function { public: Finder(const std::string& name) : m_name(name) {} bool operator () (const Object::Member& member) { return member.name == m_name; } private: std::string m_name; }; inline Object::iterator Object::Begin() { return m_Members.begin(); } inline Object::iterator Object::End() { return m_Members.end(); } inline Object::const_iterator Object::Begin() const { return m_Members.begin(); } inline Object::const_iterator Object::End() const { return m_Members.end(); } inline size_t Object::Size() const { return m_Members.size(); } inline bool Object::Empty() const { return m_Members.empty(); } inline Object::iterator Object::Find(const std::string& name) { return std::find_if(m_Members.begin(), m_Members.end(), Finder(name)); } inline Object::const_iterator Object::Find(const std::string& name) const { return std::find_if(m_Members.begin(), m_Members.end(), Finder(name)); } inline Object::iterator Object::Insert(const Member& member) { return Insert(member, End()); } inline Object::iterator Object::Insert(const Member& member, iterator itWhere) { iterator it = Find(member.name); if (it != m_Members.end()) throw Exception(std::string("Object member already exists: ") + member.name); it = m_Members.insert(itWhere, member); return it; } inline Object::iterator Object::Erase(iterator itWhere) { return m_Members.erase(itWhere); } inline UnknownElement& Object::operator [](const std::string& name) { iterator it = Find(name); if (it == m_Members.end()) { Member member(name); it = Insert(member, End()); } return it->element; } inline const UnknownElement& Object::operator [](const std::string& name) const { const_iterator it = Find(name); if (it == End()) throw Exception(std::string("Object member not found: ") + name); return it->element; } inline void Object::Clear() { m_Members.clear(); } inline bool Object::operator == (const Object& object) const { return m_Members == object.m_Members; } ///////////////// // Array members inline Array::iterator Array::Begin() { return m_Elements.begin(); } inline Array::iterator Array::End() { return m_Elements.end(); } inline Array::const_iterator Array::Begin() const { return m_Elements.begin(); } inline Array::const_iterator Array::End() const { return m_Elements.end(); } inline Array::iterator Array::Insert(const UnknownElement& element, iterator itWhere) { return m_Elements.insert(itWhere, element); } inline Array::iterator Array::Insert(const UnknownElement& element) { return Insert(element, End()); } inline Array::iterator Array::Erase(iterator itWhere) { return m_Elements.erase(itWhere); } inline void Array::Resize(size_t newSize) { m_Elements.resize(newSize); } inline size_t Array::Size() const { return m_Elements.size(); } inline bool Array::Empty() const { return m_Elements.empty(); } inline UnknownElement& Array::operator[] (size_t index) { size_t nMinSize = index + 1; // zero indexed if (m_Elements.size() < nMinSize) m_Elements.resize(nMinSize); return m_Elements[index]; } inline const UnknownElement& Array::operator[] (size_t index) const { if (index >= m_Elements.size()) throw Exception("Array out of bounds"); return m_Elements[index]; } inline void Array::Clear() { m_Elements.clear(); } inline bool Array::operator == (const Array& array) const { return m_Elements == array.m_Elements; } //////////////////////// // TrivialType_T members template TrivialType_T::TrivialType_T(const DataTypeT& t) : m_tValue(t) {} template TrivialType_T::operator DataTypeT&() { return Value(); } template TrivialType_T::operator const DataTypeT&() const { return Value(); } template DataTypeT& TrivialType_T::Value() { return m_tValue; } template const DataTypeT& TrivialType_T::Value() const { return m_tValue; } template bool TrivialType_T::operator == (const TrivialType_T& trivial) const { return m_tValue == trivial.m_tValue; } ////////////////// // Null members inline bool Null::operator == (const Null& trivial) const { return true; } } // End namespace cajun-jsonapi-cajun-2.0.3/json/reader.h000066400000000000000000000126531222100160300177310ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #pragma once #include "elements.h" #include #include namespace json { class Reader { public: // this structure will be reported in one of the exceptions defined below struct Location { Location(); unsigned int m_nLine; // document line, zero-indexed unsigned int m_nLineOffset; // character offset from beginning of line, zero indexed unsigned int m_nDocOffset; // character offset from entire document, zero indexed }; // thrown during the first phase of reading. generally catches low-level problems such // as errant characters or corrupt/incomplete documents class ScanException : public Exception { public: ScanException(const std::string& sMessage, const Reader::Location& locError) : Exception(sMessage), m_locError(locError) {} Reader::Location m_locError; }; // thrown during the second phase of reading. generally catches higher-level problems such // as missing commas or brackets class ParseException : public Exception { public: ParseException(const std::string& sMessage, const Reader::Location& locTokenBegin, const Reader::Location& locTokenEnd) : Exception(sMessage), m_locTokenBegin(locTokenBegin), m_locTokenEnd(locTokenEnd) {} Reader::Location m_locTokenBegin; Reader::Location m_locTokenEnd; }; // if you know what the document looks like, call one of these... static void Read(Object& object, std::istream& istr); static void Read(Array& array, std::istream& istr); static void Read(String& string, std::istream& istr); static void Read(Number& number, std::istream& istr); static void Read(Boolean& boolean, std::istream& istr); static void Read(Null& null, std::istream& istr); // ...otherwise, if you don't know, call this & visit it static void Read(UnknownElement& elementRoot, std::istream& istr); private: struct Token { enum Type { TOKEN_OBJECT_BEGIN, // { TOKEN_OBJECT_END, // } TOKEN_ARRAY_BEGIN, // [ TOKEN_ARRAY_END, // ] TOKEN_NEXT_ELEMENT, // , TOKEN_MEMBER_ASSIGN, // : TOKEN_STRING, // "xxx" TOKEN_NUMBER, // [+/-]000.000[e[+/-]000] TOKEN_BOOLEAN, // true -or- false TOKEN_NULL, // null }; Type nType; std::string sValue; // for malformed file debugging Reader::Location locBegin; Reader::Location locEnd; }; class InputStream; class TokenStream; typedef std::vector Tokens; template static void Read_i(ElementTypeT& element, std::istream& istr); // scanning istream into token sequence void Scan(Tokens& tokens, InputStream& inputStream); void EatWhiteSpace(InputStream& inputStream); std::string MatchString(InputStream& inputStream); std::string MatchNumber(InputStream& inputStream); std::string MatchExpectedString(InputStream& inputStream, const std::string& sExpected); // parsing token sequence into element structure void Parse(UnknownElement& element, TokenStream& tokenStream); void Parse(Object& object, TokenStream& tokenStream); void Parse(Array& array, TokenStream& tokenStream); void Parse(String& string, TokenStream& tokenStream); void Parse(Number& number, TokenStream& tokenStream); void Parse(Boolean& boolean, TokenStream& tokenStream); void Parse(Null& null, TokenStream& tokenStream); const std::string& MatchExpectedToken(Token::Type nExpected, TokenStream& tokenStream); }; } // End namespace #include "reader.inl"cajun-jsonapi-cajun-2.0.3/json/reader.inl000066400000000000000000000420411222100160300202560ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #include #include #include /* TODO: * better documentation */ namespace json { inline std::istream& operator >> (std::istream& istr, UnknownElement& elementRoot) { Reader::Read(elementRoot, istr); return istr; } inline Reader::Location::Location() : m_nLine(0), m_nLineOffset(0), m_nDocOffset(0) {} ////////////////////// // Reader::InputStream class Reader::InputStream // would be cool if we could inherit from std::istream & override "get" { public: InputStream(std::istream& iStr) : m_iStr(iStr) {} // protect access to the input stream, so we can keeep track of document/line offsets char Get(); // big, define outside char Peek() { assert(m_iStr.eof() == false); // enforce reading of only valid stream data return m_iStr.peek(); } bool EOS() { m_iStr.peek(); // apparently eof flag isn't set until a character read is attempted. whatever. return m_iStr.eof(); } const Location& GetLocation() const { return m_Location; } private: std::istream& m_iStr; Location m_Location; }; inline char Reader::InputStream::Get() { assert(m_iStr.eof() == false); // enforce reading of only valid stream data char c = m_iStr.get(); ++m_Location.m_nDocOffset; if (c == '\n') { ++m_Location.m_nLine; m_Location.m_nLineOffset = 0; } else { ++m_Location.m_nLineOffset; } return c; } ////////////////////// // Reader::TokenStream class Reader::TokenStream { public: TokenStream(const Tokens& tokens); const Token& Peek(); const Token& Get(); bool EOS() const; private: const Tokens& m_Tokens; Tokens::const_iterator m_itCurrent; }; inline Reader::TokenStream::TokenStream(const Tokens& tokens) : m_Tokens(tokens), m_itCurrent(tokens.begin()) {} inline const Reader::Token& Reader::TokenStream::Peek() { if (EOS()) { const Token& lastToken = *m_Tokens.rbegin(); std::string sMessage = "Unexpected end of token stream"; throw ParseException(sMessage, lastToken.locBegin, lastToken.locEnd); // nowhere to point to } return *(m_itCurrent); } inline const Reader::Token& Reader::TokenStream::Get() { const Token& token = Peek(); ++m_itCurrent; return token; } inline bool Reader::TokenStream::EOS() const { return m_itCurrent == m_Tokens.end(); } /////////////////// // Reader (finally) inline void Reader::Read(Object& object, std::istream& istr) { Read_i(object, istr); } inline void Reader::Read(Array& array, std::istream& istr) { Read_i(array, istr); } inline void Reader::Read(String& string, std::istream& istr) { Read_i(string, istr); } inline void Reader::Read(Number& number, std::istream& istr) { Read_i(number, istr); } inline void Reader::Read(Boolean& boolean, std::istream& istr) { Read_i(boolean, istr); } inline void Reader::Read(Null& null, std::istream& istr) { Read_i(null, istr); } inline void Reader::Read(UnknownElement& unknown, std::istream& istr) { Read_i(unknown, istr); } template void Reader::Read_i(ElementTypeT& element, std::istream& istr) { Reader reader; Tokens tokens; InputStream inputStream(istr); reader.Scan(tokens, inputStream); TokenStream tokenStream(tokens); reader.Parse(element, tokenStream); if (tokenStream.EOS() == false) { const Token& token = tokenStream.Peek(); std::string sMessage = std::string("Expected End of token stream; found ") + token.sValue; throw ParseException(sMessage, token.locBegin, token.locEnd); } } inline void Reader::Scan(Tokens& tokens, InputStream& inputStream) { while (EatWhiteSpace(inputStream), // ignore any leading white space... inputStream.EOS() == false) // ...before checking for EOS { // if all goes well, we'll create a token each pass Token token; token.locBegin = inputStream.GetLocation(); // gives us null-terminated string char sChar = inputStream.Peek(); switch (sChar) { case '{': token.sValue = MatchExpectedString(inputStream, "{"); token.nType = Token::TOKEN_OBJECT_BEGIN; break; case '}': token.sValue = MatchExpectedString(inputStream, "}"); token.nType = Token::TOKEN_OBJECT_END; break; case '[': token.sValue = MatchExpectedString(inputStream, "["); token.nType = Token::TOKEN_ARRAY_BEGIN; break; case ']': token.sValue = MatchExpectedString(inputStream, "]"); token.nType = Token::TOKEN_ARRAY_END; break; case ',': token.sValue = MatchExpectedString(inputStream, ","); token.nType = Token::TOKEN_NEXT_ELEMENT; break; case ':': token.sValue = MatchExpectedString(inputStream, ":"); token.nType = Token::TOKEN_MEMBER_ASSIGN; break; case '"': token.sValue = MatchString(inputStream); token.nType = Token::TOKEN_STRING; break; case '-': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': token.sValue = MatchNumber(inputStream); token.nType = Token::TOKEN_NUMBER; break; case 't': token.sValue = MatchExpectedString(inputStream, "true"); token.nType = Token::TOKEN_BOOLEAN; break; case 'f': token.sValue = MatchExpectedString(inputStream, "false"); token.nType = Token::TOKEN_BOOLEAN; break; case 'n': token.sValue = MatchExpectedString(inputStream, "null"); token.nType = Token::TOKEN_NULL; break; default: { std::string sErrorMessage = std::string("Unexpected character in stream: ") + sChar; throw ScanException(sErrorMessage, inputStream.GetLocation()); } } token.locEnd = inputStream.GetLocation(); tokens.push_back(token); } } inline void Reader::EatWhiteSpace(InputStream& inputStream) { while (inputStream.EOS() == false && ::isspace(inputStream.Peek())) inputStream.Get(); } inline std::string Reader::MatchExpectedString(InputStream& inputStream, const std::string& sExpected) { std::string::const_iterator it(sExpected.begin()), itEnd(sExpected.end()); for ( ; it != itEnd; ++it) { if (inputStream.EOS() || // did we reach the end before finding what we're looking for... inputStream.Get() != *it) // ...or did we find something different? { std::string sMessage = std::string("Expected string: ") + sExpected; throw ScanException(sMessage, inputStream.GetLocation()); } } // all's well if we made it here return sExpected; } inline std::string Reader::MatchString(InputStream& inputStream) { MatchExpectedString(inputStream, "\""); std::string string; while (inputStream.EOS() == false && inputStream.Peek() != '"') { char c = inputStream.Get(); // escape? if (c == '\\' && inputStream.EOS() == false) // shouldn't have reached the end yet { c = inputStream.Get(); switch (c) { case '/': string.push_back('/'); break; case '"': string.push_back('"'); break; case '\\': string.push_back('\\'); break; case 'b': string.push_back('\b'); break; case 'f': string.push_back('\f'); break; case 'n': string.push_back('\n'); break; case 'r': string.push_back('\r'); break; case 't': string.push_back('\t'); break; case 'u': { // convert unicode to UTF-8 int x = 0, i; // next four characters should be hex for (i = 0; i < 4; ++i) { c = inputStream.Get(); if (c >= '0' && c <= '9') { x = (x << 4) | (c - '0'); } else if (c >= 'a' && c <= 'f') { x = (x << 4) | (c - 'a' + 10); } else if (c >= 'A' && c <= 'F') { x = (x << 4) | (c - 'A' + 10); } else { std::string sMessage = std::string("Unrecognized hexadecimal character found in string: ") + c; throw ScanException(sMessage, inputStream.GetLocation()); } } // encode as UTF-8 if (x < 0x80) { string.push_back(x); } else if (x < 0x800) { string.push_back(0xc0 | (x >> 6)); string.push_back(0x80 | (x & 0x3f)); } else { string.push_back(0xe0 | (x >> 12)); string.push_back(0x80 | ((x >> 6) & 0x3f)); string.push_back(0x80 | (x & 0x3f)); } break; } default: { std::string sMessage = std::string("Unrecognized escape sequence found in string: \\") + c; throw ScanException(sMessage, inputStream.GetLocation()); } } } else { string.push_back(c); } } // eat the last '"' that we just peeked MatchExpectedString(inputStream, "\""); // all's well if we made it here return string; } inline std::string Reader::MatchNumber(InputStream& inputStream) { const char sNumericChars[] = "0123456789.eE-+"; std::set numericChars; numericChars.insert(sNumericChars, sNumericChars + sizeof(sNumericChars)); std::string sNumber; while (inputStream.EOS() == false && numericChars.find(inputStream.Peek()) != numericChars.end()) { sNumber.push_back(inputStream.Get()); } return sNumber; } inline void Reader::Parse(UnknownElement& element, Reader::TokenStream& tokenStream) { const Token& token = tokenStream.Peek(); switch (token.nType) { case Token::TOKEN_OBJECT_BEGIN: { // implicit non-const cast will perform conversion for us (if necessary) Object& object = element; Parse(object, tokenStream); break; } case Token::TOKEN_ARRAY_BEGIN: { Array& array = element; Parse(array, tokenStream); break; } case Token::TOKEN_STRING: { String& string = element; Parse(string, tokenStream); break; } case Token::TOKEN_NUMBER: { Number& number = element; Parse(number, tokenStream); break; } case Token::TOKEN_BOOLEAN: { Boolean& boolean = element; Parse(boolean, tokenStream); break; } case Token::TOKEN_NULL: { Null& null = element; Parse(null, tokenStream); break; } default: { std::string sMessage = std::string("Unexpected token: ") + token.sValue; throw ParseException(sMessage, token.locBegin, token.locEnd); } } } inline void Reader::Parse(Object& object, Reader::TokenStream& tokenStream) { MatchExpectedToken(Token::TOKEN_OBJECT_BEGIN, tokenStream); bool bContinue = (tokenStream.EOS() == false && tokenStream.Peek().nType != Token::TOKEN_OBJECT_END); while (bContinue) { Object::Member member; // first the member name. save the token in case we have to throw an exception const Token& tokenName = tokenStream.Peek(); member.name = MatchExpectedToken(Token::TOKEN_STRING, tokenStream); // ...then the key/value separator... MatchExpectedToken(Token::TOKEN_MEMBER_ASSIGN, tokenStream); // ...then the value itself (can be anything). Parse(member.element, tokenStream); // try adding it to the object (this could throw) try { object.Insert(member); } catch (Exception&) { // must be a duplicate name std::string sMessage = std::string("Duplicate object member token: ") + member.name; throw ParseException(sMessage, tokenName.locBegin, tokenName.locEnd); } bContinue = (tokenStream.EOS() == false && tokenStream.Peek().nType == Token::TOKEN_NEXT_ELEMENT); if (bContinue) MatchExpectedToken(Token::TOKEN_NEXT_ELEMENT, tokenStream); } MatchExpectedToken(Token::TOKEN_OBJECT_END, tokenStream); } inline void Reader::Parse(Array& array, Reader::TokenStream& tokenStream) { MatchExpectedToken(Token::TOKEN_ARRAY_BEGIN, tokenStream); bool bContinue = (tokenStream.EOS() == false && tokenStream.Peek().nType != Token::TOKEN_ARRAY_END); while (bContinue) { // ...what's next? could be anything Array::iterator itElement = array.Insert(UnknownElement()); UnknownElement& element = *itElement; Parse(element, tokenStream); bContinue = (tokenStream.EOS() == false && tokenStream.Peek().nType == Token::TOKEN_NEXT_ELEMENT); if (bContinue) MatchExpectedToken(Token::TOKEN_NEXT_ELEMENT, tokenStream); } MatchExpectedToken(Token::TOKEN_ARRAY_END, tokenStream); } inline void Reader::Parse(String& string, Reader::TokenStream& tokenStream) { string = MatchExpectedToken(Token::TOKEN_STRING, tokenStream); } inline void Reader::Parse(Number& number, Reader::TokenStream& tokenStream) { const Token& currentToken = tokenStream.Peek(); // might need this later for throwing exception const std::string& sValue = MatchExpectedToken(Token::TOKEN_NUMBER, tokenStream); std::istringstream iStr(sValue); double dValue; iStr >> dValue; // did we consume all characters in the token? if (iStr.eof() == false) { char c = iStr.peek(); std::string sMessage = std::string("Unexpected character in NUMBER token: ") + c; throw ParseException(sMessage, currentToken.locBegin, currentToken.locEnd); } number = dValue; } inline void Reader::Parse(Boolean& boolean, Reader::TokenStream& tokenStream) { const std::string& sValue = MatchExpectedToken(Token::TOKEN_BOOLEAN, tokenStream); boolean = (sValue == "true" ? true : false); } inline void Reader::Parse(Null&, Reader::TokenStream& tokenStream) { MatchExpectedToken(Token::TOKEN_NULL, tokenStream); } inline const std::string& Reader::MatchExpectedToken(Token::Type nExpected, Reader::TokenStream& tokenStream) { const Token& token = tokenStream.Get(); if (token.nType != nExpected) { std::string sMessage = std::string("Unexpected token: ") + token.sValue; throw ParseException(sMessage, token.locBegin, token.locEnd); } return token.sValue; } } // End namespace cajun-jsonapi-cajun-2.0.3/json/visitor.h000066400000000000000000000046651222100160300201720ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #pragma once #include "elements.h" namespace json { class Visitor { public: virtual ~Visitor() {} virtual void Visit(Array& array) = 0; virtual void Visit(Object& object) = 0; virtual void Visit(Number& number) = 0; virtual void Visit(String& string) = 0; virtual void Visit(Boolean& boolean) = 0; virtual void Visit(Null& null) = 0; }; class ConstVisitor { public: virtual ~ConstVisitor() {} virtual void Visit(const Array& array) = 0; virtual void Visit(const Object& object) = 0; virtual void Visit(const Number& number) = 0; virtual void Visit(const String& string) = 0; virtual void Visit(const Boolean& boolean) = 0; virtual void Visit(const Null& null) = 0; }; } // End namespace cajun-jsonapi-cajun-2.0.3/json/writer.h000066400000000000000000000060701222100160300177770ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #pragma once #include "elements.h" #include "visitor.h" namespace json { class Writer : private ConstVisitor { public: static void Write(const Object& object, std::ostream& ostr); static void Write(const Array& array, std::ostream& ostr); static void Write(const String& string, std::ostream& ostr); static void Write(const Number& number, std::ostream& ostr); static void Write(const Boolean& boolean, std::ostream& ostr); static void Write(const Null& null, std::ostream& ostr); static void Write(const UnknownElement& elementRoot, std::ostream& ostr); private: Writer(std::ostream& ostr); template static void Write_i(const ElementTypeT& element, std::ostream& ostr); void Write_i(const Object& object); void Write_i(const Array& array); void Write_i(const String& string); void Write_i(const Number& number); void Write_i(const Boolean& boolean); void Write_i(const Null& null); void Write_i(const UnknownElement& unknown); virtual void Visit(const Array& array); virtual void Visit(const Object& object); virtual void Visit(const Number& number); virtual void Visit(const String& string); virtual void Visit(const Boolean& boolean); virtual void Visit(const Null& null); std::ostream& m_ostr; int m_nTabDepth; }; } // End namespace #include "writer.inl"cajun-jsonapi-cajun-2.0.3/json/writer.inl000066400000000000000000000161231222100160300203320ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #include "writer.h" #include #include /* TODO: * better documentation */ namespace json { inline void Writer::Write(const UnknownElement& elementRoot, std::ostream& ostr) { Write_i(elementRoot, ostr); } inline void Writer::Write(const Object& object, std::ostream& ostr) { Write_i(object, ostr); } inline void Writer::Write(const Array& array, std::ostream& ostr) { Write_i(array, ostr); } inline void Writer::Write(const Number& number, std::ostream& ostr) { Write_i(number, ostr); } inline void Writer::Write(const String& string, std::ostream& ostr) { Write_i(string, ostr); } inline void Writer::Write(const Boolean& boolean, std::ostream& ostr) { Write_i(boolean, ostr); } inline void Writer::Write(const Null& null, std::ostream& ostr) { Write_i(null, ostr); } inline Writer::Writer(std::ostream& ostr) : m_ostr(ostr), m_nTabDepth(0) {} template void Writer::Write_i(const ElementTypeT& element, std::ostream& ostr) { Writer writer(ostr); writer.Write_i(element); ostr.flush(); // all done } inline void Writer::Write_i(const Array& array) { if (array.Empty()) m_ostr << "[]"; else { m_ostr << '[' << std::endl; ++m_nTabDepth; Array::const_iterator it(array.Begin()), itEnd(array.End()); while (it != itEnd) { m_ostr << std::string(m_nTabDepth, '\t'); Write_i(*it); if (++it != itEnd) m_ostr << ','; m_ostr << std::endl; } --m_nTabDepth; m_ostr << std::string(m_nTabDepth, '\t') << ']'; } } inline void Writer::Write_i(const Object& object) { if (object.Empty()) m_ostr << "{}"; else { m_ostr << '{' << std::endl; ++m_nTabDepth; Object::const_iterator it(object.Begin()), itEnd(object.End()); while (it != itEnd) { m_ostr << std::string(m_nTabDepth, '\t'); Write_i(it->name); m_ostr << " : "; Write_i(it->element); if (++it != itEnd) m_ostr << ','; m_ostr << std::endl; } --m_nTabDepth; m_ostr << std::string(m_nTabDepth, '\t') << '}'; } } inline void Writer::Write_i(const Number& numberElement) { m_ostr << std::dec << std::setprecision(20) << numberElement.Value(); } inline void Writer::Write_i(const Boolean& booleanElement) { m_ostr << (booleanElement.Value() ? "true" : "false"); } inline void Writer::Write_i(const String& stringElement) { m_ostr << '"'; const std::string& s = stringElement.Value(); std::string::const_iterator it(s.begin()), itEnd(s.end()); for (; it != itEnd; ++it) { // check for UTF-8 unicode encoding unsigned char u = static_cast(*it); if (u & 0xc0) { if ((u & 0xe0) == 0xc0) { // two-character sequence int x = (*it & 0x1f) << 6; if ((it + 1) == itEnd) { m_ostr << *it; continue; } u = static_cast(*(it + 1)); if ((u & 0xc0) == 0x80) { x |= u & 0x3f; m_ostr << "\\u" << std::hex << std::setfill('0') << std::setw(4) << x; ++it; continue; } } else if ((u & 0xf0) == 0xe0) { // three-character sequence int x = (u & 0x0f) << 12; if ((it + 1) == itEnd) { m_ostr << *it; continue; } u = static_cast(*(it + 1)); if ((u & 0xc0) == 0x80) { x |= (u & 0x3f) << 6; if ((it + 2) == itEnd) { m_ostr << *it; continue; } u = static_cast(*(it + 2)); if ((u & 0xc0) == 0x80) { x |= u & 0x3f; m_ostr << "\\u" << std::hex << std::setfill('0') << std::setw(4) << x; it = it + 2; continue; } } } } switch (*it) { case '"': m_ostr << "\\\""; break; case '\\': m_ostr << "\\\\"; break; case '\b': m_ostr << "\\b"; break; case '\f': m_ostr << "\\f"; break; case '\n': m_ostr << "\\n"; break; case '\r': m_ostr << "\\r"; break; case '\t': m_ostr << "\\t"; break; default: m_ostr << *it; break; } } m_ostr << '"'; } inline void Writer::Write_i(const Null& ) { m_ostr << "null"; } inline void Writer::Write_i(const UnknownElement& unknown) { unknown.Accept(*this); } inline void Writer::Visit(const Array& array) { Write_i(array); } inline void Writer::Visit(const Object& object) { Write_i(object); } inline void Writer::Visit(const Number& number) { Write_i(number); } inline void Writer::Visit(const String& string) { Write_i(string); } inline void Writer::Visit(const Boolean& boolean) { Write_i(boolean); } inline void Writer::Visit(const Null& null) { Write_i(null); } } // End namespace cajun-jsonapi-cajun-2.0.3/test.cpp000066400000000000000000000251661222100160300170330ustar00rootroot00000000000000/****************************************************************************** Copyright (c) 2009-2010, Terry Caton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the projecct nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #include "json/reader.h" #include "json/writer.h" #include "json/elements.h" #include int main() { using namespace json; /* we'll generate: { "Delicious Beers" : [ { "Name" : "Schlafly American Pale Ale", "Origin" : "St. Louis, MO, USA", "ABV" : 5.9, "BottleConditioned" : true }, { "Name" : "John Smith's Extra Smooth", "Origin" : "Tadcaster, Yorkshire, UK", "ABV" : 3.8, "Bottle Conditioned" : false } ] } */ //////////////////////////////////////////////////////////////////// // construction // we can build a document piece by piece... Object objAPA; objAPA["Name"] = String("Schlafly American Pale Ale"); objAPA["Origin"] = String("St. Louis, MO, USA"); objAPA["ABV"] = Number(3.8); objAPA["BottleConditioned"] = Boolean(true); Array arrayBeer; arrayBeer.Insert(objAPA); Object objDocument; objDocument["Delicious Beers"] = arrayBeer; Number numDeleteThis = objDocument["AnotherMember"]; // ...or, we can use UnknownElement's chaining child element access to quickly // construct the remainder objDocument["Delicious Beers"][1]["Name"] = String("John Smith's Extra Smooth"); objDocument["Delicious Beers"][1]["Origin"] = String("Tadcaster, Yorkshire, UK"); objDocument["Delicious Beers"][1]["ABV"] = Number(3.8); objDocument["Delicious Beers"][1]["BottleConditioned"] = Boolean(false); //////////////////////////////////////////////////////////////////// // interpretation // perform all read operations on a const ref, otherwise we may end up // manipulating the document instead of catching errors const Object& objRoot = objDocument; // the return type of Object::operator[string] & Array::operator[size_t] is UnknownElement, which // provides implicit casting to any of the other element types... const Array& arrayBeers = objRoot["Delicious Beers"]; const Object& objBeer0 = arrayBeers[0]; const String& strName0 = objBeer0["Name"]; // ...it also supports operator[string] & operator[size_t] itself, which takes the implicit casting // one step further. operator[string] implicitly casts to Object, and operator[size_t] to Array. // the return value is another UnknownElement, so these operations can be strung together const Number numAbv1 = objRoot["Delicious Beers"][1]["ABV"]; std::cout << "First beer name: " << strName0.Value() << std::endl; std::cout << "First beer ABV: " << numAbv1.Value() << std::endl; // we can also iterate through the child elements of an array or object, which is necessary // when we don't know the structure of the document Array::const_iterator itBeers(arrayBeers.Begin()), itBeersEnd(arrayBeers.End()); for (; itBeers != itBeersEnd; ++itBeers) { // remember, *itArray is an UnknownElement, which can be implicitly cast to another element type const Object& objBeer = *itBeers; Object::const_iterator itBeerFacts(objBeer.Begin()), itBeerFactsEnd(objBeer.End()); for (; itBeerFacts != itBeerFactsEnd; ++itBeerFacts) { const Object::Member& member = *itBeerFacts; const std::string& name = member.name; const UnknownElement& element = member.element; // if we didn't know the structure of the itBeerFacts subtree, we could visit it // element.Accept(nonExistantVisitor); } } // everything's cool until we try to access a non-existent array element try { std::cout << "Expecting exception: Array out of bounds" << std::endl; const String& strName2 = arrayBeers[2]; } catch (const Exception& e) { std::cout << "Caught json::Exception: " << e.what() << std::endl << std::endl; } // an exception will be thrown when expected data not found, since "Rice" is never a member of good beer try { std::cout << "Expecting exception: Object member not found" << std::endl; const Boolean& boolRice = objRoot["Delicious Beers"][1]["Rice"]; } catch (const Exception& e) { std::cout << "Caught json::Exception: " << e.what() << std::endl << std::endl; } // we'll also get an error if the document structure isn't quite what we expect try { // objRoot["Delicious Beers"] is an Array, not another Object, so the second chained operator[string] will fail std::cout << "Expecting exception: Bad cast" << std::endl; const UnknownElement& elem = objRoot["Delicious Beers"]["Some Object Member"]; } catch (json::Exception& e) { std::cout << "Caught json::Exception: " << e.what() << std::endl << std::endl; } //////////////////////////////////////////////////////////////////// // document deep copying // we can make an exact duplicate too Object objRoot2 = objRoot; // the two documents should start out equal bool bEqualInitially = (objRoot == objRoot2); std::cout << "Document copies should start out equivalent. operator == returned: " << (bEqualInitially ? "true" : "false") << std::endl; // prove objRoot2 is a deep copy of objRoot: // remove Beers[1] Array& array = objRoot2["Delicious Beers"]; array.Erase(array.Begin()); // trim it down to one. this leaves elemRoot the same // the two documents should start out equal bool bEqualNow = (objRoot == objRoot2); std::cout << "Document copies should now be different. operator == returned: " << (bEqualNow ? "true" : "false") << std::endl << std::endl; //////////////////////////////////////////////////////////////////// // read/write sanity check // write it out to a string stream (file stream would work the same).... std::cout << "Writing file out..."; std::stringstream stream; Writer::Write(objRoot, stream); // ...then read it back in. we know it's an Object std::cout << "then reading it back in." << std::endl; Object elemRootFile; Reader::Read(elemRootFile, stream); // still look right? bool bEquals = (objRoot == elemRootFile); std::cout << "Original document and streamed document should be equivalent. operator == returned: " << (bEquals ? "true" : "false") << std::endl << std::endl; //////////////////////////////////////////////////////////////////// // document read error handling // mis-predicting type type will fail with a parse error. we'll try reading an array into an object try { std::istringstream sBadDocument("[1, 2]"); // missing comma! std::cout << "Reading Object-based document into an Array; expecting Parse exception" << std::endl; Object objDocument; Reader::Read(objDocument, sBadDocument); } catch (Reader::ParseException& e) { // lines/offsets are zero-indexed, so bump them up by one for human presentation std::cout << "Caught json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << std::endl << std::endl; } // reading in a slightly malformed document may result in a parse error try { std::istringstream sBadDocument("[1, 2 3]"); // missing comma! std::cout << "Reading malformed document; expecting Parse exception" << std::endl; Array arrayDocument; Reader::Read(arrayDocument, sBadDocument); } catch (Reader::ParseException& e) { std::cout << "Caught json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << std::endl << std::endl; } // reading in gibberish will generate a scan error try { std::istringstream sBadDocument("[true, false, true, #.&@k*k4L!`1"); std::cout << "Reading complete garbage; expecting Scan exception" << std::endl; Array arrayDocument; Reader::Read(arrayDocument, sBadDocument); } catch (Reader::ScanException& e) { std::cout << "Caught json::ScanException: " << e.what() << ", Line/offset: " << e.m_locError.m_nLine + 1 << '/' << e.m_locError.m_nLineOffset + 1 << std::endl << std::endl; } try { std::istringstream sIncompleteDocument("[ true, "); std::cout << "Reading incomplete document; expecting Parse exception" << std::endl; json::Array arrayDocument; json::Reader::Read(arrayDocument, sIncompleteDocument); } catch (Reader::ParseException& e) { std::cout << "Caught json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << std::endl << std::endl; } return 0; } cajun-jsonapi-cajun-2.0.3/test8.vcproj000066400000000000000000000204611222100160300176350ustar00rootroot00000000000000 cajun-jsonapi-cajun-2.0.3/test9.vcproj000066400000000000000000000203431222100160300176350ustar00rootroot00000000000000