Click here to Skip to main content
12,241,419 members (52,014 online)
Click here to Skip to main content

Stats

2.3M views
18.5K 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 (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

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