Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur |
| 2 | // Distributed under MIT license, or public domain if desired and |
| 3 | // recognized in your jurisdiction. |
| 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 6 | #ifndef JSON_WRITER_H_INCLUDED |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 7 | #define JSON_WRITER_H_INCLUDED |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 8 | |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 9 | #if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 10 | #include "value.h" |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 11 | #endif // if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 12 | #include <vector> |
| 13 | #include <string> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 14 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 15 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 16 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 17 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 18 | #pragma warning(push) |
| 19 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 20 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 21 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 22 | namespace Json { |
| 23 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 24 | class Value; |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame^] | 25 | class StreamWriterBuilder; |
| 26 | |
| 27 | /** |
| 28 | |
| 29 | Usage: |
| 30 | |
| 31 | using namespace Json; |
| 32 | Value value; |
| 33 | StreamWriterBuilderFactory f; |
| 34 | StreamWriter::Builder builder(&f); |
| 35 | builder.setCommentStyle(StreamWriter::CommentStyle::None); |
| 36 | std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&std::cout)); |
| 37 | writer.write(value); |
| 38 | */ |
| 39 | class JSON_API StreamWriterBuilderFactory { |
| 40 | public: |
| 41 | virtual ~StreamWriterBuilderFactory(); |
| 42 | virtual StreamWriterBuilder* newStreamWriterBuilder() const; |
| 43 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 44 | |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 45 | class JSON_API StreamWriter { |
| 46 | protected: |
| 47 | std::ostream& sout_; // not owned; will not delete |
| 48 | public: |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame^] | 49 | enum class CommentStyle {None, Some, All}; |
| 50 | |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 51 | StreamWriter(std::ostream* sout); |
| 52 | virtual ~StreamWriter(); |
| 53 | /// Write Value into document as configured in sub-class. |
| 54 | /// \return zero on success |
| 55 | /// \throw std::exception possibly, depending on configuration |
| 56 | virtual int write(Value const& root) const = 0; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 57 | |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame^] | 58 | /// Because this Builder is non-virtual, we can safely add |
| 59 | /// methods without a major version bump. |
| 60 | /// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility |
| 61 | class Builder { |
| 62 | StreamWriterBuilder* own_; |
| 63 | public: |
| 64 | Builder(StreamWriterBuilderFactory const*); |
| 65 | ~Builder(); // delete underlying StreamWriterBuilder |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 66 | |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame^] | 67 | void setCommentStyle(CommentStyle cs); /// default: All |
| 68 | |
| 69 | /// Do not take ownership of sout, but maintain a reference. |
| 70 | StreamWriter* newStreamWriter(std::ostream* sout); |
| 71 | }; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /// \brief Write into stringstream, then return string, for convenience. |
| 75 | std::string writeString(Value const& root, StreamWriterBuilder const& builder); |
| 76 | |
| 77 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 78 | /** \brief Abstract class for writers. |
| 79 | */ |
| 80 | class JSON_API Writer { |
| 81 | public: |
| 82 | virtual ~Writer(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 83 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 84 | virtual std::string write(const Value& root) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 85 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 86 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 87 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
| 88 | *without formatting (not human friendly). |
| 89 | * |
| 90 | * The JSON document is written in a single line. It is not intended for 'human' |
| 91 | *consumption, |
| 92 | * but may be usefull to support feature such as RPC where bandwith is limited. |
| 93 | * \sa Reader, Value |
| 94 | */ |
| 95 | class JSON_API FastWriter : public Writer { |
| 96 | public: |
| 97 | FastWriter(); |
| 98 | virtual ~FastWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 99 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 100 | void enableYAMLCompatibility(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 101 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 102 | /** \brief Drop the "null" string from the writer's output for nullValues. |
| 103 | * Strictly speaking, this is not valid JSON. But when the output is being |
| 104 | * fed to a browser's Javascript, it makes for smaller output and the |
| 105 | * browser can handle the output just fine. |
| 106 | */ |
| 107 | void dropNullPlaceholders(); |
Aaron Jacobs | ae3c7a7 | 2012-03-12 04:53:57 +0000 | [diff] [blame] | 108 | |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 109 | void omitEndingLineFeed(); |
| 110 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 111 | public: // overridden from Writer |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 112 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 113 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 114 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 115 | void writeValue(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 116 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 117 | std::string document_; |
| 118 | bool yamlCompatiblityEnabled_; |
| 119 | bool dropNullPlaceholders_; |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 120 | bool omitEndingLineFeed_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 121 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 122 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 123 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 124 | *human friendly way. |
| 125 | * |
| 126 | * The rules for line break and indent are as follow: |
| 127 | * - Object value: |
| 128 | * - if empty then print {} without indent and line break |
| 129 | * - if not empty the print '{', line break & indent, print one value per |
| 130 | *line |
| 131 | * and then unindent and line break and print '}'. |
| 132 | * - Array value: |
| 133 | * - if empty then print [] without indent and line break |
| 134 | * - if the array contains no object value, empty array or some other value |
| 135 | *types, |
| 136 | * and all the values fit on one lines, then print the array on a single |
| 137 | *line. |
| 138 | * - otherwise, it the values do not fit on one line, or the array contains |
| 139 | * object or non empty array, then print one value per line. |
| 140 | * |
| 141 | * If the Value have comments then they are outputed according to their |
| 142 | *#CommentPlacement. |
| 143 | * |
| 144 | * \sa Reader, Value, Value::setComment() |
| 145 | */ |
| 146 | class JSON_API StyledWriter : public Writer { |
| 147 | public: |
| 148 | StyledWriter(); |
| 149 | virtual ~StyledWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 150 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 151 | public: // overridden from Writer |
| 152 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 153 | * \param root Value to serialize. |
| 154 | * \return String containing the JSON document that represents the root value. |
| 155 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 156 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 157 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 158 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 159 | void writeValue(const Value& value); |
| 160 | void writeArrayValue(const Value& value); |
| 161 | bool isMultineArray(const Value& value); |
| 162 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 163 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 164 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 165 | void indent(); |
| 166 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 167 | void writeCommentBeforeValue(const Value& root); |
| 168 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 169 | bool hasCommentForValue(const Value& value); |
| 170 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 171 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 172 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 173 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 174 | ChildValues childValues_; |
| 175 | std::string document_; |
| 176 | std::string indentString_; |
| 177 | int rightMargin_; |
| 178 | int indentSize_; |
| 179 | bool addChildValues_; |
| 180 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 181 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 182 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 183 | human friendly way, |
| 184 | to a stream rather than to a string. |
| 185 | * |
| 186 | * The rules for line break and indent are as follow: |
| 187 | * - Object value: |
| 188 | * - if empty then print {} without indent and line break |
| 189 | * - if not empty the print '{', line break & indent, print one value per |
| 190 | line |
| 191 | * and then unindent and line break and print '}'. |
| 192 | * - Array value: |
| 193 | * - if empty then print [] without indent and line break |
| 194 | * - if the array contains no object value, empty array or some other value |
| 195 | types, |
| 196 | * and all the values fit on one lines, then print the array on a single |
| 197 | line. |
| 198 | * - otherwise, it the values do not fit on one line, or the array contains |
| 199 | * object or non empty array, then print one value per line. |
| 200 | * |
| 201 | * If the Value have comments then they are outputed according to their |
| 202 | #CommentPlacement. |
| 203 | * |
| 204 | * \param indentation Each level will be indented by this amount extra. |
| 205 | * \sa Reader, Value, Value::setComment() |
| 206 | */ |
| 207 | class JSON_API StyledStreamWriter { |
| 208 | public: |
| 209 | StyledStreamWriter(std::string indentation = "\t"); |
| 210 | ~StyledStreamWriter() {} |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 211 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 212 | public: |
| 213 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 214 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
| 215 | * \param root Value to serialize. |
| 216 | * \note There is no point in deriving from Writer, since write() should not |
| 217 | * return a value. |
| 218 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 219 | void write(std::ostream& out, const Value& root); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 220 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 221 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 222 | void writeValue(const Value& value); |
| 223 | void writeArrayValue(const Value& value); |
| 224 | bool isMultineArray(const Value& value); |
| 225 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 226 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 227 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 228 | void indent(); |
| 229 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 230 | void writeCommentBeforeValue(const Value& root); |
| 231 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 232 | bool hasCommentForValue(const Value& value); |
| 233 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 234 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 235 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 236 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 237 | ChildValues childValues_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 238 | std::ostream* document_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 239 | std::string indentString_; |
| 240 | int rightMargin_; |
| 241 | std::string indentation_; |
Christopher Dunn | d383056 | 2015-01-23 13:09:43 -0600 | [diff] [blame] | 242 | bool addChildValues_ : 1; |
| 243 | bool indented_ : 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 244 | }; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 245 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 246 | #if defined(JSON_HAS_INT64) |
| 247 | std::string JSON_API valueToString(Int value); |
| 248 | std::string JSON_API valueToString(UInt value); |
| 249 | #endif // if defined(JSON_HAS_INT64) |
| 250 | std::string JSON_API valueToString(LargestInt value); |
| 251 | std::string JSON_API valueToString(LargestUInt value); |
| 252 | std::string JSON_API valueToString(double value); |
| 253 | std::string JSON_API valueToString(bool value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 254 | std::string JSON_API valueToQuotedString(const char* value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 255 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 256 | /// \brief Output using the StyledStreamWriter. |
| 257 | /// \see Json::operator>>() |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 258 | JSON_API std::ostream& operator<<(std::ostream&, const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 259 | |
| 260 | } // namespace Json |
| 261 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 262 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 263 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 264 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 265 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 266 | #endif // JSON_WRITER_H_INCLUDED |