Esempio n. 1
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;
    }

}
Esempio n. 2
0
void JsonBag::load()
{
	if( ! fs::exists( mJsonFilePath ) )
		return;
	
	try {
		JsonTree doc( loadFile( mJsonFilePath ) );

		if( doc.hasChild( "version" ) ) {
			mVersion = doc.getChild( "version" ).getValue<int>();
		}

		for( JsonTree::ConstIter groupIt = doc.begin(); groupIt != doc.end(); ++groupIt ) {
			auto& jsonGroup = *groupIt;
			auto groupName = jsonGroup.getKey();
			if( mItems.count( groupName ) ) {
				auto groupItems = mItems.at( groupName );
				for( JsonTree::ConstIter item = jsonGroup.begin(); item != jsonGroup.end(); ++item ) {
					const auto& name = item->getKey();
					if( groupItems.count( name ) ) {
						groupItems.at( name )->load( item );
					} else {
						CI_LOG_E( "No item named " + name );
					}
				}
			}
			else if( groupName != "version" ) {
				CI_LOG_E( "No group named " + groupName );
			}
		}
	}
	catch( const JsonTree::ExcJsonParserError& )  {
		CI_LOG_E( "Failed to parse json file." );
	}
	mIsLoaded = true;
}