Twitch SDK (Internal)
writer.h
Go to the documentation of this file.
1 #ifndef JSON_WRITER_H_INCLUDED
2 # define JSON_WRITER_H_INCLUDED
3 
4 # include "value.h"
5 # include <vector>
6 # include <string>
7 # include <iostream>
8 
9 namespace ttv {
10 
11  namespace json {
12 
13  class Value;
14 
18  {
19  public:
20  virtual ~Writer();
21 
22  virtual std::string write( const Value &root ) = 0;
23  };
24 
31  class JSON_API FastWriter : public Writer
32  {
33  public:
34  FastWriter();
35  virtual ~FastWriter(){}
36 
37  void enableYAMLCompatibility();
38 
39  public: // overridden from Writer
40  virtual std::string write( const Value &root );
41 
42  private:
43  void writeValue( const Value &value );
44 
45  std::string document_;
47  };
48 
68  {
69  public:
70  StyledWriter();
71  virtual ~StyledWriter(){}
72 
73  public: // overridden from Writer
78  virtual std::string write( const Value &root );
79 
80  private:
81  void writeValue( const Value &value );
82  void writeArrayValue( const Value &value );
83  bool isMultineArray( const Value &value );
84  void pushValue( const std::string &value );
85  void writeIndent();
86  void writeWithIndent( const std::string &value );
87  void indent();
88  void unindent();
89  void writeCommentBeforeValue( const Value &root );
90  void writeCommentAfterValueOnSameLine( const Value &root );
91  bool hasCommentForValue( const Value &value );
92  static std::string normalizeEOL( const std::string &text );
93 
94  typedef std::vector<std::string> ChildValues;
95 
96  ChildValues childValues_;
97  std::string document_;
98  std::string indentString_;
99  unsigned int rightMargin_;
100  unsigned int indentSize_;
102  };
103 
125  {
126  public:
127  StyledStreamWriter( std::string indentation="\t" );
129 
130  public:
136  void write( std::ostream &out, const Value &root );
137 
138  private:
139  void writeValue( const Value &value );
140  void writeArrayValue( const Value &value );
141  bool isMultineArray( const Value &value );
142  void pushValue( const std::string &value );
143  void writeIndent();
144  void writeWithIndent( const std::string &value );
145  void indent();
146  void unindent();
147  void writeCommentBeforeValue( const Value &root );
148  void writeCommentAfterValueOnSameLine( const Value &root );
149  bool hasCommentForValue( const Value &value );
150  static std::string normalizeEOL( const std::string &text );
151 
152  typedef std::vector<std::string> ChildValues;
153 
154  ChildValues childValues_;
155  std::ostream* document_;
156  std::string indentString_;
157  unsigned int rightMargin_;
158  std::string indentation_;
160  };
161 
162  std::string JSON_API valueToString( Int value );
163  std::string JSON_API valueToString( UInt value );
164  std::string JSON_API valueToString( Int64 value );
165  std::string JSON_API valueToString( UInt64 value );
166  std::string JSON_API valueToString( double value );
167  std::string JSON_API valueToString( bool value );
168  std::string JSON_API valueToQuotedString( const char *value );
169 
172  std::ostream& operator<<( std::ostream&, const Value &root );
173 
174  } // namespace json
175 
176 } // namespace ttv
177 
178 
179 #endif // JSON_WRITER_H_INCLUDED
int64_t Int64
Definition: forwards.h:24
#define JSON_API
Definition: config.h:40
std::vector< std::string > ChildValues
Definition: writer.h:152
std::string JSON_API valueToQuotedString(const char *value)
std::string indentation_
Definition: writer.h:158
unsigned int rightMargin_
Definition: writer.h:157
~StyledStreamWriter()
Definition: writer.h:128
bool addChildValues_
Definition: writer.h:101
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string...
Definition: writer.h:124
int32_t Int
Definition: forwards.h:19
std::string document_
Definition: writer.h:97
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
bool addChildValues_
Definition: writer.h:159
Writes a Value in JSON format in a human friendly way.
Definition: writer.h:67
std::string JSON_API valueToString(Int value)
Represents a JSON value.
Definition: value.h:114
std::string indentString_
Definition: writer.h:156
Abstract class for writers.
Definition: writer.h:17
std::ostream & operator<<(std::ostream &, const Value &root)
Output using the StyledStreamWriter.
uint64_t UInt64
Definition: forwards.h:25
std::ostream * document_
Definition: writer.h:155
unsigned int indentSize_
Definition: writer.h:100
std::string document_
Definition: writer.h:45
uint32_t UInt
Definition: forwards.h:23
virtual ~FastWriter()
Definition: writer.h:35
ChildValues childValues_
Definition: writer.h:154
virtual ~StyledWriter()
Definition: writer.h:71
bool yamlCompatiblityEnabled_
Definition: writer.h:46
std::vector< std::string > ChildValues
Definition: writer.h:94
unsigned int rightMargin_
Definition: writer.h:99
Outputs a Value in JSON format without formatting (not human friendly).
Definition: writer.h:31
std::string indentString_
Definition: writer.h:98
ChildValues childValues_
Definition: writer.h:96