blob: 94582d9563189ef855e9c57d2a412ee1b1c09ac5 [file] [log] [blame]
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +00001#ifndef JSON_WRITER_H_INCLUDED
2# define JSON_WRITER_H_INCLUDED
3
4# include "value.h"
5# include <vector>
6# include <string>
7
8namespace Json {
9
10 class Value;
11
Baptiste Lepilleur2d4dd282007-03-17 22:14:59 +000012 /** \brief Abstract class for writers.
13 */
14 class JSON_API Writer
15 {
16 public:
17 virtual ~Writer();
18
19 virtual std::string write( const Value &root ) = 0;
20 };
21
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000022 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
23 *
24 * The JSON document is written in a single line. It is not intended for 'human' consumption,
25 * but may be usefull to support feature such as RPC where bandwith is limited.
26 * \sa Reader, Value
27 */
Baptiste Lepilleur2d4dd282007-03-17 22:14:59 +000028 class JSON_API FastWriter : public Writer
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000029 {
30 public:
Baptiste Lepilleur2d4dd282007-03-17 22:14:59 +000031 FastWriter();
32
33 void enableYAMLCompatibility();
34
35 public: // overridden from Writer
36 virtual std::string write( const Value &root );
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000037
38 private:
39 void writeValue( const Value &value );
40
41 std::string document_;
Baptiste Lepilleur2d4dd282007-03-17 22:14:59 +000042 bool yamlCompatiblityEnabled_;
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000043 };
44
45 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
46 *
47 * The rules for line break and indent are as follow:
48 * - Object value:
49 * - if empty then print {} without indent and line break
50 * - if not empty the print '{', line break & indent, print one value per line
51 * and then unindent and line break and print '}'.
52 * - Array value:
53 * - if empty then print [] without indent and line break
54 * - if the array contains no object value, empty array or some other value types,
55 * and all the values fit on one lines, then print the array on a single line.
56 * - otherwise, it the values do not fit on one line, or the array contains
57 * object or non empty array, then print one value per line.
58 *
59 * If the Value have comments then they are outputed according to their #CommentPlacement.
60 *
61 * \sa Reader, Value, Value::setComment()
62 */
63 class JSON_API StyledWriter
64 {
65 public:
66 StyledWriter();
67
Baptiste Lepilleur2d4dd282007-03-17 22:14:59 +000068 public: // overridden from Writer
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000069 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
70 * \param root Value to serialize.
71 * \return String containing the JSON document that represent the root value.
72 */
Baptiste Lepilleur2d4dd282007-03-17 22:14:59 +000073 virtual std::string write( const Value &root );
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000074
75 private:
76 void writeValue( const Value &value );
77 void writeArrayValue( const Value &value );
78 bool isMultineArray( const Value &value );
79 void pushValue( const std::string &value );
80 void writeIndent();
81 void writeWithIndent( const std::string &value );
82 void indent();
83 void unindent();
84 void writeCommentBeforeValue( const Value &root );
85 void writeCommentAfterValueOnSameLine( const Value &root );
86 bool hasCommentForValue( const Value &value );
87 static std::string normalizeEOL( const std::string &text );
88
89 typedef std::vector<std::string> ChildValues;
90
91 ChildValues childValues_;
92 std::string document_;
93 std::string indentString_;
94 int rightMargin_;
95 int indentSize_;
96 bool addChildValues_;
97 };
98
99 std::string JSON_API valueToString( Value::Int value );
100 std::string JSON_API valueToString( Value::UInt value );
101 std::string JSON_API valueToString( double value );
102 std::string JSON_API valueToString( bool value );
103 std::string JSON_API valueToQuotedString( const char *value );
104
105} // namespace Json
106
107
108
109#endif // JSON_WRITER_H_INCLUDED