annotate third_party/json/writer.h @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
rev   line source
ian@0 1 // Copyright 2007-2010 Baptiste Lepilleur
ian@0 2 // Distributed under MIT license, or public domain if desired and
ian@0 3 // recognized in your jurisdiction.
ian@0 4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
ian@0 5
ian@0 6 #ifndef JSON_WRITER_H_INCLUDED
ian@0 7 # define JSON_WRITER_H_INCLUDED
ian@0 8
ian@0 9 #if !defined(JSON_IS_AMALGAMATION)
ian@0 10 # include "value.h"
ian@0 11 #endif // if !defined(JSON_IS_AMALGAMATION)
ian@0 12 # include <vector>
ian@0 13 # include <string>
ian@0 14 # include <iostream>
ian@0 15
ian@0 16 namespace Json {
ian@0 17
ian@0 18 class Value;
ian@0 19
ian@0 20 /** \brief Abstract class for writers.
ian@0 21 */
ian@0 22 class JSON_API Writer
ian@0 23 {
ian@0 24 public:
ian@0 25 virtual ~Writer();
ian@0 26
ian@0 27 virtual std::string write( const Value &root ) = 0;
ian@0 28 };
ian@0 29
ian@0 30 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
ian@0 31 *
ian@0 32 * The JSON document is written in a single line. It is not intended for 'human' consumption,
ian@0 33 * but may be usefull to support feature such as RPC where bandwith is limited.
ian@0 34 * \sa Reader, Value
ian@0 35 */
ian@0 36 class JSON_API FastWriter : public Writer
ian@0 37 {
ian@0 38 public:
ian@0 39 FastWriter();
ian@0 40 virtual ~FastWriter(){}
ian@0 41
ian@0 42 void enableYAMLCompatibility();
ian@0 43
ian@0 44 public: // overridden from Writer
ian@0 45 virtual std::string write( const Value &root );
ian@0 46
ian@0 47 private:
ian@0 48 void writeValue( const Value &value );
ian@0 49
ian@0 50 std::string document_;
ian@0 51 bool yamlCompatiblityEnabled_;
ian@0 52 };
ian@0 53
ian@0 54 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
ian@0 55 *
ian@0 56 * The rules for line break and indent are as follow:
ian@0 57 * - Object value:
ian@0 58 * - if empty then print {} without indent and line break
ian@0 59 * - if not empty the print '{', line break & indent, print one value per line
ian@0 60 * and then unindent and line break and print '}'.
ian@0 61 * - Array value:
ian@0 62 * - if empty then print [] without indent and line break
ian@0 63 * - if the array contains no object value, empty array or some other value types,
ian@0 64 * and all the values fit on one lines, then print the array on a single line.
ian@0 65 * - otherwise, it the values do not fit on one line, or the array contains
ian@0 66 * object or non empty array, then print one value per line.
ian@0 67 *
ian@0 68 * If the Value have comments then they are outputed according to their #CommentPlacement.
ian@0 69 *
ian@0 70 * \sa Reader, Value, Value::setComment()
ian@0 71 */
ian@0 72 class JSON_API StyledWriter: public Writer
ian@0 73 {
ian@0 74 public:
ian@0 75 StyledWriter();
ian@0 76 virtual ~StyledWriter(){}
ian@0 77
ian@0 78 public: // overridden from Writer
ian@0 79 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
ian@0 80 * \param root Value to serialize.
ian@0 81 * \return String containing the JSON document that represents the root value.
ian@0 82 */
ian@0 83 virtual std::string write( const Value &root );
ian@0 84
ian@0 85 private:
ian@0 86 void writeValue( const Value &value );
ian@0 87 void writeArrayValue( const Value &value );
ian@0 88 bool isMultineArray( const Value &value );
ian@0 89 void pushValue( const std::string &value );
ian@0 90 void writeIndent();
ian@0 91 void writeWithIndent( const std::string &value );
ian@0 92 void indent();
ian@0 93 void unindent();
ian@0 94 void writeCommentBeforeValue( const Value &root );
ian@0 95 void writeCommentAfterValueOnSameLine( const Value &root );
ian@0 96 bool hasCommentForValue( const Value &value );
ian@0 97 static std::string normalizeEOL( const std::string &text );
ian@0 98
ian@0 99 typedef std::vector<std::string> ChildValues;
ian@0 100
ian@0 101 ChildValues childValues_;
ian@0 102 std::string document_;
ian@0 103 std::string indentString_;
ian@0 104 int rightMargin_;
ian@0 105 int indentSize_;
ian@0 106 bool addChildValues_;
ian@0 107 };
ian@0 108
ian@0 109 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
ian@0 110 to a stream rather than to a string.
ian@0 111 *
ian@0 112 * The rules for line break and indent are as follow:
ian@0 113 * - Object value:
ian@0 114 * - if empty then print {} without indent and line break
ian@0 115 * - if not empty the print '{', line break & indent, print one value per line
ian@0 116 * and then unindent and line break and print '}'.
ian@0 117 * - Array value:
ian@0 118 * - if empty then print [] without indent and line break
ian@0 119 * - if the array contains no object value, empty array or some other value types,
ian@0 120 * and all the values fit on one lines, then print the array on a single line.
ian@0 121 * - otherwise, it the values do not fit on one line, or the array contains
ian@0 122 * object or non empty array, then print one value per line.
ian@0 123 *
ian@0 124 * If the Value have comments then they are outputed according to their #CommentPlacement.
ian@0 125 *
ian@0 126 * \param indentation Each level will be indented by this amount extra.
ian@0 127 * \sa Reader, Value, Value::setComment()
ian@0 128 */
ian@0 129 class JSON_API StyledStreamWriter
ian@0 130 {
ian@0 131 public:
ian@0 132 StyledStreamWriter( std::string indentation="\t" );
ian@0 133 ~StyledStreamWriter(){}
ian@0 134
ian@0 135 public:
ian@0 136 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
ian@0 137 * \param out Stream to write to. (Can be ostringstream, e.g.)
ian@0 138 * \param root Value to serialize.
ian@0 139 * \note There is no point in deriving from Writer, since write() should not return a value.
ian@0 140 */
ian@0 141 void write( std::ostream &out, const Value &root );
ian@0 142
ian@0 143 private:
ian@0 144 void writeValue( const Value &value );
ian@0 145 void writeArrayValue( const Value &value );
ian@0 146 bool isMultineArray( const Value &value );
ian@0 147 void pushValue( const std::string &value );
ian@0 148 void writeIndent();
ian@0 149 void writeWithIndent( const std::string &value );
ian@0 150 void indent();
ian@0 151 void unindent();
ian@0 152 void writeCommentBeforeValue( const Value &root );
ian@0 153 void writeCommentAfterValueOnSameLine( const Value &root );
ian@0 154 bool hasCommentForValue( const Value &value );
ian@0 155 static std::string normalizeEOL( const std::string &text );
ian@0 156
ian@0 157 typedef std::vector<std::string> ChildValues;
ian@0 158
ian@0 159 ChildValues childValues_;
ian@0 160 std::ostream* document_;
ian@0 161 std::string indentString_;
ian@0 162 int rightMargin_;
ian@0 163 std::string indentation_;
ian@0 164 bool addChildValues_;
ian@0 165 };
ian@0 166
ian@0 167 # if defined(JSON_HAS_INT64)
ian@0 168 std::string JSON_API valueToString( Int value );
ian@0 169 std::string JSON_API valueToString( UInt value );
ian@0 170 # endif // if defined(JSON_HAS_INT64)
ian@0 171 std::string JSON_API valueToString( LargestInt value );
ian@0 172 std::string JSON_API valueToString( LargestUInt value );
ian@0 173 std::string JSON_API valueToString( double value );
ian@0 174 std::string JSON_API valueToString( bool value );
ian@0 175 std::string JSON_API valueToQuotedString( const char *value );
ian@0 176
ian@0 177 /// \brief Output using the StyledStreamWriter.
ian@0 178 /// \see Json::operator>>()
ian@0 179 std::ostream& operator<<( std::ostream&, const Value &root );
ian@0 180
ian@0 181 } // namespace Json
ian@0 182
ian@0 183
ian@0 184
ian@0 185 #endif // JSON_WRITER_H_INCLUDED