Click here to Skip to main content
15,886,788 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.1M   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_value.h"

#include <cassert>

using namespace json_spirit;

const Value Value::null;

Value::Value()
:   type_( null_type )
{
}

Value::Value( const char* value )
:   type_( str_type )
,   str_( value )
{
}

Value::Value( const std::string& value )
:   type_( str_type )
,   str_( value )
{
}

Value::Value( const Object& value )
:   type_( obj_type )
,   obj_( value )
{
}

Value::Value( const Array& value )
:   type_( array_type )
,   array_( value )
{
}

Value::Value( bool value )
:   type_( bool_type )
,   bool_( value )
{
}

Value::Value( int value )
:   type_( int_type )
,   i_( value )
{
}

Value::Value( double value )
:   type_( real_type )
,   d_( value )
{
}

bool Value::operator==( const Value& lhs ) const
{
    if( this == &lhs ) return true;

    if( type() != lhs.type() ) return false;

    switch( type_ )
    {
        case str_type:   return get_str()   == lhs.get_str();
        case obj_type:   return get_obj()   == lhs.get_obj();
        case array_type: return get_array() == lhs.get_array();
        case bool_type:  return get_bool()  == lhs.get_bool();
        case int_type:   return get_int()   == lhs.get_int();
        case real_type:  return get_real()  == lhs.get_real();
        case null_type:  return true;
    };

    assert( false );

    return false; 
}

Value_type Value::type() const
{
    return type_;
}

const std::string& Value::get_str() const
{
    assert( type() == str_type );

    return str_;
}

const Object& Value::get_obj() const
{
    assert( type() == obj_type );

    return obj_;
}
 
const Array& Value::get_array() const
{
    assert( type() == array_type );

    return array_;
}
 
bool Value::get_bool() const
{
    assert( type() == bool_type );

    return bool_;
}
 
int Value::get_int() const
{
    assert( type() == int_type );

    return i_;
}

double Value::get_real() const
{
    assert( type() == real_type );

    return d_;
}

Object& Value::get_obj()
{
    assert( type() == obj_type );

    return obj_;
}

Array& Value::get_array()
{
    assert( type() == array_type );

    return array_;
}

Pair::Pair( const std::string& name, const Value& value )
:   name_( name )
,   value_( value )
{
}

bool Pair::operator==( const Pair& lhs ) const
{
    if( this == &lhs ) return true;

    return ( name_ == lhs.name_ ) && ( value_ == lhs.value_ );
}

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