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