Path: blob/master/thirdparty/openxr/src/external/jsoncpp/include/json/writer.h
9913 views
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors1// Distributed under MIT license, or public domain if desired and2// recognized in your jurisdiction.3// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE45#ifndef JSON_WRITER_H_INCLUDED6#define JSON_WRITER_H_INCLUDED78#if !defined(JSON_IS_AMALGAMATION)9#include "value.h"10#endif // if !defined(JSON_IS_AMALGAMATION)11#include <ostream>12#include <string>13#include <vector>1415// Disable warning C4251: <data member>: <type> needs to have dll-interface to16// be used by...17#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER)18#pragma warning(push)19#pragma warning(disable : 4251)20#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)2122#pragma pack(push)23#pragma pack()2425namespace Json {2627class Value;2829/**30*31* Usage:32* \code33* using namespace Json;34* void writeToStdout(StreamWriter::Factory const& factory, Value const& value)35* { std::unique_ptr<StreamWriter> const writer( factory.newStreamWriter());36* writer->write(value, &std::cout);37* std::cout << std::endl; // add lf and flush38* }39* \endcode40*/41class JSON_API StreamWriter {42protected:43OStream* sout_; // not owned; will not delete44public:45StreamWriter();46virtual ~StreamWriter();47/** Write Value into document as configured in sub-class.48* Do not take ownership of sout, but maintain a reference during function.49* \pre sout != NULL50* \return zero on success (For now, we always return zero, so check the51* stream instead.) \throw std::exception possibly, depending on52* configuration53*/54virtual int write(Value const& root, OStream* sout) = 0;5556/** \brief A simple abstract factory.57*/58class JSON_API Factory {59public:60virtual ~Factory();61/** \brief Allocate a CharReader via operator new().62* \throw std::exception if something goes wrong (e.g. invalid settings)63*/64virtual StreamWriter* newStreamWriter() const = 0;65}; // Factory66}; // StreamWriter6768/** \brief Write into stringstream, then return string, for convenience.69* A StreamWriter will be created from the factory, used, and then deleted.70*/71String JSON_API writeString(StreamWriter::Factory const& factory,72Value const& root);7374/** \brief Build a StreamWriter implementation.7576* Usage:77* \code78* using namespace Json;79* Value value = ...;80* StreamWriterBuilder builder;81* builder["commentStyle"] = "None";82* builder["indentation"] = " "; // or whatever you like83* std::unique_ptr<Json::StreamWriter> writer(84* builder.newStreamWriter());85* writer->write(value, &std::cout);86* std::cout << std::endl; // add lf and flush87* \endcode88*/89class JSON_API StreamWriterBuilder : public StreamWriter::Factory {90public:91// Note: We use a Json::Value so that we can add data-members to this class92// without a major version bump.93/** Configuration of this builder.94* Available settings (case-sensitive):95* - "commentStyle": "None" or "All"96* - "indentation": "<anything>".97* - Setting this to an empty string also omits newline characters.98* - "enableYAMLCompatibility": false or true99* - slightly change the whitespace around colons100* - "dropNullPlaceholders": false or true101* - Drop the "null" string from the writer's output for nullValues.102* Strictly speaking, this is not valid JSON. But when the output is being103* fed to a browser's JavaScript, it makes for smaller output and the104* browser can handle the output just fine.105* - "useSpecialFloats": false or true106* - If true, outputs non-finite floating point values in the following way:107* NaN values as "NaN", positive infinity as "Infinity", and negative108* infinity as "-Infinity".109* - "precision": int110* - Number of precision digits for formatting of real values.111* - "precisionType": "significant"(default) or "decimal"112* - Type of precision for formatting of real values.113* - "emitUTF8": false or true114* - If true, outputs raw UTF8 strings instead of escaping them.115116* You can examine 'settings_` yourself117* to see the defaults. You can also write and read them just like any118* JSON Value.119* \sa setDefaults()120*/121Json::Value settings_;122123StreamWriterBuilder();124~StreamWriterBuilder() override;125126/**127* \throw std::exception if something goes wrong (e.g. invalid settings)128*/129StreamWriter* newStreamWriter() const override;130131/** \return true if 'settings' are legal and consistent;132* otherwise, indicate bad settings via 'invalid'.133*/134bool validate(Json::Value* invalid) const;135/** A simple way to update a specific setting.136*/137Value& operator[](const String& key);138139/** Called by ctor, but you can use this to reset settings_.140* \pre 'settings' != NULL (but Json::null is fine)141* \remark Defaults:142* \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults143*/144static void setDefaults(Json::Value* settings);145};146147/** \brief Abstract class for writers.148* \deprecated Use StreamWriter. (And really, this is an implementation detail.)149*/150class JSON_API Writer {151public:152virtual ~Writer();153154virtual String write(const Value& root) = 0;155};156157/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format158*without formatting (not human friendly).159*160* The JSON document is written in a single line. It is not intended for 'human'161*consumption,162* but may be useful to support feature such as RPC where bandwidth is limited.163* \sa Reader, Value164* \deprecated Use StreamWriterBuilder.165*/166#if defined(_MSC_VER)167#pragma warning(push)168#pragma warning(disable : 4996) // Deriving from deprecated class169#endif170class JSON_API FastWriter : public Writer {171public:172FastWriter();173~FastWriter() override = default;174175void enableYAMLCompatibility();176177/** \brief Drop the "null" string from the writer's output for nullValues.178* Strictly speaking, this is not valid JSON. But when the output is being179* fed to a browser's JavaScript, it makes for smaller output and the180* browser can handle the output just fine.181*/182void dropNullPlaceholders();183184void omitEndingLineFeed();185186public: // overridden from Writer187String write(const Value& root) override;188189private:190void writeValue(const Value& value);191192String document_;193bool yamlCompatibilityEnabled_{false};194bool dropNullPlaceholders_{false};195bool omitEndingLineFeed_{false};196};197#if defined(_MSC_VER)198#pragma warning(pop)199#endif200201/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a202*human friendly way.203*204* The rules for line break and indent are as follow:205* - Object value:206* - if empty then print {} without indent and line break207* - if not empty the print '{', line break & indent, print one value per208*line209* and then unindent and line break and print '}'.210* - Array value:211* - if empty then print [] without indent and line break212* - if the array contains no object value, empty array or some other value213*types,214* and all the values fit on one lines, then print the array on a single215*line.216* - otherwise, it the values do not fit on one line, or the array contains217* object or non empty array, then print one value per line.218*219* If the Value have comments then they are outputted according to their220*#CommentPlacement.221*222* \sa Reader, Value, Value::setComment()223* \deprecated Use StreamWriterBuilder.224*/225#if defined(_MSC_VER)226#pragma warning(push)227#pragma warning(disable : 4996) // Deriving from deprecated class228#endif229class JSON_API StyledWriter : public Writer {230public:231StyledWriter();232~StyledWriter() override = default;233234public: // overridden from Writer235/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.236* \param root Value to serialize.237* \return String containing the JSON document that represents the root value.238*/239String write(const Value& root) override;240241private:242void writeValue(const Value& value);243void writeArrayValue(const Value& value);244bool isMultilineArray(const Value& value);245void pushValue(const String& value);246void writeIndent();247void writeWithIndent(const String& value);248void indent();249void unindent();250void writeCommentBeforeValue(const Value& root);251void writeCommentAfterValueOnSameLine(const Value& root);252static bool hasCommentForValue(const Value& value);253static String normalizeEOL(const String& text);254255using ChildValues = std::vector<String>;256257ChildValues childValues_;258String document_;259String indentString_;260unsigned int rightMargin_{74};261unsigned int indentSize_{3};262bool addChildValues_{false};263};264#if defined(_MSC_VER)265#pragma warning(pop)266#endif267268/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a269human friendly way,270to a stream rather than to a string.271*272* The rules for line break and indent are as follow:273* - Object value:274* - if empty then print {} without indent and line break275* - if not empty the print '{', line break & indent, print one value per276line277* and then unindent and line break and print '}'.278* - Array value:279* - if empty then print [] without indent and line break280* - if the array contains no object value, empty array or some other value281types,282* and all the values fit on one lines, then print the array on a single283line.284* - otherwise, it the values do not fit on one line, or the array contains285* object or non empty array, then print one value per line.286*287* If the Value have comments then they are outputted according to their288#CommentPlacement.289*290* \sa Reader, Value, Value::setComment()291* \deprecated Use StreamWriterBuilder.292*/293#if defined(_MSC_VER)294#pragma warning(push)295#pragma warning(disable : 4996) // Deriving from deprecated class296#endif297class JSON_API StyledStreamWriter {298public:299/**300* \param indentation Each level will be indented by this amount extra.301*/302StyledStreamWriter(String indentation = "\t");303~StyledStreamWriter() = default;304305public:306/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.307* \param out Stream to write to. (Can be ostringstream, e.g.)308* \param root Value to serialize.309* \note There is no point in deriving from Writer, since write() should not310* return a value.311*/312void write(OStream& out, const Value& root);313314private:315void writeValue(const Value& value);316void writeArrayValue(const Value& value);317bool isMultilineArray(const Value& value);318void pushValue(const String& value);319void writeIndent();320void writeWithIndent(const String& value);321void indent();322void unindent();323void writeCommentBeforeValue(const Value& root);324void writeCommentAfterValueOnSameLine(const Value& root);325static bool hasCommentForValue(const Value& value);326static String normalizeEOL(const String& text);327328using ChildValues = std::vector<String>;329330ChildValues childValues_;331OStream* document_;332String indentString_;333unsigned int rightMargin_{74};334String indentation_;335bool addChildValues_ : 1;336bool indented_ : 1;337};338#if defined(_MSC_VER)339#pragma warning(pop)340#endif341342#if defined(JSON_HAS_INT64)343String JSON_API valueToString(Int value);344String JSON_API valueToString(UInt value);345#endif // if defined(JSON_HAS_INT64)346String JSON_API valueToString(LargestInt value);347String JSON_API valueToString(LargestUInt value);348String JSON_API valueToString(349double value, unsigned int precision = Value::defaultRealPrecision,350PrecisionType precisionType = PrecisionType::significantDigits);351String JSON_API valueToString(bool value);352String JSON_API valueToQuotedString(const char* value);353String JSON_API valueToQuotedString(const char* value, size_t length);354355/// \brief Output using the StyledStreamWriter.356/// \see Json::operator>>()357JSON_API OStream& operator<<(OStream&, const Value& root);358359} // namespace Json360361#pragma pack(pop)362363#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)364#pragma warning(pop)365#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)366367#endif // JSON_WRITER_H_INCLUDED368369370