Remove unused includes.
[novacoin.git] / src / json / json_spirit_writer_template.h
1 #ifndef JSON_SPIRIT_WRITER_TEMPLATE
2 #define JSON_SPIRIT_WRITER_TEMPLATE
3
4 //          Copyright John W. Wilkinson 2007 - 2009.
5 // Distributed under the MIT License, see accompanying file LICENSE.txt
6
7 // json spirit version 4.03
8
9 #include "json_spirit_value.h"
10
11 #include <cassert>
12 #include <sstream>
13 #include <iomanip>
14
15 namespace json_spirit
16 {
17     inline char to_hex_char( unsigned int c )
18     {
19         assert( c <= 0xF );
20
21         const char ch = static_cast< char >( c );
22
23         if( ch < 10 ) return '0' + ch;
24
25         return 'A' - 10 + ch;
26     }
27
28     template< class String_type >
29     String_type non_printable_to_string( unsigned int c )
30     {
31         String_type result( 6, '\\' );
32
33         result[1] = 'u';
34
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 );
39
40         return result;
41     }
42
43     template< typename Char_type, class String_type >
44     bool add_esc_char( Char_type c, String_type& s )
45     {
46         switch( c )
47         {
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;
55         }
56
57         return false;
58     }
59
60     template< class String_type >
61     String_type add_esc_chars( const String_type& s )
62     {
63         typedef typename String_type::const_iterator Iter_type;
64         typedef typename String_type::value_type     Char_type;
65
66         String_type result;
67
68         const Iter_type end( s.end() );
69
70         for( Iter_type i = s.begin(); i != end; ++i )
71         {
72             const Char_type c( *i );
73
74             if( add_esc_char( c, result ) ) continue;
75
76             // FIXME: This comparison is always true on some platforms
77             const wint_t unsigned_c( ( c >= 0 ) ? c : 256 + c );
78
79             if( iswprint( unsigned_c ) )
80             {
81                 result += c;
82             }
83             else
84             {
85                 result += non_printable_to_string< String_type >( unsigned_c );
86             }
87         }
88
89         return result;
90     }
91
92     // this class generates the JSON text,
93     // it keeps track of the indentation level etc.
94     //
95     template< class Value_type, class Ostream_type >
96     class Generator
97     {
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;
104
105     public:
106
107         Generator( const Value_type& value, Ostream_type& os, bool pretty )
108         :   os_( os )
109         ,   indentation_level_( 0 )
110         ,   pretty_( pretty )
111         {
112             output( value );
113         }
114
115     private:
116
117         void output( const Value_type& value )
118         {
119             switch( value.type() )
120             {
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;
126
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;
130
131                 case null_type:  os_ << "null";               break;
132                 default: assert( false );
133             }
134         }
135
136         void output( const Object_type& obj )
137         {
138             output_array_or_obj( obj, '{', '}' );
139         }
140
141         void output( const Array_type& arr )
142         {
143             output_array_or_obj( arr, '[', ']' );
144         }
145
146         void output( const Obj_member_type& member )
147         {
148             output( Config_type::get_name( member ) ); space(); 
149             os_ << ':'; space(); 
150             output( Config_type::get_value( member ) );
151         }
152
153         void output_int( const Value_type& value )
154         {
155             if( value.is_uint64() )
156             {
157                 os_ << value.get_uint64();
158             }
159             else
160             {
161                os_ << value.get_int64();
162             }
163         }
164
165         void output( const String_type& s )
166         {
167             os_ << '"' << add_esc_chars( s ) << '"';
168         }
169
170         void output( bool b )
171         {
172             os_ << to_str< String_type >( b ? "true" : "false" );
173         }
174
175         template< class T >
176         void output_array_or_obj( const T& t, Char_type start_char, Char_type end_char )
177         {
178             os_ << start_char; new_line();
179
180             ++indentation_level_;
181             
182             for( typename T::const_iterator i = t.begin(); i != t.end(); ++i )
183             {
184                 indent(); output( *i );
185
186                 typename T::const_iterator next = i;
187
188                 if( ++next != t.end())
189                 {
190                     os_ << ',';
191                 }
192
193                 new_line();
194             }
195
196             --indentation_level_;
197
198             indent(); os_ << end_char;
199         }
200         
201         void indent()
202         {
203             if( !pretty_ ) return;
204
205             for( int i = 0; i < indentation_level_; ++i )
206             { 
207                 os_ << "    ";
208             }
209         }
210
211         void space()
212         {
213             if( pretty_ ) os_ << ' ';
214         }
215
216         void new_line()
217         {
218             if( pretty_ ) os_ << '\n';
219         }
220
221         Generator& operator=( const Generator& ); // to prevent "assignment operator could not be generated" warning
222
223         Ostream_type& os_;
224         int indentation_level_;
225         bool pretty_;
226     };
227
228     template< class Value_type, class Ostream_type >
229     void write_stream( const Value_type& value, Ostream_type& os, bool pretty )
230     {
231         Generator< Value_type, Ostream_type >( value, os, pretty );
232     }
233
234     template< class Value_type >
235     typename Value_type::String_type write_string( const Value_type& value, bool pretty )
236     {
237         typedef typename Value_type::String_type::value_type Char_type;
238
239         std::basic_ostringstream< Char_type > os;
240
241         write_stream( value, os, pretty );
242
243         return os.str();
244     }
245 }
246
247 #endif