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 // FIXME: This comparison is always true on some platforms
77 const wint_t unsigned_c( ( c >= 0 ) ? c : 256 + c );
79 if( iswprint( unsigned_c ) )
85 result += non_printable_to_string< String_type >( unsigned_c );
92 // this class generates the JSON text,
93 // it keeps track of the indentation level etc.
95 template< class Value_type, class Ostream_type >
98 typedef typename Value_type::Config_type Config_type;
99 typedef typename Config_type::String_type String_type;
100 typedef typename Config_type::Object_type Object_type;
101 typedef typename Config_type::Array_type Array_type;
102 typedef typename String_type::value_type Char_type;
103 typedef typename Object_type::value_type Obj_member_type;
107 Generator( const Value_type& value, Ostream_type& os, bool pretty )
109 , indentation_level_( 0 )
117 void output( const Value_type& value )
119 switch( value.type() )
121 case obj_type: output( value.get_obj() ); break;
122 case array_type: output( value.get_array() ); break;
123 case str_type: output( value.get_str() ); break;
124 case bool_type: output( value.get_bool() ); break;
125 case int_type: output_int( value ); break;
127 /// Bitcoin: Added std::fixed and changed precision from 16 to 8
128 case real_type: os_ << std::showpoint << std::fixed << std::setprecision(8)
129 << value.get_real(); break;
131 case null_type: os_ << "null"; break;
132 default: assert( false );
136 void output( const Object_type& obj )
138 output_array_or_obj( obj, '{', '}' );
141 void output( const Array_type& arr )
143 output_array_or_obj( arr, '[', ']' );
146 void output( const Obj_member_type& member )
148 output( Config_type::get_name( member ) ); space();
150 output( Config_type::get_value( member ) );
153 void output_int( const Value_type& value )
155 if( value.is_uint64() )
157 os_ << value.get_uint64();
161 os_ << value.get_int64();
165 void output( const String_type& s )
167 os_ << '"' << add_esc_chars( s ) << '"';
170 void output( bool b )
172 os_ << to_str< String_type >( b ? "true" : "false" );
176 void output_array_or_obj( const T& t, Char_type start_char, Char_type end_char )
178 os_ << start_char; new_line();
180 ++indentation_level_;
182 for( typename T::const_iterator i = t.begin(); i != t.end(); ++i )
184 indent(); output( *i );
186 typename T::const_iterator next = i;
188 if( ++next != t.end())
196 --indentation_level_;
198 indent(); os_ << end_char;
203 if( !pretty_ ) return;
205 for( int i = 0; i < indentation_level_; ++i )
213 if( pretty_ ) os_ << ' ';
218 if( pretty_ ) os_ << '\n';
221 Generator& operator=( const Generator& ); // to prevent "assignment operator could not be generated" warning
224 int indentation_level_;
228 template< class Value_type, class Ostream_type >
229 void write_stream( const Value_type& value, Ostream_type& os, bool pretty )
231 Generator< Value_type, Ostream_type >( value, os, pretty );
234 template< class Value_type >
235 typename Value_type::String_type write_string( const Value_type& value, bool pretty )
237 typedef typename Value_type::String_type::value_type Char_type;
239 std::basic_ostringstream< Char_type > os;
241 write_stream( value, os, pretty );