Click here to Skip to main content
15,896,207 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.92/5 (110 votes)
10 May 2014MIT12 min read 4.2M   24.3K   287  
A C++ JSON parser/generator written using Boost::spirit
/* 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


Written By
Software Developer (Senior) Spirent Communications Plc
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions