Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +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 | # 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 : public Writer |
| 30 | { |
| 31 | public: |
| 32 | FastWriter(); |
| 33 | |
| 34 | void enableYAMLCompatibility(); |
| 35 | |
| 36 | public: // overridden from Writer |
| 37 | virtual std::string write( const Value &root ); |
| 38 | |
| 39 | private: |
| 40 | void writeValue( const Value &value ); |
| 41 | |
| 42 | std::string document_; |
| 43 | bool yamlCompatiblityEnabled_; |
| 44 | }; |
| 45 | |
| 46 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way. |
| 47 | * |
| 48 | * The rules for line break and indent are as follow: |
| 49 | * - Object value: |
| 50 | * - if empty then print {} without indent and line break |
| 51 | * - if not empty the print '{', line break & indent, print one value per line |
| 52 | * and then unindent and line break and print '}'. |
| 53 | * - Array value: |
| 54 | * - if empty then print [] without indent and line break |
| 55 | * - if the array contains no object value, empty array or some other value types, |
| 56 | * and all the values fit on one lines, then print the array on a single line. |
| 57 | * - otherwise, it the values do not fit on one line, or the array contains |
| 58 | * object or non empty array, then print one value per line. |
| 59 | * |
| 60 | * If the Value have comments then they are outputed according to their #CommentPlacement. |
| 61 | * |
| 62 | * \sa Reader, Value, Value::setComment() |
| 63 | */ |
| 64 | class JSON_API StyledWriter |
| 65 | { |
| 66 | public: |
| 67 | StyledWriter(); |
| 68 | virtual ~StyledWriter(){} |
| 69 | |
| 70 | public: // overridden from Writer |
| 71 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 72 | * \param root Value to serialize. |
| 73 | * \return String containing the JSON document that represents the root value. |
| 74 | */ |
| 75 | virtual std::string write( const Value &root ); |
| 76 | |
| 77 | private: |
| 78 | void writeValue( const Value &value ); |
| 79 | void writeArrayValue( const Value &value ); |
| 80 | bool isMultineArray( const Value &value ); |
| 81 | void pushValue( const std::string &value ); |
| 82 | void writeIndent(); |
| 83 | void writeWithIndent( const std::string &value ); |
| 84 | void indent(); |
| 85 | void unindent(); |
| 86 | void writeCommentBeforeValue( const Value &root ); |
| 87 | void writeCommentAfterValueOnSameLine( const Value &root ); |
| 88 | bool hasCommentForValue( const Value &value ); |
| 89 | static std::string normalizeEOL( const std::string &text ); |
| 90 | |
| 91 | typedef std::vector<std::string> ChildValues; |
| 92 | |
| 93 | ChildValues childValues_; |
| 94 | std::string document_; |
| 95 | std::string indentString_; |
| 96 | int rightMargin_; |
| 97 | int indentSize_; |
| 98 | bool addChildValues_; |
| 99 | }; |
| 100 | |
| 101 | std::string JSON_API valueToString( Value::Int value ); |
| 102 | std::string JSON_API valueToString( Value::UInt value ); |
| 103 | std::string JSON_API valueToString( double value ); |
| 104 | std::string JSON_API valueToString( bool value ); |
| 105 | std::string JSON_API valueToQuotedString( const char *value ); |
| 106 | |
| 107 | /// \brief Output using the StyledWriter. |
| 108 | /// \see Json::operator>>() |
| 109 | std::ostream& operator<<( std::ostream&, const Value &root ); |
| 110 | |
| 111 | } // namespace Json |
| 112 | |
| 113 | |
| 114 | |
| 115 | #endif // JSON_WRITER_H_INCLUDED |