涂 桂旺
/
mbed-os-example-cj
json test
jsoncpp/include/json/writer.h@0:2ee762ea11b3, 2018-01-26 (annotated)
- Committer:
- tgw
- Date:
- Fri Jan 26 06:05:31 2018 +0000
- Revision:
- 0:2ee762ea11b3
json
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tgw | 0:2ee762ea11b3 | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
tgw | 0:2ee762ea11b3 | 2 | // Distributed under MIT license, or public domain if desired and |
tgw | 0:2ee762ea11b3 | 3 | // recognized in your jurisdiction. |
tgw | 0:2ee762ea11b3 | 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
tgw | 0:2ee762ea11b3 | 5 | |
tgw | 0:2ee762ea11b3 | 6 | #ifndef JSON_WRITER_H_INCLUDED |
tgw | 0:2ee762ea11b3 | 7 | #define JSON_WRITER_H_INCLUDED |
tgw | 0:2ee762ea11b3 | 8 | |
tgw | 0:2ee762ea11b3 | 9 | #if !defined(JSON_IS_AMALGAMATION) |
tgw | 0:2ee762ea11b3 | 10 | #include "value.h" |
tgw | 0:2ee762ea11b3 | 11 | #endif // if !defined(JSON_IS_AMALGAMATION) |
tgw | 0:2ee762ea11b3 | 12 | #include <vector> |
tgw | 0:2ee762ea11b3 | 13 | #include <string> |
tgw | 0:2ee762ea11b3 | 14 | #include <ostream> |
tgw | 0:2ee762ea11b3 | 15 | |
tgw | 0:2ee762ea11b3 | 16 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
tgw | 0:2ee762ea11b3 | 17 | // be used by... |
tgw | 0:2ee762ea11b3 | 18 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 19 | #pragma warning(push) |
tgw | 0:2ee762ea11b3 | 20 | #pragma warning(disable : 4251) |
tgw | 0:2ee762ea11b3 | 21 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
tgw | 0:2ee762ea11b3 | 22 | |
tgw | 0:2ee762ea11b3 | 23 | #pragma pack(push, 8) |
tgw | 0:2ee762ea11b3 | 24 | |
tgw | 0:2ee762ea11b3 | 25 | namespace Json { |
tgw | 0:2ee762ea11b3 | 26 | |
tgw | 0:2ee762ea11b3 | 27 | class Value; |
tgw | 0:2ee762ea11b3 | 28 | |
tgw | 0:2ee762ea11b3 | 29 | /** |
tgw | 0:2ee762ea11b3 | 30 | |
tgw | 0:2ee762ea11b3 | 31 | Usage: |
tgw | 0:2ee762ea11b3 | 32 | \code |
tgw | 0:2ee762ea11b3 | 33 | using namespace Json; |
tgw | 0:2ee762ea11b3 | 34 | void writeToStdout(StreamWriter::Factory const& factory, Value const& value) { |
tgw | 0:2ee762ea11b3 | 35 | std::unique_ptr<StreamWriter> const writer( |
tgw | 0:2ee762ea11b3 | 36 | factory.newStreamWriter()); |
tgw | 0:2ee762ea11b3 | 37 | writer->write(value, &std::cout); |
tgw | 0:2ee762ea11b3 | 38 | std::cout << std::endl; // add lf and flush |
tgw | 0:2ee762ea11b3 | 39 | } |
tgw | 0:2ee762ea11b3 | 40 | \endcode |
tgw | 0:2ee762ea11b3 | 41 | */ |
tgw | 0:2ee762ea11b3 | 42 | class JSON_API StreamWriter { |
tgw | 0:2ee762ea11b3 | 43 | protected: |
tgw | 0:2ee762ea11b3 | 44 | JSONCPP_OSTREAM* sout_; // not owned; will not delete |
tgw | 0:2ee762ea11b3 | 45 | public: |
tgw | 0:2ee762ea11b3 | 46 | StreamWriter(); |
tgw | 0:2ee762ea11b3 | 47 | virtual ~StreamWriter(); |
tgw | 0:2ee762ea11b3 | 48 | /** Write Value into document as configured in sub-class. |
tgw | 0:2ee762ea11b3 | 49 | Do not take ownership of sout, but maintain a reference during function. |
tgw | 0:2ee762ea11b3 | 50 | \pre sout != NULL |
tgw | 0:2ee762ea11b3 | 51 | \return zero on success (For now, we always return zero, so check the stream instead.) |
tgw | 0:2ee762ea11b3 | 52 | \throw std::exception possibly, depending on configuration |
tgw | 0:2ee762ea11b3 | 53 | */ |
tgw | 0:2ee762ea11b3 | 54 | virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0; |
tgw | 0:2ee762ea11b3 | 55 | |
tgw | 0:2ee762ea11b3 | 56 | /** \brief A simple abstract factory. |
tgw | 0:2ee762ea11b3 | 57 | */ |
tgw | 0:2ee762ea11b3 | 58 | class JSON_API Factory { |
tgw | 0:2ee762ea11b3 | 59 | public: |
tgw | 0:2ee762ea11b3 | 60 | virtual ~Factory(); |
tgw | 0:2ee762ea11b3 | 61 | /** \brief Allocate a CharReader via operator new(). |
tgw | 0:2ee762ea11b3 | 62 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
tgw | 0:2ee762ea11b3 | 63 | */ |
tgw | 0:2ee762ea11b3 | 64 | virtual StreamWriter* newStreamWriter() const = 0; |
tgw | 0:2ee762ea11b3 | 65 | }; // Factory |
tgw | 0:2ee762ea11b3 | 66 | }; // StreamWriter |
tgw | 0:2ee762ea11b3 | 67 | |
tgw | 0:2ee762ea11b3 | 68 | /** \brief Write into stringstream, then return string, for convenience. |
tgw | 0:2ee762ea11b3 | 69 | * A StreamWriter will be created from the factory, used, and then deleted. |
tgw | 0:2ee762ea11b3 | 70 | */ |
tgw | 0:2ee762ea11b3 | 71 | JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root); |
tgw | 0:2ee762ea11b3 | 72 | |
tgw | 0:2ee762ea11b3 | 73 | |
tgw | 0:2ee762ea11b3 | 74 | /** \brief Build a StreamWriter implementation. |
tgw | 0:2ee762ea11b3 | 75 | |
tgw | 0:2ee762ea11b3 | 76 | Usage: |
tgw | 0:2ee762ea11b3 | 77 | \code |
tgw | 0:2ee762ea11b3 | 78 | using namespace Json; |
tgw | 0:2ee762ea11b3 | 79 | Value value = ...; |
tgw | 0:2ee762ea11b3 | 80 | StreamWriterBuilder builder; |
tgw | 0:2ee762ea11b3 | 81 | builder["commentStyle"] = "None"; |
tgw | 0:2ee762ea11b3 | 82 | builder["indentation"] = " "; // or whatever you like |
tgw | 0:2ee762ea11b3 | 83 | std::unique_ptr<Json::StreamWriter> writer( |
tgw | 0:2ee762ea11b3 | 84 | builder.newStreamWriter()); |
tgw | 0:2ee762ea11b3 | 85 | writer->write(value, &std::cout); |
tgw | 0:2ee762ea11b3 | 86 | std::cout << std::endl; // add lf and flush |
tgw | 0:2ee762ea11b3 | 87 | \endcode |
tgw | 0:2ee762ea11b3 | 88 | */ |
tgw | 0:2ee762ea11b3 | 89 | class JSON_API StreamWriterBuilder : public StreamWriter::Factory { |
tgw | 0:2ee762ea11b3 | 90 | public: |
tgw | 0:2ee762ea11b3 | 91 | // Note: We use a Json::Value so that we can add data-members to this class |
tgw | 0:2ee762ea11b3 | 92 | // without a major version bump. |
tgw | 0:2ee762ea11b3 | 93 | /** Configuration of this builder. |
tgw | 0:2ee762ea11b3 | 94 | Available settings (case-sensitive): |
tgw | 0:2ee762ea11b3 | 95 | - "commentStyle": "None" or "All" |
tgw | 0:2ee762ea11b3 | 96 | - "indentation": "<anything>" |
tgw | 0:2ee762ea11b3 | 97 | - "enableYAMLCompatibility": false or true |
tgw | 0:2ee762ea11b3 | 98 | - slightly change the whitespace around colons |
tgw | 0:2ee762ea11b3 | 99 | - "dropNullPlaceholders": false or true |
tgw | 0:2ee762ea11b3 | 100 | - Drop the "null" string from the writer's output for nullValues. |
tgw | 0:2ee762ea11b3 | 101 | Strictly speaking, this is not valid JSON. But when the output is being |
tgw | 0:2ee762ea11b3 | 102 | fed to a browser's JavaScript, it makes for smaller output and the |
tgw | 0:2ee762ea11b3 | 103 | browser can handle the output just fine. |
tgw | 0:2ee762ea11b3 | 104 | - "useSpecialFloats": false or true |
tgw | 0:2ee762ea11b3 | 105 | - If true, outputs non-finite floating point values in the following way: |
tgw | 0:2ee762ea11b3 | 106 | NaN values as "NaN", positive infinity as "Infinity", and negative infinity |
tgw | 0:2ee762ea11b3 | 107 | as "-Infinity". |
tgw | 0:2ee762ea11b3 | 108 | |
tgw | 0:2ee762ea11b3 | 109 | You can examine 'settings_` yourself |
tgw | 0:2ee762ea11b3 | 110 | to see the defaults. You can also write and read them just like any |
tgw | 0:2ee762ea11b3 | 111 | JSON Value. |
tgw | 0:2ee762ea11b3 | 112 | \sa setDefaults() |
tgw | 0:2ee762ea11b3 | 113 | */ |
tgw | 0:2ee762ea11b3 | 114 | Json::Value settings_; |
tgw | 0:2ee762ea11b3 | 115 | |
tgw | 0:2ee762ea11b3 | 116 | StreamWriterBuilder(); |
tgw | 0:2ee762ea11b3 | 117 | ~StreamWriterBuilder() JSONCPP_OVERRIDE; |
tgw | 0:2ee762ea11b3 | 118 | |
tgw | 0:2ee762ea11b3 | 119 | /** |
tgw | 0:2ee762ea11b3 | 120 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
tgw | 0:2ee762ea11b3 | 121 | */ |
tgw | 0:2ee762ea11b3 | 122 | StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE; |
tgw | 0:2ee762ea11b3 | 123 | |
tgw | 0:2ee762ea11b3 | 124 | /** \return true if 'settings' are legal and consistent; |
tgw | 0:2ee762ea11b3 | 125 | * otherwise, indicate bad settings via 'invalid'. |
tgw | 0:2ee762ea11b3 | 126 | */ |
tgw | 0:2ee762ea11b3 | 127 | bool validate(Json::Value* invalid) const; |
tgw | 0:2ee762ea11b3 | 128 | /** A simple way to update a specific setting. |
tgw | 0:2ee762ea11b3 | 129 | */ |
tgw | 0:2ee762ea11b3 | 130 | Value& operator[](JSONCPP_STRING key); |
tgw | 0:2ee762ea11b3 | 131 | |
tgw | 0:2ee762ea11b3 | 132 | /** Called by ctor, but you can use this to reset settings_. |
tgw | 0:2ee762ea11b3 | 133 | * \pre 'settings' != NULL (but Json::null is fine) |
tgw | 0:2ee762ea11b3 | 134 | * \remark Defaults: |
tgw | 0:2ee762ea11b3 | 135 | * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults |
tgw | 0:2ee762ea11b3 | 136 | */ |
tgw | 0:2ee762ea11b3 | 137 | static void setDefaults(Json::Value* settings); |
tgw | 0:2ee762ea11b3 | 138 | }; |
tgw | 0:2ee762ea11b3 | 139 | |
tgw | 0:2ee762ea11b3 | 140 | /** \brief Abstract class for writers. |
tgw | 0:2ee762ea11b3 | 141 | * \deprecated Use StreamWriter. (And really, this is an implementation detail.) |
tgw | 0:2ee762ea11b3 | 142 | */ |
tgw | 0:2ee762ea11b3 | 143 | class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer { |
tgw | 0:2ee762ea11b3 | 144 | public: |
tgw | 0:2ee762ea11b3 | 145 | virtual ~Writer(); |
tgw | 0:2ee762ea11b3 | 146 | |
tgw | 0:2ee762ea11b3 | 147 | virtual JSONCPP_STRING write(const Value& root) = 0; |
tgw | 0:2ee762ea11b3 | 148 | }; |
tgw | 0:2ee762ea11b3 | 149 | |
tgw | 0:2ee762ea11b3 | 150 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
tgw | 0:2ee762ea11b3 | 151 | *without formatting (not human friendly). |
tgw | 0:2ee762ea11b3 | 152 | * |
tgw | 0:2ee762ea11b3 | 153 | * The JSON document is written in a single line. It is not intended for 'human' |
tgw | 0:2ee762ea11b3 | 154 | *consumption, |
tgw | 0:2ee762ea11b3 | 155 | * but may be usefull to support feature such as RPC where bandwith is limited. |
tgw | 0:2ee762ea11b3 | 156 | * \sa Reader, Value |
tgw | 0:2ee762ea11b3 | 157 | * \deprecated Use StreamWriterBuilder. |
tgw | 0:2ee762ea11b3 | 158 | */ |
tgw | 0:2ee762ea11b3 | 159 | #if defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 160 | #pragma warning(push) |
tgw | 0:2ee762ea11b3 | 161 | #pragma warning(disable:4996) // Deriving from deprecated class |
tgw | 0:2ee762ea11b3 | 162 | #endif |
tgw | 0:2ee762ea11b3 | 163 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer { |
tgw | 0:2ee762ea11b3 | 164 | public: |
tgw | 0:2ee762ea11b3 | 165 | FastWriter(); |
tgw | 0:2ee762ea11b3 | 166 | ~FastWriter() JSONCPP_OVERRIDE {} |
tgw | 0:2ee762ea11b3 | 167 | |
tgw | 0:2ee762ea11b3 | 168 | void enableYAMLCompatibility(); |
tgw | 0:2ee762ea11b3 | 169 | |
tgw | 0:2ee762ea11b3 | 170 | /** \brief Drop the "null" string from the writer's output for nullValues. |
tgw | 0:2ee762ea11b3 | 171 | * Strictly speaking, this is not valid JSON. But when the output is being |
tgw | 0:2ee762ea11b3 | 172 | * fed to a browser's JavaScript, it makes for smaller output and the |
tgw | 0:2ee762ea11b3 | 173 | * browser can handle the output just fine. |
tgw | 0:2ee762ea11b3 | 174 | */ |
tgw | 0:2ee762ea11b3 | 175 | void dropNullPlaceholders(); |
tgw | 0:2ee762ea11b3 | 176 | |
tgw | 0:2ee762ea11b3 | 177 | void omitEndingLineFeed(); |
tgw | 0:2ee762ea11b3 | 178 | |
tgw | 0:2ee762ea11b3 | 179 | public: // overridden from Writer |
tgw | 0:2ee762ea11b3 | 180 | JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; |
tgw | 0:2ee762ea11b3 | 181 | |
tgw | 0:2ee762ea11b3 | 182 | private: |
tgw | 0:2ee762ea11b3 | 183 | void writeValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 184 | |
tgw | 0:2ee762ea11b3 | 185 | JSONCPP_STRING document_; |
tgw | 0:2ee762ea11b3 | 186 | bool yamlCompatibilityEnabled_; |
tgw | 0:2ee762ea11b3 | 187 | bool dropNullPlaceholders_; |
tgw | 0:2ee762ea11b3 | 188 | bool omitEndingLineFeed_; |
tgw | 0:2ee762ea11b3 | 189 | }; |
tgw | 0:2ee762ea11b3 | 190 | #if defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 191 | #pragma warning(pop) |
tgw | 0:2ee762ea11b3 | 192 | #endif |
tgw | 0:2ee762ea11b3 | 193 | |
tgw | 0:2ee762ea11b3 | 194 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
tgw | 0:2ee762ea11b3 | 195 | *human friendly way. |
tgw | 0:2ee762ea11b3 | 196 | * |
tgw | 0:2ee762ea11b3 | 197 | * The rules for line break and indent are as follow: |
tgw | 0:2ee762ea11b3 | 198 | * - Object value: |
tgw | 0:2ee762ea11b3 | 199 | * - if empty then print {} without indent and line break |
tgw | 0:2ee762ea11b3 | 200 | * - if not empty the print '{', line break & indent, print one value per |
tgw | 0:2ee762ea11b3 | 201 | *line |
tgw | 0:2ee762ea11b3 | 202 | * and then unindent and line break and print '}'. |
tgw | 0:2ee762ea11b3 | 203 | * - Array value: |
tgw | 0:2ee762ea11b3 | 204 | * - if empty then print [] without indent and line break |
tgw | 0:2ee762ea11b3 | 205 | * - if the array contains no object value, empty array or some other value |
tgw | 0:2ee762ea11b3 | 206 | *types, |
tgw | 0:2ee762ea11b3 | 207 | * and all the values fit on one lines, then print the array on a single |
tgw | 0:2ee762ea11b3 | 208 | *line. |
tgw | 0:2ee762ea11b3 | 209 | * - otherwise, it the values do not fit on one line, or the array contains |
tgw | 0:2ee762ea11b3 | 210 | * object or non empty array, then print one value per line. |
tgw | 0:2ee762ea11b3 | 211 | * |
tgw | 0:2ee762ea11b3 | 212 | * If the Value have comments then they are outputed according to their |
tgw | 0:2ee762ea11b3 | 213 | *#CommentPlacement. |
tgw | 0:2ee762ea11b3 | 214 | * |
tgw | 0:2ee762ea11b3 | 215 | * \sa Reader, Value, Value::setComment() |
tgw | 0:2ee762ea11b3 | 216 | * \deprecated Use StreamWriterBuilder. |
tgw | 0:2ee762ea11b3 | 217 | */ |
tgw | 0:2ee762ea11b3 | 218 | #if defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 219 | #pragma warning(push) |
tgw | 0:2ee762ea11b3 | 220 | #pragma warning(disable:4996) // Deriving from deprecated class |
tgw | 0:2ee762ea11b3 | 221 | #endif |
tgw | 0:2ee762ea11b3 | 222 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer { |
tgw | 0:2ee762ea11b3 | 223 | public: |
tgw | 0:2ee762ea11b3 | 224 | StyledWriter(); |
tgw | 0:2ee762ea11b3 | 225 | ~StyledWriter() JSONCPP_OVERRIDE {} |
tgw | 0:2ee762ea11b3 | 226 | |
tgw | 0:2ee762ea11b3 | 227 | public: // overridden from Writer |
tgw | 0:2ee762ea11b3 | 228 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
tgw | 0:2ee762ea11b3 | 229 | * \param root Value to serialize. |
tgw | 0:2ee762ea11b3 | 230 | * \return String containing the JSON document that represents the root value. |
tgw | 0:2ee762ea11b3 | 231 | */ |
tgw | 0:2ee762ea11b3 | 232 | JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; |
tgw | 0:2ee762ea11b3 | 233 | |
tgw | 0:2ee762ea11b3 | 234 | private: |
tgw | 0:2ee762ea11b3 | 235 | void writeValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 236 | void writeArrayValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 237 | bool isMultilineArray(const Value& value); |
tgw | 0:2ee762ea11b3 | 238 | void pushValue(const JSONCPP_STRING& value); |
tgw | 0:2ee762ea11b3 | 239 | void writeIndent(); |
tgw | 0:2ee762ea11b3 | 240 | void writeWithIndent(const JSONCPP_STRING& value); |
tgw | 0:2ee762ea11b3 | 241 | void indent(); |
tgw | 0:2ee762ea11b3 | 242 | void unindent(); |
tgw | 0:2ee762ea11b3 | 243 | void writeCommentBeforeValue(const Value& root); |
tgw | 0:2ee762ea11b3 | 244 | void writeCommentAfterValueOnSameLine(const Value& root); |
tgw | 0:2ee762ea11b3 | 245 | bool hasCommentForValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 246 | static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); |
tgw | 0:2ee762ea11b3 | 247 | |
tgw | 0:2ee762ea11b3 | 248 | typedef std::vector<JSONCPP_STRING> ChildValues; |
tgw | 0:2ee762ea11b3 | 249 | |
tgw | 0:2ee762ea11b3 | 250 | ChildValues childValues_; |
tgw | 0:2ee762ea11b3 | 251 | JSONCPP_STRING document_; |
tgw | 0:2ee762ea11b3 | 252 | JSONCPP_STRING indentString_; |
tgw | 0:2ee762ea11b3 | 253 | unsigned int rightMargin_; |
tgw | 0:2ee762ea11b3 | 254 | unsigned int indentSize_; |
tgw | 0:2ee762ea11b3 | 255 | bool addChildValues_; |
tgw | 0:2ee762ea11b3 | 256 | }; |
tgw | 0:2ee762ea11b3 | 257 | #if defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 258 | #pragma warning(pop) |
tgw | 0:2ee762ea11b3 | 259 | #endif |
tgw | 0:2ee762ea11b3 | 260 | |
tgw | 0:2ee762ea11b3 | 261 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
tgw | 0:2ee762ea11b3 | 262 | human friendly way, |
tgw | 0:2ee762ea11b3 | 263 | to a stream rather than to a string. |
tgw | 0:2ee762ea11b3 | 264 | * |
tgw | 0:2ee762ea11b3 | 265 | * The rules for line break and indent are as follow: |
tgw | 0:2ee762ea11b3 | 266 | * - Object value: |
tgw | 0:2ee762ea11b3 | 267 | * - if empty then print {} without indent and line break |
tgw | 0:2ee762ea11b3 | 268 | * - if not empty the print '{', line break & indent, print one value per |
tgw | 0:2ee762ea11b3 | 269 | line |
tgw | 0:2ee762ea11b3 | 270 | * and then unindent and line break and print '}'. |
tgw | 0:2ee762ea11b3 | 271 | * - Array value: |
tgw | 0:2ee762ea11b3 | 272 | * - if empty then print [] without indent and line break |
tgw | 0:2ee762ea11b3 | 273 | * - if the array contains no object value, empty array or some other value |
tgw | 0:2ee762ea11b3 | 274 | types, |
tgw | 0:2ee762ea11b3 | 275 | * and all the values fit on one lines, then print the array on a single |
tgw | 0:2ee762ea11b3 | 276 | line. |
tgw | 0:2ee762ea11b3 | 277 | * - otherwise, it the values do not fit on one line, or the array contains |
tgw | 0:2ee762ea11b3 | 278 | * object or non empty array, then print one value per line. |
tgw | 0:2ee762ea11b3 | 279 | * |
tgw | 0:2ee762ea11b3 | 280 | * If the Value have comments then they are outputed according to their |
tgw | 0:2ee762ea11b3 | 281 | #CommentPlacement. |
tgw | 0:2ee762ea11b3 | 282 | * |
tgw | 0:2ee762ea11b3 | 283 | * \sa Reader, Value, Value::setComment() |
tgw | 0:2ee762ea11b3 | 284 | * \deprecated Use StreamWriterBuilder. |
tgw | 0:2ee762ea11b3 | 285 | */ |
tgw | 0:2ee762ea11b3 | 286 | #if defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 287 | #pragma warning(push) |
tgw | 0:2ee762ea11b3 | 288 | #pragma warning(disable:4996) // Deriving from deprecated class |
tgw | 0:2ee762ea11b3 | 289 | #endif |
tgw | 0:2ee762ea11b3 | 290 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledStreamWriter { |
tgw | 0:2ee762ea11b3 | 291 | public: |
tgw | 0:2ee762ea11b3 | 292 | /** |
tgw | 0:2ee762ea11b3 | 293 | * \param indentation Each level will be indented by this amount extra. |
tgw | 0:2ee762ea11b3 | 294 | */ |
tgw | 0:2ee762ea11b3 | 295 | StyledStreamWriter(JSONCPP_STRING indentation = "\t"); |
tgw | 0:2ee762ea11b3 | 296 | ~StyledStreamWriter() {} |
tgw | 0:2ee762ea11b3 | 297 | |
tgw | 0:2ee762ea11b3 | 298 | public: |
tgw | 0:2ee762ea11b3 | 299 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
tgw | 0:2ee762ea11b3 | 300 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
tgw | 0:2ee762ea11b3 | 301 | * \param root Value to serialize. |
tgw | 0:2ee762ea11b3 | 302 | * \note There is no point in deriving from Writer, since write() should not |
tgw | 0:2ee762ea11b3 | 303 | * return a value. |
tgw | 0:2ee762ea11b3 | 304 | */ |
tgw | 0:2ee762ea11b3 | 305 | void write(JSONCPP_OSTREAM& out, const Value& root); |
tgw | 0:2ee762ea11b3 | 306 | |
tgw | 0:2ee762ea11b3 | 307 | private: |
tgw | 0:2ee762ea11b3 | 308 | void writeValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 309 | void writeArrayValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 310 | bool isMultilineArray(const Value& value); |
tgw | 0:2ee762ea11b3 | 311 | void pushValue(const JSONCPP_STRING& value); |
tgw | 0:2ee762ea11b3 | 312 | void writeIndent(); |
tgw | 0:2ee762ea11b3 | 313 | void writeWithIndent(const JSONCPP_STRING& value); |
tgw | 0:2ee762ea11b3 | 314 | void indent(); |
tgw | 0:2ee762ea11b3 | 315 | void unindent(); |
tgw | 0:2ee762ea11b3 | 316 | void writeCommentBeforeValue(const Value& root); |
tgw | 0:2ee762ea11b3 | 317 | void writeCommentAfterValueOnSameLine(const Value& root); |
tgw | 0:2ee762ea11b3 | 318 | bool hasCommentForValue(const Value& value); |
tgw | 0:2ee762ea11b3 | 319 | static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); |
tgw | 0:2ee762ea11b3 | 320 | |
tgw | 0:2ee762ea11b3 | 321 | typedef std::vector<JSONCPP_STRING> ChildValues; |
tgw | 0:2ee762ea11b3 | 322 | |
tgw | 0:2ee762ea11b3 | 323 | ChildValues childValues_; |
tgw | 0:2ee762ea11b3 | 324 | JSONCPP_OSTREAM* document_; |
tgw | 0:2ee762ea11b3 | 325 | JSONCPP_STRING indentString_; |
tgw | 0:2ee762ea11b3 | 326 | unsigned int rightMargin_; |
tgw | 0:2ee762ea11b3 | 327 | JSONCPP_STRING indentation_; |
tgw | 0:2ee762ea11b3 | 328 | bool addChildValues_ : 1; |
tgw | 0:2ee762ea11b3 | 329 | bool indented_ : 1; |
tgw | 0:2ee762ea11b3 | 330 | }; |
tgw | 0:2ee762ea11b3 | 331 | #if defined(_MSC_VER) |
tgw | 0:2ee762ea11b3 | 332 | #pragma warning(pop) |
tgw | 0:2ee762ea11b3 | 333 | #endif |
tgw | 0:2ee762ea11b3 | 334 | |
tgw | 0:2ee762ea11b3 | 335 | #if defined(JSON_HAS_INT64) |
tgw | 0:2ee762ea11b3 | 336 | JSONCPP_STRING JSON_API valueToString(Int value); |
tgw | 0:2ee762ea11b3 | 337 | JSONCPP_STRING JSON_API valueToString(UInt value); |
tgw | 0:2ee762ea11b3 | 338 | #endif // if defined(JSON_HAS_INT64) |
tgw | 0:2ee762ea11b3 | 339 | JSONCPP_STRING JSON_API valueToString(LargestInt value); |
tgw | 0:2ee762ea11b3 | 340 | JSONCPP_STRING JSON_API valueToString(LargestUInt value); |
tgw | 0:2ee762ea11b3 | 341 | JSONCPP_STRING JSON_API valueToString(double value); |
tgw | 0:2ee762ea11b3 | 342 | JSONCPP_STRING JSON_API valueToString(bool value); |
tgw | 0:2ee762ea11b3 | 343 | JSONCPP_STRING JSON_API valueToQuotedString(const char* value); |
tgw | 0:2ee762ea11b3 | 344 | |
tgw | 0:2ee762ea11b3 | 345 | /// \brief Output using the StyledStreamWriter. |
tgw | 0:2ee762ea11b3 | 346 | /// \see Json::operator>>() |
tgw | 0:2ee762ea11b3 | 347 | JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root); |
tgw | 0:2ee762ea11b3 | 348 | |
tgw | 0:2ee762ea11b3 | 349 | } // namespace Json |
tgw | 0:2ee762ea11b3 | 350 | |
tgw | 0:2ee762ea11b3 | 351 | #pragma pack(pop) |
tgw | 0:2ee762ea11b3 | 352 | |
tgw | 0:2ee762ea11b3 | 353 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
tgw | 0:2ee762ea11b3 | 354 | #pragma warning(pop) |
tgw | 0:2ee762ea11b3 | 355 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
tgw | 0:2ee762ea11b3 | 356 | |
tgw | 0:2ee762ea11b3 | 357 | #endif // JSON_WRITER_H_INCLUDED |