From: Baptiste L. <bl...@us...> - 2005-07-30 11:15:21
|
Update of /cvsroot/jsoncpp/jsoncpp/include/json In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3607/include/json Modified Files: json_reader.h json_forwards.h json_value.h json_writer.h Log Message: * added doxygen documentation Index: json_value.h =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/include/json/json_value.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** json_value.h 27 Jul 2005 07:38:15 -0000 1.2 --- json_value.h 30 Jul 2005 11:15:11 -0000 1.3 *************** *** 2,5 **** --- 2,6 ---- # define CPPTL_JSON_H_INCLUDED + # include "json_forwards.h" # include "json_config.h" # include <string> *************** *** 11,26 **** class Value; enum ValueType { ! nullValue = 0, ! intValue, ! uintValue, ! realValue, ! stringValue, ! booleanValue, ! arrayValue, ! objectValue }; class JSON_API Value { --- 12,56 ---- class Value; + /** \brief Type of the value held by a Value object. + */ enum ValueType { ! nullValue = 0, ///< 'null' value ! intValue, ///< signed integer value ! uintValue, ///< unsigned integer value ! realValue, ///< double value ! stringValue, ///< UTF-8 string value ! booleanValue, ///< bool value ! arrayValue, ///< array value (ordered list) ! objectValue ///< object value (collection of name/value pairs). }; + /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value. + * + * This class is a discriminated union wrapper that can represents a: + * - signed integer [range: Value::minInt - Value::maxInt] + * - unsigned integer (range: 0 - Value::maxUInt) + * - double + * - UTF-8 string + * - boolean + * - 'null' + * - an ordered list of Value + * - collection of name/value pairs (javascript object) + * + * The type of the held value is represented by a #ValueType and + * can be obtained using type(). + * + * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. + * Non const methods will automatically create the a #nullValue element + * if it does not exist. + * The sequence of an #arrayValue will be automatically resize and initialized + * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. + * + * The get() methods can be used to obtanis default value in the case the required element + * does not exist. + * + * It is possible to iterate over the list of a #objectValue values using + * the getMemberNames() method. + */ class JSON_API Value { *************** *** 123,126 **** --- 153,159 ---- Members getMemberNames() const; + void setId( ValueId id ); + ValueId id() const; + private: struct CZString *************** *** 149,153 **** typedef std::map<CZString, Value> ObjectValues; - ValueType type_; union ValueHolder { --- 182,185 ---- *************** *** 159,165 **** ObjectValues *map_; } value_; ! Value *parent_; ! Value *previous_; ! Value *next_; }; --- 191,196 ---- ObjectValues *map_; } value_; ! ValueType type_; ! ValueId id_; }; Index: json_reader.h =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/include/json/json_reader.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** json_reader.h 27 Jul 2005 07:09:23 -0000 1.1.1.1 --- json_reader.h 30 Jul 2005 11:15:11 -0000 1.2 *************** *** 2,7 **** --- 2,9 ---- # define CPPTL_JSON_READER_H_INCLUDED + # include "json_forwards.h" # include "json_config.h" # include <deque> + # include <map> # include <stack> # include <string> *************** *** 9,14 **** --- 11,21 ---- namespace Json { + class CommentMemo; class Value; + /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value. + * + * + */ class JSON_API Reader { *************** *** 19,25 **** --- 26,55 ---- Reader(); + /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document. + * \param document UTF-8 encoded string containing the document to read. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \return \c true if the document was successfully parsed, \c false if an error occurred. + */ bool parse( const std::string &document, Value &root ); + /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document and preserve comments. + * \param document [in] UTF-8 encoded string containing the document to read. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \param commentMemo [out] Contains the comment associated with the value if it was successfully + * parsed. + * \return \c true if the document was successfully parsed, \c false if an error occurred. + */ + bool parse( const std::string &document, + Value &root, + CommentMemo &commentMemo ); + + /** \brief Returns a user friendly string that list errors in the parsed document. + * \return Formatted error message with the list of errors with their location in + * the parsed document. An empty string is returned if no error occurred + * during parsing. + */ std::string getFormatedErrorMessages() const; *************** *** 104,107 **** --- 134,191 ---- Location end_; Location current_; + CommentMemo *commentMemo_; + }; + + class JSON_API CommentMemo + { + public: + enum Placement + { + atDocumentStart = 1, + beforeValue, + afterValueOnSameLine, + atDocumentEnd + }; + + class CommentInfo + { + public: + std::string comment_; + Placement placement_; + ValueId id_; + }; + + CommentMemo(); + + void beginDocumentParsing(); + void parsedCComment( Reader::Location begin, + Reader::Location end ); + void parsedCppComment( Reader::Location begin, + Reader::Location end ); + void parsedValue( Reader::Location begin, + Reader::Location end, + Value &value ); + void endDocumentParsing(); + + unsigned int getCommentCount() const; + const CommentInfo &getComment( unsigned int index ) const; + const CommentInfo *findComment( ValueId id, Placement placement ) const; + private: + static bool containsNewLine( Reader::Location begin, + Reader::Location end ); + void addComment( Reader::Location begin, + Reader::Location end, + Placement placement ); + void registerComment( Placement placement, + unsigned int index ); + + + typedef std::deque<CommentInfo> Comments; + typedef std::map<unsigned int> CommentsById; + Comments comments_; + CommentsById commentsById_; + ValueId currentId_; + Reader::Location lastValueEnd_; + int monitorNextValue_; }; Index: json_forwards.h =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/include/json/json_forwards.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** json_forwards.h 27 Jul 2005 07:36:26 -0000 1.2 --- json_forwards.h 30 Jul 2005 11:15:11 -0000 1.3 *************** *** 4,12 **** namespace Json { ! class Value; ! class Reader; class FastWriter; class Path; class PathArgument; } // namespace Json --- 4,16 ---- namespace Json { ! class CommentMemo; class FastWriter; class Path; class PathArgument; + class Reader; + class StyledWriter; + class Value; + + typedef unsigned int ValueId; } // namespace Json Index: json_writer.h =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/include/json/json_writer.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** json_writer.h 28 Jul 2005 08:21:10 -0000 1.2 --- json_writer.h 30 Jul 2005 11:15:11 -0000 1.3 *************** *** 3,6 **** --- 3,7 ---- # include "json_value.h" + # include "json_reader.h" # include <deque> # include <string> *************** *** 10,13 **** --- 11,20 ---- class Value; + /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly). + * + * The JSON document is written in a single line. It is not intended for 'human' consumption, + * but may be usefull to support feature such as RPC where bandwith is limited. + * \sa Reader, Value + */ class JSON_API FastWriter { *************** *** 21,26 **** }; ! // @todo extends to allow rewriting while preserving existing comment & inserting other. ! /* Write the JSON values in a human readable way. * * The rules for line break and indent are as follow: --- 28,32 ---- }; ! /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way. * * The rules for line break and indent are as follow: *************** *** 35,38 **** --- 41,45 ---- * - otherwise, it the values do not fit on one line, or the array contains * object or non empty array, then print one value per line. + * \sa Reader, Value */ class JSON_API StyledWriter *************** *** 41,46 **** --- 48,65 ---- StyledWriter(); + /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. + * \param root Value to serialize. + * \return String containing the JSON document that represent the root value. + */ std::string write( const Value &root ); + /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. + * \param root Value to serialize. + * \param commentMemo Memo generated while parsing a JSON document. + * \return String containing the JSON document that represent the root value. + */ + std::string write( const Value &root, + const CommentMemo &commentMemo ); + private: void writeValue( const Value &value ); *************** *** 52,55 **** --- 71,80 ---- void indent(); void unindent(); + void writeInitialComment( const Value &root ); + void writeFinalComment( const Value &root ); + bool hasMoreComments() const; + const CommentMemo::CommentInfo &getCurrentComment() const; + void writeComment( const CommentMemo::CommentInfo &comment ); + bool hasCommentForValue( const Value &value ) const; typedef std::vector<std::string> ChildValues; *************** *** 60,63 **** --- 85,90 ---- int rightMargin_; int indentSize_; + const CommentMemo *commentMemo_; + unsigned int currentComment_; bool addChildValues_; }; |