Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef JSON_WRITER_H_INCLUDED
00007 #define JSON_WRITER_H_INCLUDED
00008
00009 #if !defined(JSON_IS_AMALGAMATION)
00010 #include "value.h"
00011 #endif // if !defined(JSON_IS_AMALGAMATION)
00012 #include <vector>
00013 #include <string>
00014 #include <ostream>
00015
00016
00017
00018 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00019 #pragma warning(push)
00020 #pragma warning(disable : 4251)
00021 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00022
00023 namespace Json {
00024
00025 class Value;
00026
00040 class JSON_API StreamWriter {
00041 protected:
00042 std::ostream* sout_;
00043 public:
00044 StreamWriter();
00045 virtual ~StreamWriter();
00052 virtual int write(Value const& root, std::ostream* sout) = 0;
00053
00056 class JSON_API Factory {
00057 public:
00058 virtual ~Factory();
00062 virtual StreamWriter* newStreamWriter() const = 0;
00063 };
00064 };
00065
00069 std::string JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
00070
00071
00087 class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
00088 public:
00089
00090
00108 Json::Value settings_;
00109
00110 StreamWriterBuilder();
00111 virtual ~StreamWriterBuilder();
00112
00116 virtual StreamWriter* newStreamWriter() const;
00117
00121 bool validate(Json::Value* invalid) const;
00124 Value& operator[](std::string key);
00125
00131 static void setDefaults(Json::Value* settings);
00132 };
00133
00137 class JSON_API Writer {
00138 public:
00139 virtual ~Writer();
00140
00141 virtual std::string write(const Value& root) = 0;
00142 };
00143
00153 class JSON_API FastWriter : public Writer {
00154
00155 public:
00156 FastWriter();
00157 virtual ~FastWriter() {}
00158
00159 void enableYAMLCompatibility();
00160
00161 public:
00162 virtual std::string write(const Value& root);
00163
00164 private:
00165 void writeValue(const Value& value);
00166
00167 std::string document_;
00168 bool yamlCompatiblityEnabled_;
00169 };
00170
00195 class JSON_API StyledWriter : public Writer {
00196 public:
00197 StyledWriter();
00198 virtual ~StyledWriter() {}
00199
00200 public:
00205 virtual std::string write(const Value& root);
00206
00207 private:
00208 void writeValue(const Value& value);
00209 void writeArrayValue(const Value& value);
00210 bool isMultineArray(const Value& value);
00211 void pushValue(const std::string& value);
00212 void writeIndent();
00213 void writeWithIndent(const std::string& value);
00214 void indent();
00215 void unindent();
00216 void writeCommentBeforeValue(const Value& root);
00217 void writeCommentAfterValueOnSameLine(const Value& root);
00218 bool hasCommentForValue(const Value& value);
00219 static std::string normalizeEOL(const std::string& text);
00220
00221 typedef std::vector<std::string> ChildValues;
00222
00223 ChildValues childValues_;
00224 std::string document_;
00225 std::string indentString_;
00226 int rightMargin_;
00227 int indentSize_;
00228 bool addChildValues_;
00229 };
00230
00257 class JSON_API StyledStreamWriter {
00258 public:
00259 StyledStreamWriter(std::string indentation = "\t");
00260 ~StyledStreamWriter() {}
00261
00262 public:
00269 void write(std::ostream& out, const Value& root);
00270
00271 private:
00272 void writeValue(const Value& value);
00273 void writeArrayValue(const Value& value);
00274 bool isMultineArray(const Value& value);
00275 void pushValue(const std::string& value);
00276 void writeIndent();
00277 void writeWithIndent(const std::string& value);
00278 void indent();
00279 void unindent();
00280 void writeCommentBeforeValue(const Value& root);
00281 void writeCommentAfterValueOnSameLine(const Value& root);
00282 bool hasCommentForValue(const Value& value);
00283 static std::string normalizeEOL(const std::string& text);
00284
00285 typedef std::vector<std::string> ChildValues;
00286
00287 ChildValues childValues_;
00288 std::ostream* document_;
00289 std::string indentString_;
00290 int rightMargin_;
00291 std::string indentation_;
00292 bool addChildValues_ : 1;
00293 bool indented_ : 1;
00294 };
00295
00296 #if defined(JSON_HAS_INT64)
00297 std::string JSON_API valueToString(Int value);
00298 std::string JSON_API valueToString(UInt value);
00299 #endif // if defined(JSON_HAS_INT64)
00300 std::string JSON_API valueToString(LargestInt value);
00301 std::string JSON_API valueToString(LargestUInt value);
00302 std::string JSON_API valueToString(double value);
00303 std::string JSON_API valueToString(bool value);
00304 std::string JSON_API valueToQuotedString(const char* value);
00305
00308 JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
00309
00310 }
00311
00312 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00313 #pragma warning(pop)
00314 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00315
00316 #endif // JSON_WRITER_H_INCLUDED