ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/include/json/writer.h
Revision: 1.1
Committed: Sun Oct 17 08:14:54 2010 UTC (15 years, 11 months ago) by sf-pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
initial import

File Contents

# User Rev Content
1 sf-pippijn 1.1 #pragma once
2    
3     #include "value.h"
4    
5     #include <vector>
6     #include <string>
7     #include <iostream>
8    
9     namespace Json
10     {
11     class Value;
12    
13     /** \brief Abstract class for writers.
14     */
15     class JSON_API Writer
16     {
17     public:
18     virtual ~Writer ();
19    
20     virtual std::string write (const Value &root) = 0;
21     };
22    
23     /** \brief Outputs a Value in <a href="http://www.json.org">JSON</a> format without formatting (not human friendly).
24     *
25     * The JSON document is written in a single line. It is not intended for 'human' consumption,
26     * but may be usefull to support feature such as RPC where bandwith is limited.
27     * \sa Reader, Value
28     */
29     class JSON_API FastWriter
30     : public Writer
31     {
32     public:
33     FastWriter ();
34     virtual ~FastWriter () { }
35    
36     void enableYAMLCompatibility ();
37    
38     public:
39     /* 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_;
46     bool yamlCompatiblityEnabled_;
47     };
48    
49     /** \brief Writes a Value in <a href="http://www.json.org">JSON</a> format in a human friendly way.
50     *
51     * The rules for line break and indent are as follow:
52     * - Object value:
53     * - if empty then print {} without indent and line break
54     * - if not empty the print '{', line break & indent, print one value per line
55     * and then unindent and line break and print '}'.
56     * - Array value:
57     * - if empty then print [] without indent and line break
58     * - if the array contains no object value, empty array or some other value types,
59     * and all the values fit on one lines, then print the array on a single line.
60     * - otherwise, it the values do not fit on one line, or the array contains
61     * object or non empty array, then print one value per line.
62     *
63     * If the Value have comments then they are outputed according to their #CommentPlacement.
64     *
65     * \sa Reader, Value, Value::setComment()
66     */
67     class JSON_API StyledWriter
68     : public Writer
69     {
70     public:
71     StyledWriter ();
72     virtual ~StyledWriter () { }
73    
74     public:
75     /* overridden from Writer */
76     /** \brief Serialize a Value in <a href="http://www.json.org">JSON</a> format.
77     * \param root Value to serialize.
78     * \return String containing the JSON document that represents the root value.
79     */
80     virtual std::string write (const Value &root);
81    
82     private:
83     void writeValue (const Value &value);
84     void writeArrayValue (const Value &value);
85     bool isMultineArray (const Value &value);
86     void pushValue (const std::string &value);
87     void writeIndent ();
88     void writeWithIndent (const std::string &value);
89     void indent ();
90     void unindent ();
91     void writeCommentBeforeValue (const Value &root);
92     void writeCommentAfterValueOnSameLine (const Value &root);
93     bool hasCommentForValue (const Value &value);
94     static std::string normalizeEOL (const std::string &text);
95    
96     typedef std::vector<std::string> ChildValues;
97    
98     ChildValues childValues_;
99     std::string document_;
100     std::string indentString_;
101     int rightMargin_;
102     int indentSize_;
103     bool addChildValues_;
104     };
105    
106     /** \brief Writes a Value in <a href="http://www.json.org">JSON</a> format in a human friendly way,
107     * to a stream rather than to a string.
108     *
109     * The rules for line break and indent are as follow:
110     * - Object value:
111     * - if empty then print {} without indent and line break
112     * - if not empty the print '{', line break & indent, print one value per line
113     * and then unindent and line break and print '}'.
114     * - Array value:
115     * - if empty then print [] without indent and line break
116     * - if the array contains no object value, empty array or some other value types,
117     * and all the values fit on one lines, then print the array on a single line.
118     * - otherwise, it the values do not fit on one line, or the array contains
119     * object or non empty array, then print one value per line.
120     *
121     * If the Value have comments then they are outputed according to their #CommentPlacement.
122     *
123     * \param indentation Each level will be indented by this amount extra.
124     * \sa Reader, Value, Value::setComment()
125     */
126     class JSON_API StyledStreamWriter
127     {
128     public:
129     StyledStreamWriter (std::string indentation = "\t");
130     ~StyledStreamWriter () { }
131    
132     public:
133     /** \brief Serialize a Value in <a href="http://www.json.org">JSON</a> format.
134     * \param out Stream to write to. (Can be ostringstream, e.g.)
135     * \param root Value to serialize.
136     * \note There is no point in deriving from Writer, since write() should not return a value.
137     */
138     void write (std::ostream &out, const Value &root);
139    
140     private:
141     void writeValue (const Value &value);
142     void writeArrayValue (const Value &value);
143     bool isMultineArray (const Value &value);
144     void pushValue (const std::string &value);
145     void writeIndent ();
146     void writeWithIndent (const std::string &value);
147     void indent ();
148     void unindent ();
149     void writeCommentBeforeValue (const Value &root);
150     void writeCommentAfterValueOnSameLine (const Value &root);
151     bool hasCommentForValue (const Value &value);
152     static std::string normalizeEOL (const std::string &text);
153    
154     typedef std::vector<std::string> ChildValues;
155    
156     ChildValues childValues_;
157     std::ostream *document_;
158     std::string indentString_;
159     std::string indentation_;
160     long rightMargin_;
161     bool addChildValues_;
162     };
163    
164     std::string JSON_API valueToString (Int value);
165     std::string JSON_API valueToString (UInt 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    
170     /**
171     * \brief Output using the StyledStreamWriter.
172     * \see Json::operator>>()
173     */
174     std::ostream &operator << (std::ostream &os, const Value &root);
175     } /* namespace Json */