1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/* Copyright (c) 2007 John W Wilkinson
This source code can be used for any purpose as long as
this comment is retained. */
// json spirit version 2.01
#include "json_spirit_writer.h"
#include "json_spirit_value.h"
#include <cassert>
#include <sstream>
#include <iomanip>
using namespace json_spirit;
using namespace std;
namespace
{
// this class solely allows its inner classes and methods to
// be conveniently templatised by Value type and dependent types
//
template< class Value_t >
struct Writer
{
typedef typename Value_t::String_type String_t;
typedef typename Value_t::Object Object_t;
typedef typename Value_t::Array Array_t;
typedef typename String_t::value_type Char_t;
typedef typename String_t::const_iterator Iter_t;
typedef Pair_impl< String_t > Pair_t;
typedef std::basic_ostream< Char_t > Ostream_t;
// this class generates the JSON text,
// it keeps track of the indentation level etc.
//
class Generator
{
public:
Generator( const Value_t& value, Ostream_t& os, bool pretty )
: os_( os )
, indentation_level_( 0 )
, pretty_( pretty )
{
output( value );
}
private:
void output( const Value_t& value )
{
switch( value.type() )
{
case obj_type: output( value.get_obj() ); break;
case array_type: output( value.get_array() ); break;
case str_type: output( value.get_str() ); break;
case bool_type: output( value.get_bool() ); break;
case int_type: os_ << value.get_int(); break;
case real_type: os_ << showpoint << setprecision( 16 )
<< value.get_real(); break;
case null_type: os_ << "null"; break;
default: assert( false );
}
}
void output( const Object_t& obj )
{
output_array_or_obj( obj, '{', '}' );
}
void output( const Array_t& arr )
{
output_array_or_obj( arr, '[', ']' );
}
void output( const Pair_t& pair )
{
output( pair.name_ ); space(); os_ << ':'; space(); output( pair.value_ );
}
void output( const String_t& s )
{
os_ << '"' << add_esc_chars( s ) << '"';
}
void output( bool b )
{
os_ << to_str( b ? "true" : "false" );
}
template< class T >
void output_array_or_obj( const T& t, Char_t start_char, Char_t end_char )
{
os_ << start_char; new_line();
++indentation_level_;
for( typename T::const_iterator i = t.begin(); i != t.end(); ++i )
{
indent(); output( *i );
if( i != t.end() - 1 )
{
os_ << ',';
}
new_line();
}
--indentation_level_;
indent(); os_ << end_char;
}
void indent()
{
if( !pretty_ ) return;
for( int i = 0; i < indentation_level_; ++i )
{
os_ << " ";
}
}
void space()
{
if( pretty_ ) os_ << ' ';
}
void new_line()
{
if( pretty_ ) os_ << '\n';
}
String_t to_str( const char* c_str )
{
return ::to_str< String_t >( c_str );
}
Char_t to_hex( Char_t c )
{
assert( c <= 0xF );
if( c < 10 ) return '0' + c;
return 'A' + c - 10;
}
String_t non_printable_to_string( Char_t c )
{
String_t result( to_str( "\\u" ) );
result.resize( 6, '0' );
for( int i = 0; i < 4; ++i )
{
result[ 5 - i ] = to_hex( c % 16 );
c >>= 4;
}
return result;
}
bool add_esc_char( Char_t c, String_t& s )
{
switch( c )
{
case '"': s += to_str( "\\\"" ); return true;
case '\\': s += to_str( "\\\\" ); return true;
case '\b': s += to_str( "\\b" ); return true;
case '\f': s += to_str( "\\f" ); return true;
case '\n': s += to_str( "\\n" ); return true;
case '\r': s += to_str( "\\r" ); return true;
case '\t': s += to_str( "\\t" ); return true;
}
return false;
}
String_t add_esc_chars( const String_t& s )
{
String_t result;
const Iter_t end( s.end() );
for( Iter_t i = s.begin(); i != end; ++i )
{
const Char_t c( *i );
if( add_esc_char( c, result ) ) continue;
if( iswprint( c ) )
{
result += c;
}
else
{
result += non_printable_to_string( c );
}
}
return result;
}
Ostream_t& os_;
int indentation_level_;
bool pretty_;
};
static void write( const Value_t& value, Ostream_t& os, bool pretty )
{
Generator( value, os, pretty );
}
static String_t write( const Value_t& value, bool pretty )
{
basic_ostringstream< Char_t > os;
write( value, os, pretty );
return os.str();
}
};
}
void json_spirit::write( const Value& value, std::ostream& os )
{
Writer< Value >::write( value, os, false );
}
void json_spirit::write_formatted( const Value& value, std::ostream& os )
{
Writer< Value >::write( value, os, true );
}
std::string json_spirit::write( const Value& value )
{
return Writer< Value >::write( value, false );
}
std::string json_spirit::write_formatted( const Value& value )
{
return Writer< Value >::write( value, true );
}
#ifndef BOOST_NO_STD_WSTRING
void json_spirit::write( const wValue& value, std::wostream& os )
{
Writer< wValue >::write( value, os, false );
}
void json_spirit::write_formatted( const wValue& value, std::wostream& os )
{
Writer< wValue >::write( value, os, true );
}
std::wstring json_spirit::write( const wValue& value )
{
return Writer< wValue >::write( value, false );
}
std::wstring json_spirit::write_formatted( const wValue& value )
{
return Writer< wValue >::write( value, true );
}
#endif
|