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   std::ostream* sout_;  // not owned; will not delete
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   };  // Factory
00064 };  // StreamWriter
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   // Note: We use a Json::Value so that we can add data-members to this class
00090   // without a major version bump.
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 
00166   void dropNullPlaceholders();
00167 
00168   void omitEndingLineFeed();
00169 
00170 public: // overridden from Writer
00171   virtual std::string write(const Value& root);
00172 
00173 private:
00174   void writeValue(const Value& value);
00175 
00176   std::string document_;
00177   bool yamlCompatiblityEnabled_;
00178   bool dropNullPlaceholders_;
00179   bool omitEndingLineFeed_;
00180 };
00181 
00206 class JSON_API StyledWriter : public Writer {
00207 public:
00208   StyledWriter();
00209   virtual ~StyledWriter() {}
00210 
00211 public: // overridden from Writer
00216   virtual std::string write(const Value& root);
00217 
00218 private:
00219   void writeValue(const Value& value);
00220   void writeArrayValue(const Value& value);
00221   bool isMultineArray(const Value& value);
00222   void pushValue(const std::string& value);
00223   void writeIndent();
00224   void writeWithIndent(const std::string& value);
00225   void indent();
00226   void unindent();
00227   void writeCommentBeforeValue(const Value& root);
00228   void writeCommentAfterValueOnSameLine(const Value& root);
00229   bool hasCommentForValue(const Value& value);
00230   static std::string normalizeEOL(const std::string& text);
00231 
00232   typedef std::vector<std::string> ChildValues;
00233 
00234   ChildValues childValues_;
00235   std::string document_;
00236   std::string indentString_;
00237   int rightMargin_;
00238   int indentSize_;
00239   bool addChildValues_;
00240 };
00241 
00268 class JSON_API StyledStreamWriter {
00269 public:
00270   StyledStreamWriter(std::string indentation = "\t");
00271   ~StyledStreamWriter() {}
00272 
00273 public:
00280   void write(std::ostream& out, const Value& root);
00281 
00282 private:
00283   void writeValue(const Value& value);
00284   void writeArrayValue(const Value& value);
00285   bool isMultineArray(const Value& value);
00286   void pushValue(const std::string& value);
00287   void writeIndent();
00288   void writeWithIndent(const std::string& value);
00289   void indent();
00290   void unindent();
00291   void writeCommentBeforeValue(const Value& root);
00292   void writeCommentAfterValueOnSameLine(const Value& root);
00293   bool hasCommentForValue(const Value& value);
00294   static std::string normalizeEOL(const std::string& text);
00295 
00296   typedef std::vector<std::string> ChildValues;
00297 
00298   ChildValues childValues_;
00299   std::ostream* document_;
00300   std::string indentString_;
00301   int rightMargin_;
00302   std::string indentation_;
00303   bool addChildValues_ : 1;
00304   bool indented_ : 1;
00305 };
00306 
00307 #if defined(JSON_HAS_INT64)
00308 std::string JSON_API valueToString(Int value);
00309 std::string JSON_API valueToString(UInt value);
00310 #endif // if defined(JSON_HAS_INT64)
00311 std::string JSON_API valueToString(LargestInt value);
00312 std::string JSON_API valueToString(LargestUInt value);
00313 std::string JSON_API valueToString(double value);
00314 std::string JSON_API valueToString(bool value);
00315 std::string JSON_API valueToQuotedString(const char* value);
00316 
00319 JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
00320 
00321 } // namespace Json
00322 
00323 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00324 #pragma warning(pop)
00325 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00326 
00327 #endif // JSON_WRITER_H_INCLUDED