annotate json/writer.h @ 15:d5758530a039 tip

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