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.
#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();; }
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 );
vector< Address > new_addrs = read_addrs( file_name );
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