From: Baptiste L. <bl...@us...> - 2005-07-28 08:21:20
|
Update of /cvsroot/jsoncpp/jsoncpp/include/json In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23924/include/json Modified Files: json_writer.h Log Message: * added implementation of StyledWriter which output JSON value formatted to be easy read by human. Index: json_writer.h =================================================================== RCS file: /cvsroot/jsoncpp/jsoncpp/include/json/json_writer.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** json_writer.h 27 Jul 2005 07:09:23 -0000 1.1.1.1 --- json_writer.h 28 Jul 2005 08:21:10 -0000 1.2 *************** *** 10,14 **** class Value; - // @todo extends to allow rewriting while preserving existing comment & inserting other. class JSON_API FastWriter { --- 10,13 ---- *************** *** 22,25 **** --- 21,67 ---- }; + // @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: + * - Object value: + * - if empty then print {} without indent and line break + * - if not empty the print '{', line break & indent, print one value per line + * and then unindent and line break and print '}'. + * - Array value: + * - if empty then print [] without indent and line break + * - if the array contains no object value, empty array or some other value types, + * and all the values fit on one lines, then print the array on a single line. + * - 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. + */ + class JSON_API StyledWriter + { + public: + StyledWriter(); + + std::string write( const Value &root ); + + private: + void writeValue( const Value &value ); + void writeArrayValue( const Value &value ); + bool isMultineArray( const Value &value ); + void pushValue( const std::string &value ); + void writeIndent(); + void writeWithIndent( const std::string &value ); + void indent(); + void unindent(); + + typedef std::vector<std::string> ChildValues; + + ChildValues childValues_; + std::string document_; + std::string indentString_; + int rightMargin_; + int indentSize_; + bool addChildValues_; + }; + + std::string JSON_API valueToString( Value::Int value ); std::string JSON_API valueToString( Value::UInt value ); |