Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame^] | 1 | #ifndef JSON_WRITER_H_INCLUDED
|
| 2 | # define JSON_WRITER_H_INCLUDED
|
| 3 |
|
| 4 | # include "value.h"
|
| 5 | # include <vector>
|
| 6 | # include <string>
|
| 7 |
|
| 8 | namespace Json {
|
| 9 |
|
| 10 | class Value;
|
| 11 |
|
| 12 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
|
| 13 | *
|
| 14 | * The JSON document is written in a single line. It is not intended for 'human' consumption,
|
| 15 | * but may be usefull to support feature such as RPC where bandwith is limited.
|
| 16 | * \sa Reader, Value
|
| 17 | */
|
| 18 | class JSON_API FastWriter
|
| 19 | {
|
| 20 | public:
|
| 21 | std::string write( const Value &root );
|
| 22 |
|
| 23 | private:
|
| 24 | void writeValue( const Value &value );
|
| 25 |
|
| 26 | std::string document_;
|
| 27 | };
|
| 28 |
|
| 29 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
|
| 30 | *
|
| 31 | * The rules for line break and indent are as follow:
|
| 32 | * - Object value:
|
| 33 | * - if empty then print {} without indent and line break
|
| 34 | * - if not empty the print '{', line break & indent, print one value per line
|
| 35 | * and then unindent and line break and print '}'.
|
| 36 | * - Array value:
|
| 37 | * - if empty then print [] without indent and line break
|
| 38 | * - if the array contains no object value, empty array or some other value types,
|
| 39 | * and all the values fit on one lines, then print the array on a single line.
|
| 40 | * - otherwise, it the values do not fit on one line, or the array contains
|
| 41 | * object or non empty array, then print one value per line.
|
| 42 | *
|
| 43 | * If the Value have comments then they are outputed according to their #CommentPlacement.
|
| 44 | *
|
| 45 | * \sa Reader, Value, Value::setComment()
|
| 46 | */
|
| 47 | class JSON_API StyledWriter
|
| 48 | {
|
| 49 | public:
|
| 50 | StyledWriter();
|
| 51 |
|
| 52 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
|
| 53 | * \param root Value to serialize.
|
| 54 | * \return String containing the JSON document that represent the root value.
|
| 55 | */
|
| 56 | std::string write( const Value &root );
|
| 57 |
|
| 58 | private:
|
| 59 | void writeValue( const Value &value );
|
| 60 | void writeArrayValue( const Value &value );
|
| 61 | bool isMultineArray( const Value &value );
|
| 62 | void pushValue( const std::string &value );
|
| 63 | void writeIndent();
|
| 64 | void writeWithIndent( const std::string &value );
|
| 65 | void indent();
|
| 66 | void unindent();
|
| 67 | void writeCommentBeforeValue( const Value &root );
|
| 68 | void writeCommentAfterValueOnSameLine( const Value &root );
|
| 69 | bool hasCommentForValue( const Value &value );
|
| 70 | static std::string normalizeEOL( const std::string &text );
|
| 71 |
|
| 72 | typedef std::vector<std::string> ChildValues;
|
| 73 |
|
| 74 | ChildValues childValues_;
|
| 75 | std::string document_;
|
| 76 | std::string indentString_;
|
| 77 | int rightMargin_;
|
| 78 | int indentSize_;
|
| 79 | bool addChildValues_;
|
| 80 | };
|
| 81 |
|
| 82 | std::string JSON_API valueToString( Value::Int value );
|
| 83 | std::string JSON_API valueToString( Value::UInt value );
|
| 84 | std::string JSON_API valueToString( double value );
|
| 85 | std::string JSON_API valueToString( bool value );
|
| 86 | std::string JSON_API valueToQuotedString( const char *value );
|
| 87 |
|
| 88 | } // namespace Json
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 | #endif // JSON_WRITER_H_INCLUDED
|