1 #ifndef JSON_SPIRIT_WRITER_TEMPLATE
2 #define JSON_SPIRIT_WRITER_TEMPLATE
4 // Copyright John W. Wilkinson 2007 - 2009.
5 // Distributed under the MIT License, see accompanying file LICENSE.txt
7 // json spirit version 4.03
9 #include "json_spirit_value.h"
17 inline char to_hex_char( unsigned int c )
21 const char ch = static_cast< char >( c );
23 if( ch < 10 ) return '0' + ch;
28 template< class String_type >
29 String_type non_printable_to_string( unsigned int c )
31 String_type result( 6, '\\' );
35 result[ 5 ] = to_hex_char( c & 0x000F ); c >>= 4;
36 result[ 4 ] = to_hex_char( c & 0x000F ); c >>= 4;
37 result[ 3 ] = to_hex_char( c & 0x000F ); c >>= 4;
38 result[ 2 ] = to_hex_char( c & 0x000F );
43 template< typename Char_type, class String_type >
44 bool add_esc_char( Char_type c, String_type& s )
48 case '"': s += to_str< String_type >( "\\\"" ); return true;
49 case '\\': s += to_str< String_type >( "\\\\" ); return true;
50 case '\b': s += to_str< String_type >( "\\b" ); return true;
51 case '\f': s += to_str< String_type >( "\\f" ); return true;
52 case '\n': s += to_str< String_type >( "\\n" ); return true;
53 case '\r': s += to_str< String_type >( "\\r" ); return true;
54 case '\t': s += to_str< String_type >( "\\t" ); return true;
60 template< class String_type >
61 String_type add_esc_chars( const String_type& s )
63 typedef typename String_type::const_iterator Iter_type;
64 typedef typename String_type::value_type Char_type;
68 const Iter_type end( s.end() );
70 for( Iter_type i = s.begin(); i != end; ++i )
72 const Char_type c( *i );
74 if( add_esc_char( c, result ) ) continue;
76 const wint_t unsigned_c( ( c >= 0 ) ? c : 256 + c );
78 if( iswprint( unsigned_c ) )
84 result += non_printable_to_string< String_type >( unsigned_c );
91 // this class generates the JSON text,
92 // it keeps track of the indentation level etc.
94 template< class Value_type, class Ostream_type >
97 typedef typename Value_type::Config_type Config_type;
98 typedef typename Config_type::String_type String_type;
99 typedef typename Config_type::Object_type Object_type;
100 typedef typename Config_type::Array_type Array_type;
101 typedef typename String_type::value_type Char_type;
102 typedef typename Object_type::value_type Obj_member_type;
106 Generator( const Value_type& value, Ostream_type& os, bool pretty )
108 , indentation_level_( 0 )
116 void output( const Value_type& value )
118 switch( value.type() )
120 case obj_type: output( value.get_obj() ); break;
121 case array_type: output( value.get_array() ); break;
122 case str_type: output( value.get_str() ); break;
123 case bool_type: output( value.get_bool() ); break;
124 case int_type: output_int( value ); break;
126 /// Bitcoin: Added std::fixed and changed precision from 16 to 8
127 case real_type: os_ << std::showpoint << std::fixed << std::setprecision(8)
128 << value.get_real(); break;
130 case null_type: os_ << "null"; break;
131 default: assert( false );
135 void output( const Object_type& obj )
137 output_array_or_obj( obj, '{', '}' );
140 void output( const Array_type& arr )
142 output_array_or_obj( arr, '[', ']' );
145 void output( const Obj_member_type& member )
147 output( Config_type::get_name( member ) ); space();
149 output( Config_type::get_value( member ) );
152 void output_int( const Value_type& value )
154 if( value.is_uint64() )
156 os_ << value.get_uint64();
160 os_ << value.get_int64();
164 void output( const String_type& s )
166 os_ << '"' << add_esc_chars( s ) << '"';
169 void output( bool b )
171 os_ << to_str< String_type >( b ? "true" : "false" );
175 void output_array_or_obj( const T& t, Char_type start_char, Char_type end_char )
177 os_ << start_char; new_line();
179 ++indentation_level_;
181 for( typename T::const_iterator i = t.begin(); i != t.end(); ++i )
183 indent(); output( *i );
185 typename T::const_iterator next = i;
187 if( ++next != t.end())
195 --indentation_level_;
197 indent(); os_ << end_char;
202 if( !pretty_ ) return;
204 for( int i = 0; i < indentation_level_; ++i )
212 if( pretty_ ) os_ << ' ';
217 if( pretty_ ) os_ << '\n';
220 Generator& operator=( const Generator& ); // to prevent "assignment operator could not be generated" warning
223 int indentation_level_;
227 template< class Value_type, class Ostream_type >
228 void write_stream( const Value_type& value, Ostream_type& os, bool pretty )
230 Generator< Value_type, Ostream_type >( value, os, pretty );
233 template< class Value_type >
234 typename Value_type::String_type write_string( const Value_type& value, bool pretty )
236 typedef typename Value_type::String_type::value_type Char_type;
238 std::basic_ostringstream< Char_type > os;
240 write_stream( value, os, pretty );