Exemplo n.º 1
0
void JsonTree::pushBack( const JsonTree &newChild )
{
	if( newChild.getKey() == "" )
		mNodeType = NODE_ARRAY;
	else
		mNodeType = NODE_OBJECT;

	mChildren.push_back( newChild );
	mChildren.back().mParent = this;
    mValue = "";
}
Exemplo n.º 2
0
void JsonTestApp::setup()
{

    JsonTree value( "key", "value" );
    console() << value << endl;

    JsonTree doc( loadResource( RES_JSON ) );
    JsonTree musicLibrary( doc.getChild( "library" ) );

    JsonTree owner = doc.getChild( "library.owner" );
    for( JsonTree::ConstIter item = owner.begin(); item != owner.end(); ++item ) {
        console() << "Node: " << item->getKey() << " Value: " << item->getValue<string>() << endl;
    }

    JsonTree tracks = doc.getChild( "library.albums[0].tracks" );
    for( JsonTree::ConstIter track = tracks.begin(); track != tracks.end(); ++track ) {
        console() << track->getChild( "id" ).getValue<int>() << endl;
    }

    for( JsonTree::ConstIter trackIt = tracks.begin(); trackIt != tracks.end(); ++trackIt ) {
        JsonTree track = * trackIt;
        console() << track["id"].getValue<int>() << endl;
    }

    JsonTree firstAlbum = doc.getChild( "library.albums[0]" );
    for( JsonTree::Iter child = firstAlbum.begin(); child != firstAlbum.end(); ++child ) {
        if ( !child->hasChildren() ) {
            console() << "Key: " << child->getKey() << "  Value: " << child->getValue<string>() << endl;
        }
    }

    console() << doc.getChild( "library.owner" );
    JsonTree &ownerCity = doc.getChild( "library.owner.city" );
    string s = ownerCity.getPath();
    console() << "Path: " << ownerCity.getPath() << "\n  Value: " << ownerCity.getValue<string>() << std::endl;
    console() << doc;

    JsonTree firstTrackCopy = doc.getChild( "library.albums[0].tracks[0].title" );
    firstTrackCopy = JsonTree( firstTrackCopy.getKey(), string( "Replacement name" ) );
    console() << doc.getChild( "library.albums[0].tracks[0]['title']" ) << std::endl;

    JsonTree &firstTrackRef = doc.getChild( "library.albums[0].tracks[2].title" );
    console() << firstTrackRef.getPath() << std::endl;
    firstTrackRef = JsonTree( firstTrackRef.getKey(), string( "Replacement name" ) );
    console() << doc.getChild( "library.albums[0].tracks[0].title" ) << std::endl;

    try {
        JsonTree invalid( "%%%%%%%%" );
    } catch ( JsonTree::ExcJsonParserError ex ) {
        console() << ex.what() << std::endl;
    }

}