Hi John,
I did implement the read_value functions by just choosing the root rule depending on which of the functions was used
>
> const rule< ScannerT >& start() const
> { return self_.supportValues_ ? json_value : json_; }
>
About the [1 2] bug, its pretty obvious to me that
*elements_
matches to any sequence of elements, regardless of a comma as separator,
while
!elements_ only just allows the empty sequence.
Just noted that this issue is not fixed in the version below. When looking at it the object definition has the same bug, I think it has to be !members_ and not *members_, but I did not try to reproduce this case.
Daniel
#include "json_spirit_reader.h"
#include "json_spirit_value.h"
#include
#include
#include
#include
#include
#include
#include <assert.h>
using namespace json_spirit;
using namespace std;
using namespace boost;
using namespace boost::spirit;
namespace
{
const int_parser< int64_t > int64_p = int_parser< int64_t >();
template< class Value_t >
struct Reader
{
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_istream< Char_t > Istream_t;
class Semantic_actions
{
public:
Semantic_actions( Value_t& value )
: value_( value )
, current_p_( 0 )
{
}
void begin_obj( Char_t c )
{
assert( c == '{' );
begin_compound< Object_t >();
}
void end_obj( Char_t c )
{
assert( c == '}' );
end_compound();
}
void begin_array( Char_t c )
{
assert( c == '[' );
begin_compound< Array_t >();
}
void end_array( Char_t c )
{
assert( c == ']' );
end_compound();
}
void new_name( Iter_t str, Iter_t end )
{
assert( current_p_->type() == obj_type );
name_ = get_str( str, end );
}
void new_str( Iter_t str, Iter_t end )
{
add_to_current( get_str( str, end ) );
}
void new_true( Iter_t str, Iter_t end )
{
assert( is_eq( str, end, "true" ) );
add_to_current( true );
}
void new_false( Iter_t str, Iter_t end )
{
assert( is_eq( str, end, "false" ) );
add_to_current( false );
}
void new_null( Iter_t str, Iter_t end )
{
assert( is_eq( str, end, "null" ) );
add_to_current( Value_t() );
}
void new_int( int64_t i )
{
add_to_current( i );
}
void new_real( double d )
{
add_to_current( d );
}
private:
template< class Array_or_obj >
void begin_compound()
{
if( current_p_ == 0 )
{
value_ = Array_or_obj();
current_p_ = &value_;
}
else
{
stack_.push_back( current_p_ );
Array_or_obj new_array_or_obj;
add_to_current( new_array_or_obj );
if( current_p_->type() == array_type )
{
current_p_ = ¤t_p_->get_array().back();
}
else
{
current_p_ = ¤t_p_->get_obj().back().value_;
}
}
}
void end_compound()
{
if( current_p_ != &value_ )
{
current_p_ = stack_.back();
stack_.pop_back();
}
}
void add_to_current( const Value_t& value )
{
if (!current_p_)
{
value_ = Value_t(value);
current_p_ = &value_;
}
else if( current_p_->type() == array_type )
{
current_p_->get_array().push_back( value );
}
else if( current_p_->type() == obj_type )
{
current_p_->get_obj().push_back( Pair_t( name_, value ) );
}
else
{
assert(false);
}
}
Value_t& value_;
Value_t* current_p_;
vector< Value_t* > stack_;
String_t name_;
};
class Json_grammer : public grammar< Json_grammer >
{
public:
Json_grammer( Semantic_actions& semantic_actions, bool supportValues )
: actions_( semantic_actions ), supportValues_(supportValues)
{
}
template< typename ScannerT >
struct definition
{
definition( const Json_grammer& self ) : self_(self)
{
typedef function< void( Char_t ) > Char_action;
typedef function< void( Iter_t, Iter_t ) > Str_action;
typedef function< void( double ) > Real_action;
typedef function< void( int64_t ) > Int_action;
Char_action begin_obj ( bind( &Semantic_actions::begin_obj, &self.actions_, _1 ) );
Char_action end_obj ( bind( &Semantic_actions::end_obj, &self.actions_, _1 ) );
Char_action begin_array( bind( &Semantic_actions::begin_array, &self.actions_, _1 ) );
Char_action end_array ( bind( &Semantic_actions::end_array, &self.actions_, _1 ) );
Str_action new_name ( bind( &Semantic_actions::new_name, &self.actions_, _1, _2 ) );
Str_action new_str ( bind( &Semantic_actions::new_str, &self.actions_, _1, _2 ) );
Str_action new_true ( bind( &Semantic_actions::new_true, &self.actions_, _1, _2 ) );
Str_action new_false ( bind( &Semantic_actions::new_false, &self.actions_, _1, _2 ) );
Str_action new_null ( bind( &Semantic_actions::new_null, &self.actions_, _1, _2 ) );
Real_action new_real ( bind( &Semantic_actions::new_real, &self.actions_, _1 ) );
Int_action new_int ( bind( &Semantic_actions::new_int, &self.actions_, _1 ) );
json_ = ( object_ | array_ ) >> end_p;
json_value = ( value_ ) >> end_p;
object_
= confix_p
(
ch_p('{')[ begin_obj ],
*members_,
ch_p('}')[ end_obj ]
)
;
members_
= pair_ >> *( ',' >> pair_ )
;
pair_
= string_[ new_name ]
>> ':'
>> value_
;
value_
= string_[ new_str ]
| number_
| object_
| array_
| str_p( "true" ) [ new_true ]
| str_p( "false" )[ new_false ]
| str_p( "null" ) [ new_null ]
;
array_
= confix_p
(
ch_p('[')[ begin_array ],
*list_p( elements_, ',' ),
ch_p(']')[ end_array ]
)
;
elements_
= value_ >> *( ',' >> value_ )
;
string_
= lexeme_d
[
confix_p
(
'"',
*lex_escape_ch_p,
'"'
)
]
;
number_
= strict_real_p[ new_real ]
| int64_p [ new_int ]
;
}
rule< ScannerT > json_, json_value, object_, members_, pair_, array_, elements_, value_, string_, number_;
const rule< ScannerT >& start() const { return self_.supportValues_ ? json_value : json_; }
const Json_grammer& self_;
};
Semantic_actions& actions_;
const bool supportValues_;
};
static bool is_eq( Iter_t str, Iter_t end, const char* c_str )
{
const String_t s1( str, end );
const string s2( c_str );
if( s1.length() != s2.length() ) return false;
for( typename String_t::size_type i = 0; i < s1.length(); ++i )
{
if( s1[i] != s2[i] ) return false;
}
return true;
}
static Char_t hex_to_num( const Char_t c )
{
if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
return 0;
}
static Char_t hex_str_to_char( Iter_t& str )
{
const Char_t c1( *( ++str ) );
const Char_t c2( *( ++str ) );
return hex_to_num( c1 ) * 0x10 + hex_to_num( c2 );
}
static Char_t unicode_str_to_char( Iter_t& str )
{
const Char_t c1( *( ++str ) );
const Char_t c2( *( ++str ) );
const Char_t c3( *( ++str ) );
const Char_t c4( *( ++str ) );
return hex_to_num( c1 ) * 0x1000 + hex_to_num( c2 ) * 0x100 + hex_to_num( c3 ) * 0x10 + hex_to_num( c4 );
}
static String_t substitute_esc_chars( Iter_t str, Iter_t end )
{
if( end - str < 2 ) return String_t( str, end );
String_t result;
Iter_t end_minus_1( end - 1 );
for( Iter_t i = str; i < end; ++i )
{
const Char_t c1( *i );
if( ( c1 == '\\' ) && ( i != end_minus_1 ) )
{
++i;
const Char_t c2( *i );
switch( c2 )
{
case 't': result += '\t'; break;
case 'b': result += '\b'; break;
case 'f': result += '\f'; break;
case 'n': result += '\n'; break;
case 'r': result += '\r'; break;
case '\\': result += '\\'; break;
case '/': result += '/'; break;
case '"': result += '"'; break;
case 'x':
{
if( end - i >= 3 )
{
result += hex_str_to_char( i );
}
break;
}
case 'u':
{
if( end - i >= 5 )
{
result += unicode_str_to_char( i );
}
break;
}
}
}
else
{
result += c1;
}
}
return result;
}
static String_t get_str( Iter_t str, Iter_t end )
{
assert( end - str >= 2 );
Iter_t str_without_quotes( str + 1 );
Iter_t end_without_quotes( end - 1 );
return substitute_esc_chars( str_without_quotes, end_without_quotes );
}
static void stream_to_string( Istream_t& is, String_t& s )
{
typedef istream_iterator< Char_t, Char_t > istream_iter;
is.unsetf( ios::skipws );
copy( istream_iter( is ), istream_iter(), back_inserter( s ) );
}
static bool read_string( const String_t& s, Value_t& value, bool supportValues = false )
{
Semantic_actions semantic_actions( value );
parse_info< Iter_t > info = parse( s.begin(), s.end(),
Json_grammer( semantic_actions, supportValues ),
space_p );
return info.full;
}
static bool read_stream( Istream_t& is, Value_t& value, bool supportValues = false )
{
String_t s;
stream_to_string( is, s );
return read_string( s, value, supportValues );
}
};
}
bool json_spirit::read( const std::string& s, Value& value )
{
return Reader< Value >::read_string( s, value );
}
bool json_spirit::read( std::istream& is, Value& value )
{
return Reader< Value >::read_stream( is, value );
}
bool json_spirit::read_value( const std::string& s, Value& value )
{
return Reader< Value >::read_string( s, value, true );
}
bool json_spirit::read_value( std::istream& is, Value& value )
{
return Reader< Value >::read_stream( is, value, true );
}
#ifndef BOOST_NO_STD_WSTRING
bool json_spirit::read( const std::wstring& s, wValue& value )
{
return Reader< wValue >::read_string( s, value );
}
bool json_spirit::read( std::wistream& is, wValue& value )
{
return Reader< wValue >::read_stream( is, value );
}
bool json_spirit::read_value( const std::wstring& s, wValue& value )
{
return Reader< wValue >::read_string( s, value, true );
}
bool json_spirit::read_value( std::wistream& is, wValue& value )
{
return Reader< wValue >::read_stream( is, value, true);
}
#endif
</assert.h>
#ifndef JASON_SPIRIT_READER
#define JASON_SPIRIT_READER
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#include "json_spirit_value.h"
#include <iostream>
namespace json_spirit
{
bool read( const std::string& s, Value& value );
bool read( std::istream& is, Value& value );
bool read_value( const std::string& s, Value& value );
bool read_value( std::istream& is, Value& value );
#ifndef BOOST_NO_STD_WSTRING
bool read( const std::wstring& s, wValue& value );
bool read( std::wistream& is, wValue& value );
bool read_value( const std::wstring& s, wValue& value );
bool read_value( std::wistream& is, wValue& value );
#endif
}
#endif
</iostream>
#include "json_spirit_reader_test.h"
#include "json_spirit_reader.h"
#include "json_spirit_value.h"
#include "json_spirit_writer.h"
#include "Utils_test.h"
#include <sstream>
#include
#include
#include
using namespace json_spirit;
using namespace std;
using namespace boost;
using namespace boost::assign;
namespace
{
template< class Value_t >
struct Test_runner
{
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_istream< Char_t > Istream_t;
String_t to_str( const char* c_str )
{
return ::to_str< String_t >( c_str );
}
Pair_t make_pair( const char* c_name, const char* c_value )
{
return Pair_t( to_str( c_name ), to_str( c_value ) );
}
Pair_t p1;
Pair_t p2;
Pair_t p3;
Test_runner()
: p1( make_pair("name 1", "value 1") )
, p2( make_pair("name 2", "value 2") )
, p3( make_pair("name 3", "value 3") )
{
}
void check_eq( const Object_t& obj_1, const Object_t& obj_2 )
{
const typename Object_t::size_type size( obj_1.size() );
assert_eq( size, obj_2.size() );
for( typename Object_t::size_type i = 0; i < size; ++i )
{
assert_eq( obj_1[i], obj_2[i] );
}
}
void test_syntax( const char* c_str )
{
Value_t value;
const bool success = read( to_str( c_str ), value );
assert_eq( success, true );
}
template< typename Int >
void test_syntax( Int min_int, Int max_int )
{
ostringstream os;
os << "[" << min_int << "," << max_int << "]";
test_syntax( os.str().c_str() );
}
void test_syntax()
{
test_syntax( "{}" );
test_syntax( "{ }" );
test_syntax( "{\"\":\"\"}" );
test_syntax( "{\"test\":\"123\"}" );
test_syntax( "{\"test\" : \"123\"}" );
test_syntax( "{\"testing testing testing\":\"123\"}" );
test_syntax( "{\"\":\"abc\"}" );
test_syntax( "{\"abc\":\"\"}" );
test_syntax( "{\"\":\"\"}" );
test_syntax( "{\"test\":true}" );
test_syntax( "{\"test\":false}" );
test_syntax( "{\"test\":null}" );
test_syntax( "{\"test1\":\"123\",\"test2\":\"456\"}" );
test_syntax( "{\"test1\":\"123\",\"test2\":\"456\",\"test3\":\"789\"}" );
test_syntax( "{\"test1\":{\"test2\":\"123\",\"test3\":\"456\"}}" );
test_syntax( "{\"test1\":{\"test2\":{\"test3\":\"456\"}}}" );
test_syntax( "{\"test1\":[\"a\",\"bb\",\"cc\"]}" );
test_syntax( "{\"test1\":[true,false,null]}" );
test_syntax( "{\"test1\":[true,\"abc\",{\"a\":\"b\"},{\"d\":false},null]}" );
test_syntax( "{\"test1\":[1,2,-3]}" );
test_syntax( "{\"test1\":[1.1,2e4,-1.234e-34]}" );
test_syntax( "{\n"
"\t\"test1\":\n"
"\t\t{\n"
"\t\t\t\"test2\":\"123\",\n"
"\t\t\t\"test3\":\"456\"\n"
"\t\t}\n"
"}\n" );
test_syntax( "[]" );
test_syntax( "[ ]" );
test_syntax( "[1,2,3]" );
test_syntax( "[ 1, -2, 3]" );
test_syntax( "[ 1.2, -2e6, -3e-6 ]" );
test_syntax( "[ 1.2, \"str\", -3e-6, { \"field\" : \"data\" } ]" );
test_syntax( INT_MIN, INT_MAX );
test_syntax( LLONG_MIN, LLONG_MAX );
}
void check_read( const char* c_str, Value_t& value )
{
const bool success = read( to_str( c_str ), value );
assert_eq( success, true );
}
void check_reading( const char* c_str )
{
Value_t value;
String_t in_s( to_str( c_str ) );
const bool success = read( in_s, value );
assert_eq( success, true );
const String_t result = write_formatted( value );
assert_eq( in_s, result );
}
template< typename Int >
void check_reading( Int min_int, Int max_int )
{
ostringstream os;
os << "[\n"
" " << min_int << ",\n"
" " << max_int << "\n"
"]";
check_reading( os.str().c_str() );
}
void test_reading()
{
check_reading( "{\n}" );
Value_t value;
check_read( "{\n"
" \"name 1\" : \"value 1\"\n"
"}", value );
check_eq( value.get_obj(), list_of( p1 ) );
check_read( "{\"name 1\":\"value 1\",\"name 2\":\"value 2\"}", value );
check_eq( value.get_obj(), list_of( p1 )( p2 ) );
check_read( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : \"value 2\",\n"
" \"name 3\" : \"value 3\"\n"
"}", value );
check_eq( value.get_obj(), list_of( p1 )( p2 )( p3 ) );
check_read( "{\n"
" \"\" : \"value\",\n"
" \"name\" : \"\"\n"
"}", value );
check_eq( value.get_obj(), list_of( make_pair( "", "value" ) )( make_pair( "name", "" ) ) );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : {\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : \"value_4\"\n"
" }\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : {\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : \"value_4\",\n"
" \"name_5\" : {\n"
" \"name_6\" : \"value_6\",\n"
" \"name_7\" : \"value_7\"\n"
" }\n"
" }\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : {\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : {\n"
" \"name_5\" : \"value_5\",\n"
" \"name_6\" : \"value_6\"\n"
" },\n"
" \"name_7\" : \"value_7\"\n"
" }\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : {\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : {\n"
" \"name_5\" : \"value_5\",\n"
" \"name_6\" : \"value_6\"\n"
" },\n"
" \"name_7\" : \"value_7\"\n"
" },\n"
" \"name_8\" : \"value_8\",\n"
" \"name_9\" : {\n"
" \"name_10\" : \"value_10\"\n"
" }\n"
"}" );
check_reading( "{\n"
" \"name 1\" : {\n"
" \"name 2\" : {\n"
" \"name 3\" : {\n"
" \"name_4\" : {\n"
" \"name_5\" : \"value\"\n"
" }\n"
" }\n"
" }\n"
" }\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : true,\n"
" \"name 3\" : false,\n"
" \"name_4\" : \"value_4\",\n"
" \"name_5\" : true\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : null,\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : null\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : 123,\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : -567\n"
"}" );
check_reading( "{\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : 1.200000000000000,\n"
" \"name 3\" : \"value 3\",\n"
" \"name_4\" : 1.234567890123456e+025,\n"
" \"name_5\" : -1.234000000000000e-123,\n"
" \"name_6\" : 1.000000000000000e-123,\n"
" \"name_7\" : 1234567890.123456\n"
"}" );
check_reading( "[\n]" );
check_reading( "[\n"
" 1\n"
"]" );
check_reading( "[\n"
" 1,\n"
" 1.200000000000000,\n"
" \"john\",\n"
" true,\n"
" false,\n"
" null\n"
"]" );
check_reading( "[\n"
" 1,\n"
" [\n"
" 2,\n"
" 3\n"
" ]\n"
"]" );
check_reading( "[\n"
" 1,\n"
" [\n"
" 2,\n"
" 3\n"
" ],\n"
" [\n"
" 4,\n"
" [\n"
" 5,\n"
" 6,\n"
" 7\n"
" ]\n"
" ]\n"
"]" );
check_reading( "[\n"
" {\n"
" \"name\" : \"value\"\n"
" }\n"
"]" );
check_reading( "{\n"
" \"name\" : [\n"
" 1\n"
" ]\n"
"}" );
check_reading( "[\n"
" {\n"
" \"name 1\" : \"value\",\n"
" \"name 2\" : [\n"
" 1,\n"
" 2,\n"
" 3\n"
" ]\n"
" }\n"
"]" );
check_reading( "{\n"
" \"name 1\" : [\n"
" 1,\n"
" {\n"
" \"name 2\" : \"value 2\"\n"
" }\n"
" ]\n"
"}" );
check_reading( "[\n"
" {\n"
" \"name 1\" : \"value 1\",\n"
" \"name 2\" : [\n"
" 1,\n"
" 2,\n"
" {\n"
" \"name 3\" : \"value 3\"\n"
" }\n"
" ]\n"
" }\n"
"]" );
check_reading( "{\n"
" \"name 1\" : [\n"
" 1,\n"
" {\n"
" \"name 2\" : [\n"
" 1,\n"
" 2,\n"
" 3\n"
" ]\n"
" }\n"
" ]\n"
"}" );
check_reading( INT_MIN, INT_MAX );
check_reading( LLONG_MIN, LLONG_MAX );
}
void test_from_stream()
{
Value_t value;
String_t in_s( to_str( "[1,2]" ) );
basic_istringstream< Char_t > is( in_s );
const bool success = read( is, value );
assert_eq( success, true );
assert_eq( in_s, write( value ) );
}
void test_escape_chars( const char* json_str, const char* c_str )
{
Value_t value;
string s( string( "{\"" ) + json_str + "\" : \"" + json_str + "\"} " );
check_read( s.c_str(), value );
const Pair_t& pair( value.get_obj()[0] );
assert_eq( pair.name_, to_str( c_str ) );
assert_eq( pair.value_, to_str( c_str ) );
}
void test_escape_chars()
{
test_escape_chars( "\\t", "\t");
test_escape_chars( "a\\t", "a\t" );
test_escape_chars( "\\tb", "\tb" );
test_escape_chars( "a\\tb", "a\tb" );
test_escape_chars( "a\\tb", "a\tb" );
test_escape_chars( "a123\\tb", "a123\tb" );
test_escape_chars( "\\t\\n\\\\", "\t\n\\" );
test_escape_chars( "\\/\\r\\b\\f\\\"", "/\r\b\f\"" );
test_escape_chars( "\\h\\j\\k", "" );
test_escape_chars( "\\x61\\x62\\x63", "abc" );
test_escape_chars( "a\\x62c", "abc" );
test_escape_chars( "\\x01\\x02\\x7F", "\x01\x02\x7F" );
test_escape_chars( "\\u0061\\u0062\\u0063", "abc" );
}
void test_values()
{
String_t input;
bool res;
Value_t value;
input = to_str("1");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::int_type && value.get_int() == 1);
input = to_str("1.5");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::real_type && value.get_real() == 1.5);
input = to_str("\"Test\"");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::str_type && value.get_str() == to_str("Test"));
input = to_str("null");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::null_type);
input = to_str("true");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::bool_type && value.get_bool() == true);
input = to_str("false");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::bool_type && value.get_bool() == false);
input = to_str("{}");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::obj_type);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::obj_type);
input = to_str("[]");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::array_type);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::array_type);
input = to_str("[null]");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::array_type);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::array_type);
input = to_str("[null, 1]");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::array_type);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::array_type);
input = to_str("[\"1\",\"2\"]");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::array_type && value.get_array().size() == 2);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::array_type && value.get_array().size() == 2);
input = to_str("[\"1\\\",\\\"2\"]");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::array_type && value.get_array().size() == 1);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::array_type && value.get_array().size() == 1);
input = to_str("[\"1\\\\\",\"2\\\\\"]");
res= json_spirit::read(input, value);
assert(res && value.type() == json_spirit::array_type && value.get_array().size() == 2);
res= json_spirit::read_value(input, value);
assert(res && value.type() == json_spirit::array_type && value.get_array().size() == 2);
}
void test_error_cases()
{
String_t input;
bool res;
Value_t value;
input = to_str("[\"1\\\\\",\"2\\\"]");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str(".");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("1 1");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("\"\"\"");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("'1'");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("{1 2}");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("1.263Q");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("1.26 3");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("'");
res= json_spirit::read(input, value);
assert(!res);
res= json_spirit::read_value(input, value);
assert(!res);
input = to_str("[1 2]");
res= json_spirit::read(input, value);
res= json_spirit::read_value(input, value);
}
void run_tests()
{
test_syntax();
test_reading();
test_from_stream();
test_escape_chars();
test_values();
test_error_cases();
}
};
#ifndef BOOST_NO_STD_WSTRING
void test_wide_esc_u()
{
wValue value;
const bool success = read( L"[\"\\uABCD\"]", value );
assert( success );
const wstring s( value.get_array()[0].get_str() );
assert_eq( s.length(), static_cast< wstring::size_type >( 1u ) );
assert_eq( s[0], 0xABCD );
}
#endif
void test_extended_ascii( const string& s )
{
Value value;
const bool success = read( "[\"" + s + "\"]", value );
assert_eq( success, true );
assert_eq( value.get_array()[0].get_str(), "äöüß" );
}
void test_extended_ascii()
{
test_extended_ascii( "\\u00E4\\u00F6\\u00FC\\u00DF" );
test_extended_ascii( "äöüß" );
}
}
void json_spirit::test_reader()
{
Test_runner< Value >().run_tests();
#ifndef BOOST_NO_STD_WSTRING
Test_runner< wValue >().run_tests();
test_wide_esc_u();
#endif
test_extended_ascii();
}
</sstream>
|