Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some example of how to serialize/deserilize from Json format to Object and from Object to Json. I used this example http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html, but it only serialize to a simple text. In my project, for other requirenments, I am using boost library with success. I am almost sure that I do need some other specific library like Json-Spirit or standard QT library (QScriptEngine). Any simple example will be very appreciate.

This is an example of the Json files I must serialized to a class.

[{"DscTipoEstrutura":"Unidade de Neg\u00f3cio","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":1,"NroOrdem":0},{"DscTipoEstrutura":"Regi\u00e3o","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":2,"NroOrdem":1},{"DscTipoEstrutura":"Distrito","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":3,"NroOrdem":2}]


Ps.: I found http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx and http://boost-spirit.com/repository/applications/show_contents.php similiar to I need but, because I am C++ begnnier, I couldn't understand them neither run the example after some couple of hours. I need some simple example.
Posted
Updated 2-Sep-11 11:12am
v3
Comments
Philippe Mori 2-Sep-11 18:28pm    
I don't think there would be easy solution in C++ as there is in language with dynamic object support like JavaScript or never release of C#.

1 solution

Hi guys,
I have used a CodeProject tutorial to bring a solution so you should download the code associated to the tutorial first at this adress:
http://www.codeproject.com/KB/recipes/JSON_Spirit.aspx

Then you can adapt it to your project quite rapidly (thanks to the tutorial)
and you get this solution.


C++
#include "json_spirit.h"
#include <cassert>
#include <fstream>

#ifndef JSON_SPIRIT_VALUE_ENABLED
#error Please define JSON_SPIRIT_VALUE_ENABLED for the Value type to be enabled 
#endif

using namespace std;
using namespace json_spirit;

struct Address
{
    string DscTipoEstrutura_;
    bool FlgAtivo_;
    int IdDominio_;
    int IdTipoEstrutura_;
    int NroOrdem_;
};

bool operator==( const Address& a1, const Address& a2 )
{
    return ( a1.DscTipoEstrutura_ == a2.DscTipoEstrutura_ ) &&
           ( a1.FlgAtivo_         == a2.FlgAtivo_ ) &&
           ( a1.IdDominio_         == a2.IdDominio_ ) &&
           ( a1.IdTipoEstrutura_       == a2.IdTipoEstrutura_ ) &&
           ( a1.NroOrdem_      == a2.NroOrdem_ );
}

void write_address( Array& a, const Address& addr )
{
    Object addr_obj;

    addr_obj.push_back( Pair( "DscTipoEstrutura", addr.DscTipoEstrutura_ ) );
    addr_obj.push_back( Pair( "FlgAtivo",         addr.FlgAtivo_ ) );
    addr_obj.push_back( Pair( "IdDominio",         addr.IdDominio_ ) );
    addr_obj.push_back( Pair( "IdTipoEstrutura",       addr.IdTipoEstrutura_ ) );
    addr_obj.push_back( Pair( "NroOrdem",      addr.NroOrdem_ ) );

    a.push_back( addr_obj );
}

Address read_address( const Object& obj )
{
    Address addr;
	string strBool;

    for( Object::size_type i = 0; i != obj.size(); ++i )
    {
        const Pair& pair = obj[i];

        const string& name  = pair.name_;
        const Value&  value = pair.value_;

        if( name == "DscTipoEstrutura" )
        {
            addr.DscTipoEstrutura_ = value.get_str();
        }
        else if( name == "FlgAtivo" )
        {
			addr.FlgAtivo_ = value.get_bool();;//to be changed
        }
        else if( name == "IdDominio" )
        {
            addr.IdDominio_ = value.get_int();
        }
        else if( name == "IdTipoEstrutura" )
        {
            addr.IdTipoEstrutura_ = value.get_int();
        }
        else if( name == "NroOrdem" )
        {
            addr.NroOrdem_ = value.get_int();
        }
        else
        {
            assert( false );
        }
    }

    return addr;
}

void write_addrs( const char* file_name, const Address addrs[] )
{
    Array addr_array;

    for( int i = 0; i < 5; ++i )
    {
        write_address( addr_array, addrs[i] );
    }

    ofstream os( file_name );

    write_formatted( addr_array, os );

    os.close();
}

vector< Address > read_addrs( const char* file_name )
{
    ifstream is( file_name );

    Value value;

    read( is, value );

    const Array& addr_array = value.get_array();

    vector< Address > addrs;

    for( unsigned int i = 0; i < addr_array.size(); ++i )
    {
        addrs.push_back( read_address( addr_array[i].get_obj() ) );
    }

    return addrs;
}

int main()
{
    const Address addrs[5] = { { "Unidade de Neg\u00f3cio",		"true",	   1,	1,	0 },
                               { "Regi\u00e3o",     "true",    1,	2	,1 },
                               { "Distrito", "true",   1, 3,2   },
                               { "jkl", "false",   3,    3, 2 },
                               { "mno", "false", 2, 4, 2 } };

    const char* file_name( "demo.txt" );

    write_addrs( file_name, addrs );//de la représentation tabulée au JSON

    vector< Address > new_addrs = read_addrs( file_name );//du JSON à la représentation tabulée

    assert( new_addrs.size() == 5 );

    for( int i = 0; i < 5; ++i )
    {
        assert( new_addrs[i] == addrs[i] );
    }

	return 0;
}
</fstream></cassert>


Hope it helps and tell me if you have problems or if the solution is not what you expected.

By from France,
Gabriel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900