2007-03-15 23:11:38 +01:00
|
|
|
#ifndef JSON_WRITER_H_INCLUDED
|
|
|
|
# define JSON_WRITER_H_INCLUDED
|
|
|
|
|
|
|
|
# include "value.h"
|
|
|
|
# include <vector>
|
|
|
|
# include <string>
|
2007-03-23 11:19:49 +01:00
|
|
|
# include <iostream>
|
2007-03-15 23:11:38 +01:00
|
|
|
|
|
|
|
namespace Json {
|
|
|
|
|
|
|
|
class Value;
|
|
|
|
|
2007-03-17 23:14:59 +01:00
|
|
|
/** \brief Abstract class for writers.
|
|
|
|
*/
|
|
|
|
class JSON_API Writer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Writer();
|
|
|
|
|
|
|
|
virtual std::string write( const Value &root ) = 0;
|
|
|
|
};
|
|
|
|
|
2007-03-15 23:11:38 +01:00
|
|
|
/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
|
|
|
|
*
|
|
|
|
* The JSON document is written in a single line. It is not intended for 'human' consumption,
|
|
|
|
* but may be usefull to support feature such as RPC where bandwith is limited.
|
|
|
|
* \sa Reader, Value
|
|
|
|
*/
|
2007-03-17 23:14:59 +01:00
|
|
|
class JSON_API FastWriter : public Writer
|
2007-03-15 23:11:38 +01:00
|
|
|
{
|
|
|
|
public:
|
2007-03-17 23:14:59 +01:00
|
|
|
FastWriter();
|
|
|
|
|
|
|
|
void enableYAMLCompatibility();
|
|
|
|
|
|
|
|
public: // overridden from Writer
|
|
|
|
virtual std::string write( const Value &root );
|
2007-03-15 23:11:38 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
void writeValue( const Value &value );
|
|
|
|
|
|
|
|
std::string document_;
|
2007-03-17 23:14:59 +01:00
|
|
|
bool yamlCompatiblityEnabled_;
|
2007-03-15 23:11:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
|
|
|
|
*
|
|
|
|
* The rules for line break and indent are as follow:
|
|
|
|
* - Object value:
|
|
|
|
* - if empty then print {} without indent and line break
|
|
|
|
* - if not empty the print '{', line break & indent, print one value per line
|
|
|
|
* and then unindent and line break and print '}'.
|
|
|
|
* - Array value:
|
|
|
|
* - if empty then print [] without indent and line break
|
|
|
|
* - if the array contains no object value, empty array or some other value types,
|
|
|
|
* and all the values fit on one lines, then print the array on a single line.
|
|
|
|
* - otherwise, it the values do not fit on one line, or the array contains
|
|
|
|
* object or non empty array, then print one value per line.
|
|
|
|
*
|
|
|
|
* If the Value have comments then they are outputed according to their #CommentPlacement.
|
|
|
|
*
|
|
|
|
* \sa Reader, Value, Value::setComment()
|
|
|
|
*/
|
|
|
|
class JSON_API StyledWriter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StyledWriter();
|
2007-04-20 00:20:46 +02:00
|
|
|
virtual ~StyledWriter(){}
|
2007-03-15 23:11:38 +01:00
|
|
|
|
2007-03-17 23:14:59 +01:00
|
|
|
public: // overridden from Writer
|
2007-03-15 23:11:38 +01:00
|
|
|
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
|
|
|
|
* \param root Value to serialize.
|
2007-03-23 10:57:01 +01:00
|
|
|
* \return String containing the JSON document that represents the root value.
|
2007-03-15 23:11:38 +01:00
|
|
|
*/
|
2007-03-17 23:14:59 +01:00
|
|
|
virtual std::string write( const Value &root );
|
2007-03-15 23:11:38 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
void writeValue( const Value &value );
|
|
|
|
void writeArrayValue( const Value &value );
|
|
|
|
bool isMultineArray( const Value &value );
|
|
|
|
void pushValue( const std::string &value );
|
|
|
|
void writeIndent();
|
|
|
|
void writeWithIndent( const std::string &value );
|
|
|
|
void indent();
|
|
|
|
void unindent();
|
|
|
|
void writeCommentBeforeValue( const Value &root );
|
|
|
|
void writeCommentAfterValueOnSameLine( const Value &root );
|
|
|
|
bool hasCommentForValue( const Value &value );
|
|
|
|
static std::string normalizeEOL( const std::string &text );
|
|
|
|
|
|
|
|
typedef std::vector<std::string> ChildValues;
|
|
|
|
|
|
|
|
ChildValues childValues_;
|
|
|
|
std::string document_;
|
|
|
|
std::string indentString_;
|
|
|
|
int rightMargin_;
|
|
|
|
int indentSize_;
|
|
|
|
bool addChildValues_;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string JSON_API valueToString( Value::Int value );
|
|
|
|
std::string JSON_API valueToString( Value::UInt value );
|
|
|
|
std::string JSON_API valueToString( double value );
|
|
|
|
std::string JSON_API valueToString( bool value );
|
|
|
|
std::string JSON_API valueToQuotedString( const char *value );
|
|
|
|
|
2007-03-23 10:57:01 +01:00
|
|
|
/// \brief Output using the StyledWriter.
|
|
|
|
/// \see Json::operator>>()
|
|
|
|
std::ostream& operator<<( std::ostream&, const Value &root );
|
|
|
|
|
2007-03-15 23:11:38 +01:00
|
|
|
} // namespace Json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // JSON_WRITER_H_INCLUDED
|