2010-04-20 21:35:19 +00:00
|
|
|
// Copyright 2007-2010 Baptiste Lepilleur
|
|
|
|
// 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
|
|
|
|
|
2007-06-13 15:51:04 +00:00
|
|
|
#ifndef JSON_WRITER_H_INCLUDED
|
2014-07-01 08:48:54 +10:00
|
|
|
#define JSON_WRITER_H_INCLUDED
|
2007-06-13 15:51:04 +00:00
|
|
|
|
2011-05-02 21:09:30 +00:00
|
|
|
#if !defined(JSON_IS_AMALGAMATION)
|
2014-07-01 08:48:54 +10:00
|
|
|
#include "value.h"
|
2011-05-02 21:09:30 +00:00
|
|
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
2014-07-01 08:48:54 +10:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2015-01-26 10:35:54 -06:00
|
|
|
#include <ostream>
|
2007-06-13 15:51:04 +00:00
|
|
|
|
2014-07-01 08:48:54 +10:00
|
|
|
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
|
|
|
|
// be used by...
|
2013-05-08 20:21:11 +00:00
|
|
|
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
2014-07-01 08:48:54 +10:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4251)
|
2013-05-08 20:21:11 +00:00
|
|
|
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
|
|
|
|
2007-06-13 15:51:04 +00:00
|
|
|
namespace Json {
|
|
|
|
|
2014-07-01 08:48:54 +10:00
|
|
|
class Value;
|
2015-01-22 15:25:30 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
Usage:
|
2015-01-25 19:20:43 -06:00
|
|
|
\code
|
2015-01-22 15:25:30 -06:00
|
|
|
using namespace Json;
|
2015-01-26 11:12:53 -06:00
|
|
|
void writeToStdout(StreamWriter::Builder const& builder, Value const& value) {
|
|
|
|
std::unique_ptr<StreamWriter> const writer(
|
|
|
|
builder.newStreamWriter(&std::cout));
|
|
|
|
writer->write(value);
|
|
|
|
std::cout << std::endl; // add lf and flush
|
|
|
|
}
|
2015-01-25 19:20:43 -06:00
|
|
|
\endcode
|
2015-01-22 15:25:30 -06:00
|
|
|
*/
|
2015-01-22 14:31:32 -06:00
|
|
|
class JSON_API StreamWriter {
|
|
|
|
protected:
|
|
|
|
std::ostream& sout_; // not owned; will not delete
|
|
|
|
public:
|
2015-01-25 19:20:43 -06:00
|
|
|
/// Decide whether to write comments.
|
|
|
|
enum class CommentStyle {
|
|
|
|
None, ///< Drop all comments.
|
|
|
|
Most, ///< Recover odd behavior of previous versions (not implemented yet).
|
|
|
|
All ///< Keep all comments.
|
|
|
|
};
|
2015-01-22 15:25:30 -06:00
|
|
|
|
2015-01-24 13:54:28 -06:00
|
|
|
/// Keep a reference, but do not take ownership of `sout`.
|
2015-01-22 14:31:32 -06:00
|
|
|
StreamWriter(std::ostream* sout);
|
|
|
|
virtual ~StreamWriter();
|
|
|
|
/// Write Value into document as configured in sub-class.
|
|
|
|
/// \return zero on success
|
|
|
|
/// \throw std::exception possibly, depending on configuration
|
2015-01-23 07:51:40 -06:00
|
|
|
virtual int write(Value const& root) = 0;
|
2015-01-22 14:31:32 -06:00
|
|
|
|
2015-01-26 10:35:54 -06:00
|
|
|
/** \brief A simple abstract factory.
|
|
|
|
*/
|
|
|
|
class JSON_API Factory {
|
|
|
|
public:
|
|
|
|
virtual ~Factory();
|
|
|
|
/// Do not take ownership of sout, but maintain a reference.
|
|
|
|
virtual StreamWriter* newStreamWriter(std::ostream* sout) const = 0;
|
2015-01-26 11:01:15 -06:00
|
|
|
}; // Factory
|
|
|
|
}; // StreamWriter
|
|
|
|
|
|
|
|
/// \brief Write into stringstream, then return string, for convenience.
|
|
|
|
std::string writeString(Value const& root, StreamWriter::Factory const& factory);
|
|
|
|
|
2015-01-26 10:35:54 -06:00
|
|
|
|
2015-01-26 11:01:15 -06:00
|
|
|
/** \brief Build a StreamWriter implementation.
|
2015-01-26 11:12:53 -06:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
\code
|
|
|
|
using namespace Json;
|
|
|
|
Value value = ...;
|
|
|
|
StreamWriter::Builder builder;
|
|
|
|
builder.cs_ = StreamWriter::CommentStyle::None;
|
|
|
|
std::shared_ptr<StreamWriter> writer(
|
|
|
|
builder.newStreamWriter(&std::cout));
|
|
|
|
writer->write(value);
|
|
|
|
std::cout << std::endl; // add lf and flush
|
|
|
|
\endcode
|
|
|
|
*/
|
2015-01-26 11:01:15 -06:00
|
|
|
class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
|
|
|
|
public:
|
|
|
|
// Note: We cannot add data-members to this class without a major version bump.
|
|
|
|
// So these might as well be completely exposed.
|
|
|
|
|
|
|
|
/** \brief How to write comments.
|
|
|
|
* Default: All
|
2015-01-26 10:35:54 -06:00
|
|
|
*/
|
2015-01-26 11:17:42 -06:00
|
|
|
StreamWriter::CommentStyle cs_;
|
2015-01-26 11:01:15 -06:00
|
|
|
/** \brief Write in human-friendly style.
|
2015-01-26 10:35:54 -06:00
|
|
|
|
2015-01-26 11:01:15 -06:00
|
|
|
If "", then skip all indentation and newlines.
|
|
|
|
In that case, you probably want CommentStyle::None also.
|
|
|
|
Default: "\t"
|
|
|
|
*/
|
2015-01-26 11:17:42 -06:00
|
|
|
std::string indentation_;
|
|
|
|
|
|
|
|
StreamWriterBuilder();
|
2015-01-22 14:31:32 -06:00
|
|
|
|
2015-01-26 11:01:15 -06:00
|
|
|
/// Do not take ownership of sout, but maintain a reference.
|
|
|
|
StreamWriter* newStreamWriter(std::ostream* sout) const;
|
|
|
|
};
|
2015-01-22 14:31:32 -06:00
|
|
|
|
2015-01-26 10:35:54 -06:00
|
|
|
/** \brief Build a StreamWriter implementation.
|
|
|
|
* Comments are not written, and most whitespace is omitted.
|
|
|
|
* In addition, there are some special settings to allow compatibility
|
|
|
|
* with the old FastWriter.
|
|
|
|
* Usage:
|
|
|
|
* \code
|
|
|
|
* OldCompressingStreamWriterBuilder b;
|
|
|
|
* b.dropNullPlaceHolders_ = true; // etc.
|
|
|
|
* StreamWriter* w = b.newStreamWriter(&std::cout);
|
|
|
|
* w.write(value);
|
|
|
|
* delete w;
|
|
|
|
* \endcode
|
|
|
|
*/
|
2015-01-26 11:01:15 -06:00
|
|
|
class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
|
2015-01-26 10:35:54 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Note: We cannot add data-members to this class without a major version bump.
|
|
|
|
// So these might as well be completely exposed.
|
|
|
|
|
|
|
|
/** \brief 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.
|
|
|
|
*/
|
|
|
|
bool dropNullPlaceholders_;
|
|
|
|
/** \brief Do not add \n at end of document.
|
|
|
|
* Normally, we add an extra newline, just because.
|
|
|
|
*/
|
|
|
|
bool omitEndingLineFeed_;
|
|
|
|
/** \brief Add a space after ':'.
|
|
|
|
* If indentation is non-empty, we surround colon with whitespace,
|
|
|
|
* e.g. " : "
|
|
|
|
* This will add back the trailing space when there is no indentation.
|
|
|
|
* This seems dubious when the entire document is on a single line,
|
|
|
|
* but we leave this here to repduce the behavior of the old `FastWriter`.
|
|
|
|
*/
|
|
|
|
bool enableYAMLCompatibility_;
|
|
|
|
|
|
|
|
OldCompressingStreamWriterBuilder()
|
|
|
|
: dropNullPlaceholders_(false)
|
|
|
|
, omitEndingLineFeed_(false)
|
|
|
|
, enableYAMLCompatibility_(false)
|
|
|
|
{}
|
|
|
|
virtual StreamWriter* newStreamWriter(std::ostream*) const;
|
|
|
|
};
|
2015-01-22 14:31:32 -06:00
|
|
|
|
2014-07-01 08:48:54 +10:00
|
|
|
/** \brief Abstract class for writers.
|
2015-01-25 18:45:59 -06:00
|
|
|
* \deprecated Use StreamWriter::Builder.
|
2014-07-01 08:48:54 +10:00
|
|
|
*/
|
|
|
|
class JSON_API Writer {
|
|
|
|
public:
|
|
|
|
virtual ~Writer();
|
|
|
|
|
2014-09-15 10:15:29 +10:00
|
|
|
virtual std::string write(const Value& root) = 0;
|
2014-07-01 08:48:54 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
|
|
|
|
*without formatting (not human friendly).
|
|
|
|
*
|
|
|
|
* The JSON document is written in a single line. It is not intended for 'human'
|
|
|
|
*consumption,
|
|
|
|
* but may be usefull to support feature such as RPC where bandwith is limited.
|
|
|
|
* \sa Reader, Value
|
2015-01-25 18:45:59 -06:00
|
|
|
* \deprecated Use StreamWriter::Builder.
|
2014-07-01 08:48:54 +10:00
|
|
|
*/
|
|
|
|
class JSON_API FastWriter : public Writer {
|
|
|
|
public:
|
|
|
|
FastWriter();
|
|
|
|
virtual ~FastWriter() {}
|
|
|
|
|
|
|
|
void enableYAMLCompatibility();
|
|
|
|
|
|
|
|
/** \brief 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.
|
|
|
|
*/
|
|
|
|
void dropNullPlaceholders();
|
|
|
|
|
2014-09-02 17:09:07 -06:00
|
|
|
void omitEndingLineFeed();
|
|
|
|
|
2014-07-01 08:48:54 +10:00
|
|
|
public: // overridden from Writer
|
2014-09-15 10:15:29 +10:00
|
|
|
virtual std::string write(const Value& root);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
private:
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeValue(const Value& value);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
std::string document_;
|
|
|
|
bool yamlCompatiblityEnabled_;
|
|
|
|
bool dropNullPlaceholders_;
|
2014-09-02 17:09:07 -06:00
|
|
|
bool omitEndingLineFeed_;
|
2014-07-01 08:48:54 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
|
|
|
|
*human friendly way.
|
|
|
|
*
|
|
|
|
* The rules for line break and indent are as follow:
|
|
|
|
* - Object value:
|
|
|
|
* - if empty then print {} without indent and line break
|
|
|
|
* - if not empty the print '{', line break & indent, print one value per
|
|
|
|
*line
|
|
|
|
* and then unindent and line break and print '}'.
|
|
|
|
* - Array value:
|
|
|
|
* - if empty then print [] without indent and line break
|
|
|
|
* - if the array contains no object value, empty array or some other value
|
|
|
|
*types,
|
|
|
|
* and all the values fit on one lines, then print the array on a single
|
|
|
|
*line.
|
|
|
|
* - otherwise, it the values do not fit on one line, or the array contains
|
|
|
|
* object or non empty array, then print one value per line.
|
|
|
|
*
|
|
|
|
* If the Value have comments then they are outputed according to their
|
|
|
|
*#CommentPlacement.
|
|
|
|
*
|
|
|
|
* \sa Reader, Value, Value::setComment()
|
2015-01-25 18:45:59 -06:00
|
|
|
* \deprecated Use StreamWriter::Builder.
|
2014-07-01 08:48:54 +10:00
|
|
|
*/
|
|
|
|
class JSON_API StyledWriter : public Writer {
|
|
|
|
public:
|
|
|
|
StyledWriter();
|
|
|
|
virtual ~StyledWriter() {}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-09-15 10:15:29 +10:00
|
|
|
virtual std::string write(const Value& root);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
private:
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeValue(const Value& value);
|
|
|
|
void writeArrayValue(const Value& value);
|
|
|
|
bool isMultineArray(const Value& value);
|
|
|
|
void pushValue(const std::string& value);
|
2014-07-01 08:48:54 +10:00
|
|
|
void writeIndent();
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeWithIndent(const std::string& value);
|
2014-07-01 08:48:54 +10:00
|
|
|
void indent();
|
|
|
|
void unindent();
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeCommentBeforeValue(const Value& root);
|
|
|
|
void writeCommentAfterValueOnSameLine(const Value& root);
|
|
|
|
bool hasCommentForValue(const Value& value);
|
|
|
|
static std::string normalizeEOL(const std::string& text);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
typedef std::vector<std::string> ChildValues;
|
|
|
|
|
|
|
|
ChildValues childValues_;
|
|
|
|
std::string document_;
|
|
|
|
std::string indentString_;
|
|
|
|
int rightMargin_;
|
|
|
|
int indentSize_;
|
|
|
|
bool addChildValues_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
|
|
|
|
human friendly way,
|
|
|
|
to a stream rather than to a string.
|
|
|
|
*
|
|
|
|
* The rules for line break and indent are as follow:
|
|
|
|
* - Object value:
|
|
|
|
* - if empty then print {} without indent and line break
|
|
|
|
* - if not empty the print '{', line break & indent, print one value per
|
|
|
|
line
|
|
|
|
* and then unindent and line break and print '}'.
|
|
|
|
* - Array value:
|
|
|
|
* - if empty then print [] without indent and line break
|
|
|
|
* - if the array contains no object value, empty array or some other value
|
|
|
|
types,
|
|
|
|
* and all the values fit on one lines, then print the array on a single
|
|
|
|
line.
|
|
|
|
* - otherwise, it the values do not fit on one line, or the array contains
|
|
|
|
* object or non empty array, then print one value per line.
|
|
|
|
*
|
|
|
|
* If the Value have comments then they are outputed according to their
|
|
|
|
#CommentPlacement.
|
|
|
|
*
|
|
|
|
* \param indentation Each level will be indented by this amount extra.
|
|
|
|
* \sa Reader, Value, Value::setComment()
|
2015-01-25 18:45:59 -06:00
|
|
|
* \deprecated Use StreamWriter::Builder.
|
2014-07-01 08:48:54 +10:00
|
|
|
*/
|
|
|
|
class JSON_API StyledStreamWriter {
|
|
|
|
public:
|
|
|
|
StyledStreamWriter(std::string indentation = "\t");
|
|
|
|
~StyledStreamWriter() {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
|
|
|
|
* \param out Stream to write to. (Can be ostringstream, e.g.)
|
|
|
|
* \param root Value to serialize.
|
|
|
|
* \note There is no point in deriving from Writer, since write() should not
|
|
|
|
* return a value.
|
|
|
|
*/
|
2014-09-15 10:15:29 +10:00
|
|
|
void write(std::ostream& out, const Value& root);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
private:
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeValue(const Value& value);
|
|
|
|
void writeArrayValue(const Value& value);
|
|
|
|
bool isMultineArray(const Value& value);
|
|
|
|
void pushValue(const std::string& value);
|
2014-07-01 08:48:54 +10:00
|
|
|
void writeIndent();
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeWithIndent(const std::string& value);
|
2014-07-01 08:48:54 +10:00
|
|
|
void indent();
|
|
|
|
void unindent();
|
2014-09-15 10:15:29 +10:00
|
|
|
void writeCommentBeforeValue(const Value& root);
|
|
|
|
void writeCommentAfterValueOnSameLine(const Value& root);
|
|
|
|
bool hasCommentForValue(const Value& value);
|
|
|
|
static std::string normalizeEOL(const std::string& text);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
typedef std::vector<std::string> ChildValues;
|
|
|
|
|
|
|
|
ChildValues childValues_;
|
2014-09-15 10:15:29 +10:00
|
|
|
std::ostream* document_;
|
2014-07-01 08:48:54 +10:00
|
|
|
std::string indentString_;
|
|
|
|
int rightMargin_;
|
|
|
|
std::string indentation_;
|
2015-01-23 13:09:43 -06:00
|
|
|
bool addChildValues_ : 1;
|
|
|
|
bool indented_ : 1;
|
2014-07-01 08:48:54 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(JSON_HAS_INT64)
|
|
|
|
std::string JSON_API valueToString(Int value);
|
|
|
|
std::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);
|
2014-09-15 10:15:29 +10:00
|
|
|
std::string JSON_API valueToQuotedString(const char* value);
|
2014-07-01 08:48:54 +10:00
|
|
|
|
|
|
|
/// \brief Output using the StyledStreamWriter.
|
|
|
|
/// \see Json::operator>>()
|
2014-09-15 10:15:29 +10:00
|
|
|
JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
|
2007-06-13 15:51:04 +00:00
|
|
|
|
|
|
|
} // namespace Json
|
|
|
|
|
2013-05-08 20:21:11 +00:00
|
|
|
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
2014-07-01 08:48:54 +10:00
|
|
|
#pragma warning(pop)
|
2013-05-08 20:21:11 +00:00
|
|
|
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
|
|
|
|
2007-06-13 15:51:04 +00:00
|
|
|
#endif // JSON_WRITER_H_INCLUDED
|