Click here to Skip to main content
11,576,891 members (55,103 online)
Click here to Skip to main content
Add your own
alternative version

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

, 10 May 2014 MIT 2.2M 16.5K 270
A C++ JSON parser/generator written using Boost::spirit
json_spirit.zip
json_spirit_v2.03
json_demo
json_spirit
json_test
json_spirit_src.zip
json_spirit
json_demo
json_spirit
json_test
json_spirit_v2.04.zip
json_spirit_v2.04
json_demo
json_spirit
json_test
json_spirit_v2.05.zip
json_spirit_v2.05
json_demo
json_spirit
json_test
json_spirit_v2_06.zip
json_spirit_v2.06
json_demo
json_spirit
json_test
json_spirit_v3.00.zip
json_spirit_v3.00
json_demo
json_spirit
json_test
json_spirit_v3.01.zip
json_spirit_v3.01
json_demo
json_spirit
json_test
README.cmake
json_spirit_v4.00.zip
json_spirit_v4.00
json_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.01.zip
json_spirit_v4.01
json_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.02.zip
json_spirit_v4.02
json_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.03.zip
json_spirit_v4.03
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.05.zip
json_spirit_v4.05
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.06.zip
json_spirit_v4.06
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4.08.zip
json_spirit_v4.08
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
json_spirit_v4_04.zip
json_demo
json_headers_only_demo
json_map_demo
json_test
json_spirit_v4_05.zip
README.cmake
json_spirit_v4_07.zip
json_spirit_v4.07
json_demo
json_headers_only_demo
json_map_demo
json_spirit
json_test
README.cmake
/* Copyright (c) 2007 John W Wilkinson

   This source code can be used for any purpose as long as
   this comment is retained. */

#include "json_spirit_writer.h"
#include "json_spirit_value.h"

#include <cassert>
#include <sstream>

using namespace json_spirit;
using namespace std;

namespace
{

    // does the actual formatting,
    // it keeps track of the indentation level etc.
    //
    class Generator
    {
    public:

        Generator( const Value& value, ostream& os, bool pretty )
        :   os_( os )
        ,   indentation_level_( 0 )
        ,   pretty_( pretty )
        {
            output( value );
        }

    private:

        void output( const Value& 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_ <<  value.get_real();    break;
                case null_type:  os_ << "null";               break;
                default: assert( false );
            }
        }

        void output( const Object& obj )
        {
            output_array_or_obj( obj, '{', '}' );
        }

        void output( const Array& arr )
        {
            output_array_or_obj( arr, '[', ']' );
        }

        void output( const Pair& pair )
        {
            output( pair.name_ ); space(); os_ << ':'; space(); output( pair.value_ );
        }

        void output( const string& s )
        {
            os_ << '"' << s << '"';
        }

        void output( bool b )
        {
            const string s = b ? "true" : "false";
            os_ << s;
        }

        template< class T >
        void output_array_or_obj( const T& t, char start_char, char 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';
        }

        ostream& os_;
        int indentation_level_;
        bool pretty_;
    };
}

void json_spirit::write( const Value& value, std::ostream& os )
{
    Generator( value, os, false );
}

void json_spirit::write_formatted( const Value& value, std::ostream& os )
{
    Generator( value, os, true );
}

std::string json_spirit::write( const Value& value )
{
    ostringstream os;

    write( value, os );

    return os.str();
}

std::string json_spirit::write_formatted( const Value& value )
{
    ostringstream os;

    write_formatted( value, os );

    return os.str();
}

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
Web04 | 2.8.150603.1 | Last Updated 10 May 2014
Article Copyright 2007 by John W. Wilkinson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid