JsonCpp project page | Classes | Namespace | JsonCpp home page

writer.h
Go to the documentation of this file.
// 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
#ifndef JSON_WRITER_H_INCLUDED
#define JSON_WRITER_H_INCLUDED
#if !defined(JSON_IS_AMALGAMATION)
#include "value.h"
#endif // if !defined(JSON_IS_AMALGAMATION)
#include <ostream>
#include <string>
#include <vector>
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
// be used by...
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4251)
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
#pragma pack(push)
#pragma pack()
namespace Json {
class Value;
class JSON_API StreamWriter {
protected:
OStream* sout_; // not owned; will not delete
public:
StreamWriter();
virtual ~StreamWriter();
virtual int write(Value const& root, OStream* sout) = 0;
class JSON_API Factory {
public:
virtual ~Factory();
virtual StreamWriter* newStreamWriter() const = 0;
}; // Factory
}; // StreamWriter
String JSON_API writeString(StreamWriter::Factory const& factory,
Value const& root);
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.
Json::Value settings_;
StreamWriterBuilder();
~StreamWriterBuilder() override;
StreamWriter* newStreamWriter() const override;
bool validate(Json::Value* invalid) const;
Value& operator[](const String& key);
static void setDefaults(Json::Value* settings);
};
class JSON_API Writer {
public:
virtual ~Writer();
virtual String write(const Value& root) = 0;
};
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996) // Deriving from deprecated class
#endif
class JSON_API FastWriter : public Writer {
public:
FastWriter();
~FastWriter() override = default;
void enableYAMLCompatibility();
void dropNullPlaceholders();
void omitEndingLineFeed();
public: // overridden from Writer
String write(const Value& root) override;
private:
void writeValue(const Value& value);
String document_;
bool yamlCompatibilityEnabled_{false};
bool dropNullPlaceholders_{false};
bool omitEndingLineFeed_{false};
};
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996) // Deriving from deprecated class
#endif
class JSON_API StyledWriter : public Writer {
public:
StyledWriter();
~StyledWriter() override = default;
public: // overridden from Writer
String write(const Value& root) override;
private:
void writeValue(const Value& value);
void writeArrayValue(const Value& value);
bool isMultilineArray(const Value& value);
void pushValue(const String& value);
void writeIndent();
void writeWithIndent(const String& value);
void indent();
void unindent();
void writeCommentBeforeValue(const Value& root);
void writeCommentAfterValueOnSameLine(const Value& root);
static bool hasCommentForValue(const Value& value);
static String normalizeEOL(const String& text);
using ChildValues = std::vector<String>;
ChildValues childValues_;
String document_;
String indentString_;
unsigned int rightMargin_{74};
unsigned int indentSize_{3};
bool addChildValues_{false};
};
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4996) // Deriving from deprecated class
#endif
class JSON_API StyledStreamWriter {
public:
StyledStreamWriter(String indentation = "\t");
~StyledStreamWriter() = default;
public:
void write(OStream& out, const Value& root);
private:
void writeValue(const Value& value);
void writeArrayValue(const Value& value);
bool isMultilineArray(const Value& value);
void pushValue(const String& value);
void writeIndent();
void writeWithIndent(const String& value);
void indent();
void unindent();
void writeCommentBeforeValue(const Value& root);
void writeCommentAfterValueOnSameLine(const Value& root);
static bool hasCommentForValue(const Value& value);
static String normalizeEOL(const String& text);
using ChildValues = std::vector<String>;
ChildValues childValues_;
OStream* document_;
String indentString_;
unsigned int rightMargin_{74};
String indentation_;
bool addChildValues_ : 1;
bool indented_ : 1;
};
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(JSON_HAS_INT64)
String JSON_API valueToString(Int value);
String JSON_API valueToString(UInt value);
#endif // if defined(JSON_HAS_INT64)
String JSON_API valueToString(LargestInt value);
String JSON_API valueToString(LargestUInt value);
String JSON_API valueToString(
double value, unsigned int precision = Value::defaultRealPrecision,
PrecisionType precisionType = PrecisionType::significantDigits);
String JSON_API valueToString(bool value);
String JSON_API valueToQuotedString(const char* value);
String JSON_API valueToQuotedString(const char* value, size_t length);
JSON_API OStream& operator<<(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)
#endif // JSON_WRITER_H_INCLUDED
Json::FastWriter::write
String write(const Value &root) override
Definition json_writer.cpp:315
Json::FastWriter::FastWriter
FastWriter()
Json::FastWriter::dropNullPlaceholders
void dropNullPlaceholders()
Drop the "null" string from the writer's output for nullValues.
Definition json_writer.cpp:311
Json::FastWriter::enableYAMLCompatibility
void enableYAMLCompatibility()
Definition json_writer.cpp:309
Json::FastWriter::~FastWriter
~FastWriter() override=default
Json::FastWriter::omitEndingLineFeed
void omitEndingLineFeed()
Definition json_writer.cpp:313
Json::StreamWriter::Factory
A simple abstract factory.
Definition writer.h:59
Json::StreamWriter::Factory::~Factory
virtual ~Factory()
Json::StreamWriter::Factory::newStreamWriter
virtual StreamWriter * newStreamWriter() const =0
Allocate a CharReader via operator new().
Json::StreamWriterBuilder
Build a StreamWriter implementation.
Definition writer.h:90
Json::StreamWriterBuilder::newStreamWriter
StreamWriter * newStreamWriter() const override
Configuration of this builder.
Definition json_writer.cpp:1104
Json::StreamWriterBuilder::validate
bool validate(Json::Value *invalid) const
Definition json_writer.cpp:1147
Json::StreamWriterBuilder::setDefaults
static void setDefaults(Json::Value *settings)
Called by ctor, but you can use this to reset settings_.
Definition json_writer.cpp:1174
Json::StreamWriterBuilder::operator[]
Value & operator[](const String &key)
A simple way to update a specific setting.
Definition json_writer.cpp:1170
Json::StreamWriter
Usage:
Definition writer.h:42
Json::StreamWriter::~StreamWriter
virtual ~StreamWriter()
Json::StreamWriter::StreamWriter
StreamWriter()
Definition json_writer.cpp:1099
Json::StreamWriter::write
virtual int write(Value const &root, OStream *sout)=0
Write Value into document as configured in sub-class.
Json::StreamWriter::sout_
OStream * sout_
Definition writer.h:44
Json::StyledStreamWriter::write
void write(OStream &out, const Value &root)
Serialize a Value in JSON format.
Definition json_writer.cpp:595
Json::StyledStreamWriter::StyledStreamWriter
StyledStreamWriter(String indentation="\t")
Definition json_writer.cpp:591
Json::StyledStreamWriter::~StyledStreamWriter
~StyledStreamWriter()=default
Json::StyledWriter::StyledWriter
StyledWriter()
Json::StyledWriter::~StyledWriter
~StyledWriter() override=default
Json::StyledWriter::write
String write(const Value &root) override
Serialize a Value in JSON format.
Definition json_writer.cpp:381
Json::Value
Represents a JSON value.
Definition value.h:207
Json::Value::defaultRealPrecision
static constexpr UInt defaultRealPrecision
Default precision for real value for string representation.
Definition value.h:261
Json::Writer
Abstract class for writers.
Definition writer.h:151
Json::Writer::~Writer
virtual ~Writer()
Json::Writer::write
virtual String write(const Value &root)=0
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition config.h:50
Json
JSON (JavaScript Object Notation).
Definition allocator.h:16
Json::writeString
String writeString(StreamWriter::Factory const &factory, Value const &root)
Write into stringstream, then return string, for convenience.
Definition json_writer.cpp:1187
Json::valueToQuotedString
String valueToQuotedString(const char *value)
Definition json_writer.cpp:290
Json::valueToString
String valueToString(Int value)
Definition json_writer.cpp:57
Json::UInt
unsigned int UInt
Definition config.h:110
Json::operator<<
OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
Definition json_writer.cpp:1194
Json::Int
int Int
Definition config.h:109
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135
Json::OStream
std::ostream OStream
Definition config.h:143
Json::significantDigits
@ significantDigits
we set max number of significant digits in string
Definition value.h:143
value.h