--- /dev/null
+#ifndef JSON_SPIRIT_WRITER_TEMPLATE\r
+#define JSON_SPIRIT_WRITER_TEMPLATE\r
+\r
+// Copyright John W. Wilkinson 2007 - 2011\r
+// Distributed under the MIT License, see accompanying file LICENSE.txt\r
+\r
+// json spirit version 4.05\r
+\r
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)\r
+# pragma once\r
+#endif\r
+\r
+#include "json_spirit_value.h"\r
+#include "json_spirit_writer_options.h"\r
+\r
+#include <cassert>\r
+#include <sstream>\r
+#include <iomanip>\r
+#include <boost/io/ios_state.hpp>\r
+\r
+namespace json_spirit\r
+{\r
+ inline char to_hex_char( unsigned int c )\r
+ {\r
+ assert( c <= 0xF );\r
+\r
+ const char ch = static_cast< char >( c );\r
+\r
+ if( ch < 10 ) return '0' + ch;\r
+\r
+ return 'A' - 10 + ch;\r
+ }\r
+\r
+ template< class String_type >\r
+ String_type non_printable_to_string( unsigned int c )\r
+ {\r
+ String_type result( 6, '\\' );\r
+\r
+ result[1] = 'u';\r
+\r
+ result[ 5 ] = to_hex_char( c & 0x000F ); c >>= 4;\r
+ result[ 4 ] = to_hex_char( c & 0x000F ); c >>= 4;\r
+ result[ 3 ] = to_hex_char( c & 0x000F ); c >>= 4;\r
+ result[ 2 ] = to_hex_char( c & 0x000F );\r
+\r
+ return result;\r
+ }\r
+\r
+ template< typename Char_type, class String_type >\r
+ bool add_esc_char( Char_type c, String_type& s )\r
+ {\r
+ switch( c )\r
+ {\r
+ case '"': s += to_str< String_type >( "\\\"" ); return true;\r
+ case '\\': s += to_str< String_type >( "\\\\" ); return true;\r
+ case '\b': s += to_str< String_type >( "\\b" ); return true;\r
+ case '\f': s += to_str< String_type >( "\\f" ); return true;\r
+ case '\n': s += to_str< String_type >( "\\n" ); return true;\r
+ case '\r': s += to_str< String_type >( "\\r" ); return true;\r
+ case '\t': s += to_str< String_type >( "\\t" ); return true;\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ template< class String_type >\r
+ String_type add_esc_chars( const String_type& s, bool raw_utf8 )\r
+ {\r
+ typedef typename String_type::const_iterator Iter_type;\r
+ typedef typename String_type::value_type Char_type;\r
+\r
+ String_type result;\r
+\r
+ const Iter_type end( s.end() );\r
+\r
+ for( Iter_type i = s.begin(); i != end; ++i )\r
+ {\r
+ const Char_type c( *i );\r
+\r
+ if( add_esc_char( c, result ) ) continue;\r
+\r
+ if( raw_utf8 )\r
+ {\r
+ result += c;\r
+ }\r
+ else\r
+ {\r
+ const wint_t unsigned_c( ( c >= 0 ) ? c : 256 + c );\r
+\r
+ if( iswprint( unsigned_c ) )\r
+ {\r
+ result += c;\r
+ }\r
+ else\r
+ {\r
+ result += non_printable_to_string< String_type >( unsigned_c );\r
+ }\r
+ }\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ template< class Ostream >\r
+ void append_double( Ostream& os, const double d, const int precision )\r
+ {\r
+ os << std::showpoint << std::setprecision( precision ) << d;\r
+ }\r
+\r
+ template< class String_type >\r
+ void erase_and_extract_exponent( String_type& str, String_type& exp )\r
+ {\r
+ const typename String_type::size_type exp_start= str.find( 'e' );\r
+\r
+ if( exp_start != String_type::npos )\r
+ {\r
+ exp = str.substr( exp_start );\r
+ str.erase( exp_start );\r
+ }\r
+ }\r
+\r
+ template< class String_type >\r
+ typename String_type::size_type find_first_non_zero( const String_type& str )\r
+ {\r
+ typename String_type::size_type result = str.size() - 1;\r
+\r
+ for( ; result != 0; --result )\r
+ {\r
+ if( str[ result ] != '0' )\r
+ {\r
+ break;\r
+ }\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ template< class String_type >\r
+ void remove_trailing( String_type& str )\r
+ {\r
+ String_type exp;\r
+\r
+ erase_and_extract_exponent( str, exp );\r
+\r
+ const typename String_type::size_type first_non_zero = find_first_non_zero( str );\r
+\r
+ if( first_non_zero != 0 )\r
+ {\r
+ const int offset = str[first_non_zero] == '.' ? 2 : 1; // note zero digits following a decimal point is non standard\r
+ str.erase( first_non_zero + offset );\r
+ }\r
+\r
+ str += exp;\r
+ }\r
+\r
+ // this class generates the JSON text,\r
+ // it keeps track of the indentation level etc.\r
+ //\r
+ template< class Value_type, class Ostream_type >\r
+ class Generator\r
+ {\r
+ typedef typename Value_type::Config_type Config_type;\r
+ typedef typename Config_type::String_type String_type;\r
+ typedef typename Config_type::Object_type Object_type;\r
+ typedef typename Config_type::Array_type Array_type;\r
+ typedef typename String_type::value_type Char_type;\r
+ typedef typename Object_type::value_type Obj_member_type;\r
+\r
+ public:\r
+\r
+ Generator( const Value_type& value, Ostream_type& os, unsigned int options )\r
+ : os_( os )\r
+ , indentation_level_( 0 )\r
+ , pretty_( ( options & pretty_print ) != 0 || ( options & single_line_arrays ) != 0 )\r
+ , raw_utf8_( ( options & raw_utf8 ) != 0 )\r
+ , remove_trailing_zeros_( ( options & remove_trailing_zeros ) != 0 )\r
+ , single_line_arrays_( ( options & single_line_arrays ) != 0 )\r
+ , ios_saver_( os )\r
+ {\r
+ output( value );\r
+ }\r
+\r
+ private:\r
+\r
+ void output( const Value_type& value )\r
+ {\r
+ switch( value.type() )\r
+ {\r
+ case obj_type: output( value.get_obj() ); break;\r
+ case array_type: output( value.get_array() ); break;\r
+ case str_type: output( value.get_str() ); break;\r
+ case bool_type: output( value.get_bool() ); break;\r
+ case real_type: output( value.get_real() ); break;\r
+ case int_type: output_int( value ); break;\r
+ case null_type: os_ << "null"; break;\r
+ default: assert( false );\r
+ }\r
+ }\r
+\r
+ void output( const Object_type& obj )\r
+ {\r
+ output_array_or_obj( obj, '{', '}' );\r
+ }\r
+\r
+ void output( const Obj_member_type& member )\r
+ {\r
+ output( Config_type::get_name( member ) ); space(); \r
+ os_ << ':'; space(); \r
+ output( Config_type::get_value( member ) );\r
+ }\r
+\r
+ void output_int( const Value_type& value )\r
+ {\r
+ if( value.is_uint64() )\r
+ {\r
+ os_ << value.get_uint64();\r
+ }\r
+ else\r
+ {\r
+ os_ << value.get_int64();\r
+ }\r
+ }\r
+\r
+ void output( const String_type& s )\r
+ {\r
+ os_ << '"' << add_esc_chars( s, raw_utf8_ ) << '"';\r
+ }\r
+\r
+ void output( bool b )\r
+ {\r
+ os_ << to_str< String_type >( b ? "true" : "false" );\r
+ }\r
+\r
+ void output( double d )\r
+ {\r
+ if( remove_trailing_zeros_ )\r
+ {\r
+ std::basic_ostringstream< Char_type > os;\r
+\r
+ append_double( os, d, 16 ); // note precision is 16 so that we get some trailing space that we can remove,\r
+ // otherwise, 0.1234 gets converted to "0.12399999..."\r
+\r
+ String_type str = os.str();\r
+\r
+ remove_trailing( str );\r
+\r
+ os_ << str;\r
+ }\r
+ else\r
+ {\r
+ append_double( os_, d, 17 );\r
+ }\r
+ }\r
+\r
+ static bool contains_composite_elements( const Array_type& arr )\r
+ {\r
+ for( typename Array_type::const_iterator i = arr.begin(); i != arr.end(); ++i )\r
+ {\r
+ const Value_type& val = *i;\r
+\r
+ if( val.type() == obj_type ||\r
+ val.type() == array_type )\r
+ {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ template< class Iter >\r
+ void output_composite_item( Iter i, Iter last )\r
+ {\r
+ output( *i );\r
+\r
+ if( ++i != last )\r
+ {\r
+ os_ << ',';\r
+ }\r
+ }\r
+\r
+ void output( const Array_type& arr )\r
+ {\r
+ if( single_line_arrays_ && !contains_composite_elements( arr ) )\r
+ {\r
+ os_ << '['; space();\r
+ \r
+ for( typename Array_type::const_iterator i = arr.begin(); i != arr.end(); ++i )\r
+ {\r
+ output_composite_item( i, arr.end() );\r
+\r
+ space();\r
+ }\r
+\r
+ os_ << ']';\r
+ }\r
+ else\r
+ {\r
+ output_array_or_obj( arr, '[', ']' );\r
+ }\r
+ }\r
+\r
+ template< class T >\r
+ void output_array_or_obj( const T& t, Char_type start_char, Char_type end_char )\r
+ {\r
+ os_ << start_char; new_line();\r
+\r
+ ++indentation_level_;\r
+ \r
+ for( typename T::const_iterator i = t.begin(); i != t.end(); ++i )\r
+ {\r
+ indent();\r
+\r
+ output_composite_item( i, t.end() );\r
+\r
+ new_line();\r
+ }\r
+\r
+ --indentation_level_;\r
+\r
+ indent(); os_ << end_char;\r
+ }\r
+ \r
+ void indent()\r
+ {\r
+ if( !pretty_ ) return;\r
+\r
+ for( int i = 0; i < indentation_level_; ++i )\r
+ { \r
+ os_ << " ";\r
+ }\r
+ }\r
+\r
+ void space()\r
+ {\r
+ if( pretty_ ) os_ << ' ';\r
+ }\r
+\r
+ void new_line()\r
+ {\r
+ if( pretty_ ) os_ << '\n';\r
+ }\r
+\r
+ Generator& operator=( const Generator& ); // to prevent "assignment operator could not be generated" warning\r
+\r
+ Ostream_type& os_;\r
+ int indentation_level_;\r
+ bool pretty_;\r
+ bool raw_utf8_;\r
+ bool remove_trailing_zeros_;\r
+ bool single_line_arrays_;\r
+ boost::io::basic_ios_all_saver< Char_type > ios_saver_; // so that ostream state is reset after control is returned to the caller\r
+ };\r
+\r
+ // writes JSON Value to a stream, e.g.\r
+ //\r
+ // write_stream( value, os, pretty_print );\r
+ //\r
+ template< class Value_type, class Ostream_type >\r
+ void write_stream( const Value_type& value, Ostream_type& os, unsigned int options = 0 )\r
+ {\r
+ os << std::dec;\r
+ Generator< Value_type, Ostream_type >( value, os, options );\r
+ }\r
+\r
+ // writes JSON Value to a stream, e.g.\r
+ //\r
+ // const string json_str = write( value, pretty_print );\r
+ //\r
+ template< class Value_type >\r
+ typename Value_type::String_type write_string( const Value_type& value, unsigned int options = 0 )\r
+ {\r
+ typedef typename Value_type::String_type::value_type Char_type;\r
+\r
+ std::basic_ostringstream< Char_type > os;\r
+\r
+ write_stream( value, os, options );\r
+\r
+ return os.str();\r
+ }\r
+}\r
+\r
+#endif\r