diff options
author | Brad King <brad.king@kitware.com> | 2017-08-28 15:33:06 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2017-08-30 14:16:59 (GMT) |
commit | e09819557f5bf29f8414d5165e2c3e8ad8dee2d1 (patch) | |
tree | a12c94c8890665c2c45d7a859f897efe8931b4ad /Utilities/cmjsoncpp/include/json/writer.h | |
parent | a7241206bf4e1cb6ebbb24a05334cf4b9e2328df (diff) | |
parent | 6e9ef8d1b18f8a33ab97483437da819e342a9997 (diff) | |
download | CMake-e09819557f5bf29f8414d5165e2c3e8ad8dee2d1.zip CMake-e09819557f5bf29f8414d5165e2c3e8ad8dee2d1.tar.gz CMake-e09819557f5bf29f8414d5165e2c3e8ad8dee2d1.tar.bz2 |
Merge branch 'upstream-jsoncpp' into update-jsoncpp
* upstream-jsoncpp:
jsoncpp 2017-08-27 (4cfae897)
Diffstat (limited to 'Utilities/cmjsoncpp/include/json/writer.h')
-rw-r--r-- | Utilities/cmjsoncpp/include/json/writer.h | 201 |
1 files changed, 163 insertions, 38 deletions
diff --git a/Utilities/cmjsoncpp/include/json/writer.h b/Utilities/cmjsoncpp/include/json/writer.h index 10863b0..ee83ae0 100644 --- a/Utilities/cmjsoncpp/include/json/writer.h +++ b/Utilities/cmjsoncpp/include/json/writer.h @@ -1,4 +1,4 @@ -// Copyright 2007-2010 Baptiste Lepilleur +// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE @@ -12,6 +12,7 @@ #include <iosfwd> #include <vector> #include <string> +#include <ostream> // Disable warning C4251: <data member>: <type> needs to have dll-interface to // be used by... @@ -20,17 +21,131 @@ #pragma warning(disable : 4251) #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) +#pragma pack(push, 8) + namespace Json { class Value; +/** + +Usage: +\code + using namespace Json; + void writeToStdout(StreamWriter::Factory const& factory, Value const& value) { + std::unique_ptr<StreamWriter> const writer( + factory.newStreamWriter()); + writer->write(value, &std::cout); + std::cout << std::endl; // add lf and flush + } +\endcode +*/ +class JSON_API StreamWriter { +protected: + JSONCPP_OSTREAM* sout_; // not owned; will not delete +public: + StreamWriter(); + virtual ~StreamWriter(); + /** Write Value into document as configured in sub-class. + Do not take ownership of sout, but maintain a reference during function. + \pre sout != NULL + \return zero on success (For now, we always return zero, so check the stream instead.) + \throw std::exception possibly, depending on configuration + */ + virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0; + + /** \brief A simple abstract factory. + */ + class JSON_API Factory { + public: + virtual ~Factory(); + /** \brief Allocate a CharReader via operator new(). + * \throw std::exception if something goes wrong (e.g. invalid settings) + */ + virtual StreamWriter* newStreamWriter() const = 0; + }; // Factory +}; // StreamWriter + +/** \brief Write into stringstream, then return string, for convenience. + * A StreamWriter will be created from the factory, used, and then deleted. + */ +JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root); + + +/** \brief Build a StreamWriter implementation. + +Usage: +\code + using namespace Json; + Value value = ...; + StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = " "; // or whatever you like + std::unique_ptr<Json::StreamWriter> writer( + builder.newStreamWriter()); + writer->write(value, &std::cout); + std::cout << std::endl; // add lf and flush +\endcode +*/ +class JSON_API StreamWriterBuilder : public StreamWriter::Factory { +public: + // Note: We use a Json::Value so that we can add data-members to this class + // without a major version bump. + /** Configuration of this builder. + Available settings (case-sensitive): + - "commentStyle": "None" or "All" + - "indentation": "<anything>" + - "enableYAMLCompatibility": false or true + - slightly change the whitespace around colons + - "dropNullPlaceholders": false or true + - Drop the "null" string from the writer's output for nullValues. + Strictly speaking, this is not valid JSON. But when the output is being + fed to a browser's Javascript, it makes for smaller output and the + browser can handle the output just fine. + - "useSpecialFloats": false or true + - If true, outputs non-finite floating point values in the following way: + NaN values as "NaN", positive infinity as "Infinity", and negative infinity + as "-Infinity". + + You can examine 'settings_` yourself + to see the defaults. You can also write and read them just like any + JSON Value. + \sa setDefaults() + */ + Json::Value settings_; + + StreamWriterBuilder(); + ~StreamWriterBuilder() JSONCPP_OVERRIDE; + + /** + * \throw std::exception if something goes wrong (e.g. invalid settings) + */ + StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE; + + /** \return true if 'settings' are legal and consistent; + * otherwise, indicate bad settings via 'invalid'. + */ + bool validate(Json::Value* invalid) const; + /** A simple way to update a specific setting. + */ + Value& operator[](JSONCPP_STRING key); + + /** Called by ctor, but you can use this to reset settings_. + * \pre 'settings' != NULL (but Json::null is fine) + * \remark Defaults: + * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults + */ + static void setDefaults(Json::Value* settings); +}; + /** \brief Abstract class for writers. + * \deprecated Use StreamWriter. (And really, this is an implementation detail.) */ -class JSON_API Writer { +class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer { public: virtual ~Writer(); - virtual std::string write(const Value& root) = 0; + virtual JSONCPP_STRING write(const Value& root) = 0; }; /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format @@ -40,11 +155,13 @@ public: *consumption, * but may be usefull to support feature such as RPC where bandwith is limited. * \sa Reader, Value + * \deprecated Use StreamWriterBuilder. */ -class JSON_API FastWriter : public Writer { +class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer { + public: FastWriter(); - virtual ~FastWriter() {} + ~FastWriter() JSONCPP_OVERRIDE {} void enableYAMLCompatibility(); @@ -58,12 +175,12 @@ public: void omitEndingLineFeed(); public: // overridden from Writer - virtual std::string write(const Value& root); + JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; private: void writeValue(const Value& value); - std::string document_; + JSONCPP_STRING document_; bool yamlCompatiblityEnabled_; bool dropNullPlaceholders_; bool omitEndingLineFeed_; @@ -91,40 +208,41 @@ private: *#CommentPlacement. * * \sa Reader, Value, Value::setComment() + * \deprecated Use StreamWriterBuilder. */ -class JSON_API StyledWriter : public Writer { +class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer { public: StyledWriter(); - virtual ~StyledWriter() {} + ~StyledWriter() JSONCPP_OVERRIDE {} public: // overridden from Writer /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. * \param root Value to serialize. * \return String containing the JSON document that represents the root value. */ - virtual std::string write(const Value& root); + JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; private: void writeValue(const Value& value); void writeArrayValue(const Value& value); bool isMultineArray(const Value& value); - void pushValue(const std::string& value); + void pushValue(const JSONCPP_STRING& value); void writeIndent(); - void writeWithIndent(const std::string& value); + void writeWithIndent(const JSONCPP_STRING& value); void indent(); void unindent(); void writeCommentBeforeValue(const Value& root); void writeCommentAfterValueOnSameLine(const Value& root); bool hasCommentForValue(const Value& value); - static std::string normalizeEOL(const std::string& text); + static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); - typedef std::vector<std::string> ChildValues; + typedef std::vector<JSONCPP_STRING> ChildValues; ChildValues childValues_; - std::string document_; - std::string indentString_; - int rightMargin_; - int indentSize_; + JSONCPP_STRING document_; + JSONCPP_STRING indentString_; + unsigned int rightMargin_; + unsigned int indentSize_; bool addChildValues_; }; @@ -151,10 +269,14 @@ private: #CommentPlacement. * * \sa Reader, Value, Value::setComment() + * \deprecated Use StreamWriterBuilder. */ -class JSON_API StyledStreamWriter { +class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledStreamWriter { public: - StyledStreamWriter(std::string indentation = "\t"); +/** + * \param indentation Each level will be indented by this amount extra. + */ + StyledStreamWriter(JSONCPP_STRING indentation = "\t"); ~StyledStreamWriter() {} public: @@ -164,48 +286,51 @@ public: * \note There is no point in deriving from Writer, since write() should not * return a value. */ - void write(std::ostream& out, const Value& root); + void write(JSONCPP_OSTREAM& out, const Value& root); private: void writeValue(const Value& value); void writeArrayValue(const Value& value); bool isMultineArray(const Value& value); - void pushValue(const std::string& value); + void pushValue(const JSONCPP_STRING& value); void writeIndent(); - void writeWithIndent(const std::string& value); + void writeWithIndent(const JSONCPP_STRING& value); void indent(); void unindent(); void writeCommentBeforeValue(const Value& root); void writeCommentAfterValueOnSameLine(const Value& root); bool hasCommentForValue(const Value& value); - static std::string normalizeEOL(const std::string& text); + static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); - typedef std::vector<std::string> ChildValues; + typedef std::vector<JSONCPP_STRING> ChildValues; ChildValues childValues_; - std::ostream* document_; - std::string indentString_; - int rightMargin_; - std::string indentation_; - bool addChildValues_; + JSONCPP_OSTREAM* document_; + JSONCPP_STRING indentString_; + unsigned int rightMargin_; + JSONCPP_STRING indentation_; + bool addChildValues_ : 1; + bool indented_ : 1; }; #if defined(JSON_HAS_INT64) -std::string JSON_API valueToString(Int value); -std::string JSON_API valueToString(UInt value); +JSONCPP_STRING JSON_API valueToString(Int value); +JSONCPP_STRING JSON_API valueToString(UInt value); #endif // if defined(JSON_HAS_INT64) -std::string JSON_API valueToString(LargestInt value); -std::string JSON_API valueToString(LargestUInt value); -std::string JSON_API valueToString(double value); -std::string JSON_API valueToString(bool value); -std::string JSON_API valueToQuotedString(const char* value); +JSONCPP_STRING JSON_API valueToString(LargestInt value); +JSONCPP_STRING JSON_API valueToString(LargestUInt value); +JSONCPP_STRING JSON_API valueToString(double value); +JSONCPP_STRING JSON_API valueToString(bool value); +JSONCPP_STRING JSON_API valueToQuotedString(const char* value); /// \brief Output using the StyledStreamWriter. /// \see Json::operator>>() -JSON_API std::ostream& operator<<(std::ostream&, const Value& root); +JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root); } // namespace Json +#pragma pack(pop) + #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) #pragma warning(pop) #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |