JsonCpp project page JsonCpp home page

writer.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5 
6 #ifndef JSON_WRITER_H_INCLUDED
7 # define JSON_WRITER_H_INCLUDED
8 
9 #if !defined(JSON_IS_AMALGAMATION)
10 # include "value.h"
11 #endif // if !defined(JSON_IS_AMALGAMATION)
12 # include <vector>
13 # include <string>
14 # include <iostream>
15 
16 namespace Json {
17 
18  class Value;
19 
23  {
24  public:
25  virtual ~Writer();
26 
27  virtual std::string write( const Value &root ) = 0;
28  };
29 
36  class JSON_API FastWriter : public Writer
37  {
38  public:
39  FastWriter();
40  virtual ~FastWriter(){}
41 
42  void enableYAMLCompatibility();
43 
44  public: // overridden from Writer
45  virtual std::string write( const Value &root );
46 
47  private:
48  void writeValue( const Value &value );
49 
50  std::string document_;
51  bool yamlCompatiblityEnabled_;
52  };
53 
73  {
74  public:
75  StyledWriter();
76  virtual ~StyledWriter(){}
77 
78  public: // overridden from Writer
83  virtual std::string write( const Value &root );
84 
85  private:
86  void writeValue( const Value &value );
87  void writeArrayValue( const Value &value );
88  bool isMultineArray( const Value &value );
89  void pushValue( const std::string &value );
90  void writeIndent();
91  void writeWithIndent( const std::string &value );
92  void indent();
93  void unindent();
94  void writeCommentBeforeValue( const Value &root );
95  void writeCommentAfterValueOnSameLine( const Value &root );
96  bool hasCommentForValue( const Value &value );
97  static std::string normalizeEOL( const std::string &text );
98 
99  typedef std::vector<std::string> ChildValues;
100 
101  ChildValues childValues_;
102  std::string document_;
103  std::string indentString_;
104  int rightMargin_;
105  int indentSize_;
106  bool addChildValues_;
107  };
108 
130  {
131  public:
132  StyledStreamWriter( std::string indentation="\t" );
134 
135  public:
141  void write( std::ostream &out, const Value &root );
142 
143  private:
144  void writeValue( const Value &value );
145  void writeArrayValue( const Value &value );
146  bool isMultineArray( const Value &value );
147  void pushValue( const std::string &value );
148  void writeIndent();
149  void writeWithIndent( const std::string &value );
150  void indent();
151  void unindent();
152  void writeCommentBeforeValue( const Value &root );
153  void writeCommentAfterValueOnSameLine( const Value &root );
154  bool hasCommentForValue( const Value &value );
155  static std::string normalizeEOL( const std::string &text );
156 
157  typedef std::vector<std::string> ChildValues;
158 
159  ChildValues childValues_;
160  std::ostream* document_;
161  std::string indentString_;
162  int rightMargin_;
163  std::string indentation_;
164  bool addChildValues_;
165  };
166 
167 # if defined(JSON_HAS_INT64)
168  std::string JSON_API valueToString( Int value );
169  std::string JSON_API valueToString( UInt value );
170 # endif // if defined(JSON_HAS_INT64)
171  std::string JSON_API valueToString( LargestInt value );
172  std::string JSON_API valueToString( LargestUInt value );
173  std::string JSON_API valueToString( double value );
174  std::string JSON_API valueToString( bool value );
175  std::string JSON_API valueToQuotedString( const char *value );
176 
179  std::ostream& operator<<( std::ostream&, const Value &root );
180 
181 } // namespace Json
182 
183 
184 
185 #endif // JSON_WRITER_H_INCLUDED
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:36
Int64 LargestInt
Definition: config.h:89
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:72
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:51
virtual ~FastWriter()
Definition: writer.h:40
std::string valueToQuotedString(const char *value)
UInt64 LargestUInt
Definition: config.h:90
std::string valueToString(Int value)
Definition: json_writer.cpp:61
JSON (JavaScript Object Notation).
Definition: config.h:73
Abstract class for writers.
Definition: writer.h:22
Represents a JSON value.
Definition: value.h:118
unsigned int UInt
Definition: config.h:75
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:129
virtual ~StyledWriter()
Definition: writer.h:76
int Int
Definition: config.h:74
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.

SourceForge Logo hosts this site. Send comments to:
Json-cpp Developers