$extrastylesheet
JsonCpp project page Classes Namespace JsonCpp home page

include/json/writer.h
Go to the documentation of this file.
00001 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
00002 // Distributed under MIT license, or public domain if desired and
00003 // recognized in your jurisdiction.
00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
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 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
00017 // be used by...
00018 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER)
00019 #pragma warning(push)
00020 #pragma warning(disable : 4251)
00021 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00022 
00023 #pragma pack(push, 8)
00024 
00025 namespace Json {
00026 
00027 class Value;
00028 
00042 class JSON_API StreamWriter {
00043 protected:
00044   JSONCPP_OSTREAM* sout_;  // not owned; will not delete
00045 public:
00046   StreamWriter();
00047   virtual ~StreamWriter();
00054   virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
00055 
00058   class JSON_API Factory {
00059   public:
00060     virtual ~Factory();
00064     virtual StreamWriter* newStreamWriter() const = 0;
00065   };  // Factory
00066 };  // StreamWriter
00067 
00071 JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
00072 
00073 
00089 class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
00090 public:
00091   // Note: We use a Json::Value so that we can add data-members to this class
00092   // without a major version bump.
00114   Json::Value settings_;
00115 
00116   StreamWriterBuilder();
00117   ~StreamWriterBuilder() JSONCPP_OVERRIDE;
00118 
00122   StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE;
00123 
00127   bool validate(Json::Value* invalid) const;
00130   Value& operator[](JSONCPP_STRING key);
00131 
00137   static void setDefaults(Json::Value* settings);
00138 };
00139 
00143 class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer {
00144 public:
00145   virtual ~Writer();
00146 
00147   virtual JSONCPP_STRING write(const Value& root) = 0;
00148 };
00149 
00159 #if defined(_MSC_VER)
00160 #pragma warning(push)
00161 #pragma warning(disable:4996) // Deriving from deprecated class
00162 #endif
00163 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer {
00164 public:
00165   FastWriter();
00166   ~FastWriter() JSONCPP_OVERRIDE {}
00167 
00168   void enableYAMLCompatibility();
00169 
00175   void dropNullPlaceholders();
00176 
00177   void omitEndingLineFeed();
00178 
00179 public: // overridden from Writer
00180   JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
00181 
00182 private:
00183   void writeValue(const Value& value);
00184 
00185   JSONCPP_STRING document_;
00186   bool yamlCompatibilityEnabled_;
00187   bool dropNullPlaceholders_;
00188   bool omitEndingLineFeed_;
00189 };
00190 #if defined(_MSC_VER)
00191 #pragma warning(pop)
00192 #endif
00193 
00218 #if defined(_MSC_VER)
00219 #pragma warning(push)
00220 #pragma warning(disable:4996) // Deriving from deprecated class
00221 #endif
00222 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer {
00223 public:
00224   StyledWriter();
00225   ~StyledWriter() JSONCPP_OVERRIDE {}
00226 
00227 public: // overridden from Writer
00232   JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
00233 
00234 private:
00235   void writeValue(const Value& value);
00236   void writeArrayValue(const Value& value);
00237   bool isMultilineArray(const Value& value);
00238   void pushValue(const JSONCPP_STRING& value);
00239   void writeIndent();
00240   void writeWithIndent(const JSONCPP_STRING& value);
00241   void indent();
00242   void unindent();
00243   void writeCommentBeforeValue(const Value& root);
00244   void writeCommentAfterValueOnSameLine(const Value& root);
00245   bool hasCommentForValue(const Value& value);
00246   static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
00247 
00248   typedef std::vector<JSONCPP_STRING> ChildValues;
00249 
00250   ChildValues childValues_;
00251   JSONCPP_STRING document_;
00252   JSONCPP_STRING indentString_;
00253   unsigned int rightMargin_;
00254   unsigned int indentSize_;
00255   bool addChildValues_;
00256 };
00257 #if defined(_MSC_VER)
00258 #pragma warning(pop)
00259 #endif
00260 
00286 #if defined(_MSC_VER)
00287 #pragma warning(push)
00288 #pragma warning(disable:4996) // Deriving from deprecated class
00289 #endif
00290 class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledStreamWriter {
00291 public:
00295   StyledStreamWriter(JSONCPP_STRING indentation = "\t");
00296   ~StyledStreamWriter() {}
00297 
00298 public:
00305   void write(JSONCPP_OSTREAM& out, const Value& root);
00306 
00307 private:
00308   void writeValue(const Value& value);
00309   void writeArrayValue(const Value& value);
00310   bool isMultilineArray(const Value& value);
00311   void pushValue(const JSONCPP_STRING& value);
00312   void writeIndent();
00313   void writeWithIndent(const JSONCPP_STRING& value);
00314   void indent();
00315   void unindent();
00316   void writeCommentBeforeValue(const Value& root);
00317   void writeCommentAfterValueOnSameLine(const Value& root);
00318   bool hasCommentForValue(const Value& value);
00319   static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
00320 
00321   typedef std::vector<JSONCPP_STRING> ChildValues;
00322 
00323   ChildValues childValues_;
00324   JSONCPP_OSTREAM* document_;
00325   JSONCPP_STRING indentString_;
00326   unsigned int rightMargin_;
00327   JSONCPP_STRING indentation_;
00328   bool addChildValues_ : 1;
00329   bool indented_ : 1;
00330 };
00331 #if defined(_MSC_VER)
00332 #pragma warning(pop)
00333 #endif
00334 
00335 #if defined(JSON_HAS_INT64)
00336 JSONCPP_STRING JSON_API valueToString(Int value);
00337 JSONCPP_STRING JSON_API valueToString(UInt value);
00338 #endif // if defined(JSON_HAS_INT64)
00339 JSONCPP_STRING JSON_API valueToString(LargestInt value);
00340 JSONCPP_STRING JSON_API valueToString(LargestUInt value);
00341 JSONCPP_STRING JSON_API valueToString(double value);
00342 JSONCPP_STRING JSON_API valueToString(bool value);
00343 JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
00344 
00347 JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
00348 
00349 } // namespace Json
00350 
00351 #pragma pack(pop)
00352 
00353 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00354 #pragma warning(pop)
00355 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00356 
00357 #endif // JSON_WRITER_H_INCLUDED