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