JsonCpp project page JsonCpp home page

include/json/writer.h
Go to the documentation of this file.
00001 // Copyright 2007-2010 Baptiste Lepilleur
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)
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   JSONCPP_OSTREAM* sout_;  // not owned; will not delete
00043 public:
00044   StreamWriter();
00045   virtual ~StreamWriter();
00052   virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
00053 
00056   class JSON_API Factory {
00057   public:
00058     virtual ~Factory();
00062     virtual StreamWriter* newStreamWriter() const = 0;
00063   };  // Factory
00064 };  // StreamWriter
00065 
00069 JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
00070 
00071 
00087 class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
00088 public:
00089   // Note: We use a Json::Value so that we can add data-members to this class
00090   // without a major version bump.
00112   Json::Value settings_;
00113 
00114   StreamWriterBuilder();
00115   ~StreamWriterBuilder() JSONCPP_OVERRIDE;
00116 
00120   StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE;
00121 
00125   bool validate(Json::Value* invalid) const;
00128   Value& operator[](JSONCPP_STRING key);
00129 
00135   static void setDefaults(Json::Value* settings);
00136 };
00137 
00141 class JSON_API Writer {
00142 public:
00143   virtual ~Writer();
00144 
00145   virtual JSONCPP_STRING write(const Value& root) = 0;
00146 };
00147 
00157 class JSON_API FastWriter : public Writer {
00158 
00159 public:
00160   FastWriter();
00161   ~FastWriter() JSONCPP_OVERRIDE {}
00162 
00163   void enableYAMLCompatibility();
00164 
00170   void dropNullPlaceholders();
00171 
00172   void omitEndingLineFeed();
00173 
00174 public: // overridden from Writer
00175   JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
00176 
00177 private:
00178   void writeValue(const Value& value);
00179 
00180   JSONCPP_STRING document_;
00181   bool yamlCompatiblityEnabled_;
00182   bool dropNullPlaceholders_;
00183   bool omitEndingLineFeed_;
00184 };
00185 
00210 class JSON_API StyledWriter : public Writer {
00211 public:
00212   StyledWriter();
00213   ~StyledWriter() JSONCPP_OVERRIDE {}
00214 
00215 public: // overridden from Writer
00220   JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
00221 
00222 private:
00223   void writeValue(const Value& value);
00224   void writeArrayValue(const Value& value);
00225   bool isMultineArray(const Value& value);
00226   void pushValue(const JSONCPP_STRING& value);
00227   void writeIndent();
00228   void writeWithIndent(const JSONCPP_STRING& value);
00229   void indent();
00230   void unindent();
00231   void writeCommentBeforeValue(const Value& root);
00232   void writeCommentAfterValueOnSameLine(const Value& root);
00233   bool hasCommentForValue(const Value& value);
00234   static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
00235 
00236   typedef std::vector<JSONCPP_STRING> ChildValues;
00237 
00238   ChildValues childValues_;
00239   JSONCPP_STRING document_;
00240   JSONCPP_STRING indentString_;
00241   unsigned int rightMargin_;
00242   unsigned int indentSize_;
00243   bool addChildValues_;
00244 };
00245 
00272 class JSON_API StyledStreamWriter {
00273 public:
00274   StyledStreamWriter(JSONCPP_STRING indentation = "\t");
00275   ~StyledStreamWriter() {}
00276 
00277 public:
00284   void write(JSONCPP_OSTREAM& out, const Value& root);
00285 
00286 private:
00287   void writeValue(const Value& value);
00288   void writeArrayValue(const Value& value);
00289   bool isMultineArray(const Value& value);
00290   void pushValue(const JSONCPP_STRING& value);
00291   void writeIndent();
00292   void writeWithIndent(const JSONCPP_STRING& value);
00293   void indent();
00294   void unindent();
00295   void writeCommentBeforeValue(const Value& root);
00296   void writeCommentAfterValueOnSameLine(const Value& root);
00297   bool hasCommentForValue(const Value& value);
00298   static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
00299 
00300   typedef std::vector<JSONCPP_STRING> ChildValues;
00301 
00302   ChildValues childValues_;
00303   JSONCPP_OSTREAM* document_;
00304   JSONCPP_STRING indentString_;
00305   unsigned int rightMargin_;
00306   JSONCPP_STRING indentation_;
00307   bool addChildValues_ : 1;
00308   bool indented_ : 1;
00309 };
00310 
00311 #if defined(JSON_HAS_INT64)
00312 JSONCPP_STRING JSON_API valueToString(Int value);
00313 JSONCPP_STRING JSON_API valueToString(UInt value);
00314 #endif // if defined(JSON_HAS_INT64)
00315 JSONCPP_STRING JSON_API valueToString(LargestInt value);
00316 JSONCPP_STRING JSON_API valueToString(LargestUInt value);
00317 JSONCPP_STRING JSON_API valueToString(double value);
00318 JSONCPP_STRING JSON_API valueToString(bool value);
00319 JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
00320 
00323 JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
00324 
00325 } // namespace Json
00326 
00327 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00328 #pragma warning(pop)
00329 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00330 
00331 #endif // JSON_WRITER_H_INCLUDED