Click here to Skip to main content
12,253,554 members (61,744 online)
Click here to Skip to main content

Stats

2.3M views
18.6K downloads
275 bookmarked
Posted

JSON Spirit: A C++ JSON Parser/Generator Implemented with Boost Spirit

, 10 May 2014 MIT
A C++ JSON parser/generator written using Boost::spirit
json_spirit_v2.03
json_demo
json_spirit
json_test
json_spirit
json_demo
json_spirit
json_test
json_spirit_v2.04
json_demo
json_spirit
json_test
json_spirit_v2.05
json_demo
json_spirit
json_test
json_spirit_v2.06
json_demo
json_spirit
json_test
json_spirit_v3.00
json_demo
json_spirit
json_test
json_spirit_v3.01
json_demo
json_spirit
json_test
README.cmake
json_spirit_v4.00
json_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.01
json_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.02
json_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.03
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.05
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.06
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.08
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_demo
json_headers_only_demo
json_map_demo
json_test
README.cmake
json_spirit_v4.07
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
//          Copyright John W. Wilkinson 2007 - 2011
// Distributed under the MIT License, see accompanying file LICENSE.txt

// json spirit version 4.05

#include "json_spirit_stream_reader_test.h"
#include "utils_test.h"
#include "json_spirit_stream_reader.h"

#include <sstream>
#include <boost/assign/list_of.hpp>

using namespace json_spirit;
using namespace std;
using namespace boost;
using namespace boost::assign;

namespace
{
    template< class Config_type >
    struct Test_runner
    {
        typedef typename Config_type::String_type String_type;
        typedef typename Config_type::Object_type Object_type;
        typedef typename Config_type::Array_type Array_type;
        typedef typename Config_type::Value_type Value_type;
        typedef typename Config_type::Pair_type Pair_type;
        typedef typename String_type::value_type  Char_type;
        typedef typename String_type::const_iterator Iter_type;
        typedef std::basic_istringstream< Char_type > Istringstream_type;
        typedef std::basic_istream< Char_type > Istream_type;

        String_type to_str( const char* c_str )
        {
            return ::to_str< String_type >( c_str );
        }

        Test_runner()
        {
        }

        void check_stream_reader( Stream_reader< Istream_type, Value_type >& reader, const vector< int >& expected_result )
        {
            Value_type v;
            const bool ok = reader.read_next( v );
            assert_eq( ok, true );
            assert_eq( v.type(), array_type );
            assert_eq( v.get_array().size(), expected_result.size() );
            for( vector< int >::size_type i = 0; i < v.get_array().size(); ++i )
            {
                assert_eq( v.get_array()[i], expected_result[i] );
            }
        }

        void check_stream_read_or_throw( Stream_reader_thrower< Istream_type, Value_type >& reader, const vector< int >& expected_result )
        {
            Value_type v;

            try
            {
                reader.read_next( v );
                assert_eq( v.type(), array_type );
                assert_eq( v.get_array().size(), expected_result.size() );
                for( vector< int >::size_type i = 0; i < v.get_array().size(); ++i )
                {
                    assert_eq( v.get_array()[i], expected_result[i] );
                }
            }
            catch( ... )
            {
                assert( false );
            }
        }

        void test_stream_reader( const char* s )
        {
            {
                Istringstream_type is( to_str( s ) );

                Stream_reader< Istream_type, Value_type > reader( is );

                check_stream_reader( reader, vector< int >() );
                check_stream_reader( reader, list_of( 1 ) );
                check_stream_reader( reader, list_of( 1 )( 2 ) );
                check_stream_reader( reader, list_of( 1 )( 2 )( 3 ) );

                Value_type v;
                const bool ok = reader.read_next( v );
                assert_eq( ok, false );
            }
            {
                Istringstream_type is( to_str( s ) );

                Stream_reader_thrower< Istream_type, Value_type > reader( is );

                check_stream_read_or_throw( reader, vector< int >() );
                check_stream_read_or_throw( reader, list_of( 1 ) );
                check_stream_read_or_throw( reader, list_of( 1 )( 2 ) );
                check_stream_read_or_throw( reader, list_of( 1 )( 2 )( 3 ) );

                try
                {
                    Value_type v;
                    reader.read_next( v );
                    assert( false );
                }
                catch( ... )
                {
                }
            }
        }

        void run_tests()
        {
            test_stream_reader( "[][1][1,2][1,2,3]" );
            test_stream_reader( "[] [1] [1,2] [1,2,3]" );
            test_stream_reader( " [] [1] [1,2] [1,2,3] " );
        }
    };
}

void json_spirit::test_stream_reader()
{
#ifdef JSON_SPIRIT_VALUE_ENABLED
    Test_runner< Config  >().run_tests();
#endif
#ifdef JSON_SPIRIT_MVALUE_ENABLED
    Test_runner< mConfig >().run_tests();
#endif

#if defined( JSON_SPIRIT_WVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
    Test_runner< wConfig  >().run_tests();
#endif
#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )
    Test_runner< wmConfig >().run_tests();
#endif
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

John W. Wilkinson
Software Developer (Senior) Spirent Communications Plc
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 10 May 2014
Article Copyright 2007 by John W. Wilkinson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid