annotate json/writer.h @ 52:89944ab3e129 tip

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