示例#1
0
void JSONTest::testEmptyObjectElement()
{
	std::string json = "[{}]";
	Parser parser;
	Var result;

	try
	{
		DefaultHandler handler;
		parser.setHandler(&handler);
		parser.parse(json);
		result = handler.result();
	}
	catch(JSONException& jsone)
	{
		std::cout << jsone.message() << std::endl;
		assert(false);
	}

	assert(result.type() == typeid(Array::Ptr));

	Array::Ptr array = result.extract<Array::Ptr>();
	Object::Ptr object = array->getObject(0);
	assert(object->size() == 0);
}
vector<shared_ptr<FDSBucket> > GalaxyFDSClient::listBuckets() {
  string uri = formatUri(_pConfig->getBaseUri(), "", _emptySubResources);
  URI pocoUri(uri);

  shared_ptr<HTTPClientSession> pSession(_pSessionFacotry
      ->createClientSession(pocoUri));
  pSession->setHost(pocoUri.getHost());
  pSession->setPort(pocoUri.getPort());
  HTTPRequest request(HTTPRequest::HTTP_GET, uri, HTTPMessage::HTTP_1_1);
  prepareRequestHeaders(uri, HTTPRequest::HTTP_GET, "", _emptyStream,
      *_pEmptyMetadata, request);
  HTTPResponse response;

  ostream& os = pSession->sendRequest(request);
  istream& rs = pSession->receiveResponse(response);

  if (response.getStatus() != HTTPResponse::HTTP_OK) {
    stringstream msg;
    msg << "List buckets failed, status=" << response.getStatus() << ", reason=";
    StreamCopier::copyStream(rs, msg);
    throw GalaxyFDSClientException(response.getStatus(), msg.str());
  }

  vector<shared_ptr<FDSBucket> > res;
  Parser parser;
  parser.parse(rs);
  Var result = parser.result();
  Object::Ptr pObject = result.extract<Object::Ptr>();
  Object::Ptr pOwnerObject = pObject->getObject("owner");
  stringstream ss;
  pOwnerObject->stringify(ss);
  shared_ptr<Owner> pOwner = Owner::deserialize(ss);
  Array::Ptr pBucketsArray = pObject->getArray("buckets");
  for (size_t i = 0; i < pBucketsArray->size(); i++) {
    string bucketName = pBucketsArray->getObject(i)->getValue<string>("name");
    shared_ptr<FDSBucket> pBucket(new FDSBucket(bucketName));
    pBucket->setOwner(*pOwner);
    res.push_back(pBucket);
  }

  return res;
}
示例#3
0
void RandomVar::ReadFromJSON(const char* fileName) {
    
    Parser loParser;
    std::ifstream t(fileName);
    std::stringstream buffer;
    buffer << t.rdbuf();
    string json = buffer.str();
    // Parse the JSON and get the Results
    Poco::Dynamic::Var loParsedJson = loParser.parse(json);
    Poco::Dynamic::Var loParsedJsonResult = loParser.result();

    // Random variable data are an array of objects
    //[
    // {"value": 1, "probability" : 0.2, "cumulative" : 0.2},
    // {"value": 2, "probability" : 0.2, "cumulative" : 0.4},
    // {"value": 3, "probability" : 0.2, "cumulative" : 0.6},
    // {"value": 4, "probability" : 0.2, "cumulative" : 0.8},
    // {"value": 5, "probability" : 0.2, "cumulative" : 1.0}
    //]    
    
    Array::Ptr arr = loParsedJsonResult.extract<Array::Ptr>();
    size_t size = arr->size();

    m_P.resize(size);
    m_C.resize(size);
    m_X.resize(size);
            
    // Individual data rows
    Object::Ptr object;
    for (size_t i = 0; i < size; i++) {
        object = arr->getObject(i);
        this->setX(i, object->getValue<double>("value"));
        this->setP(i, object->getValue<double>("probability"));
        this->setC(i, object->getValue<double>("cumulative"));        
    }

    return;
}
示例#4
0
	void doSearch( Utility::MTInput & input,
			Utility::MTOutput & mtOutput,
			Utility::MTOutput & mtConsoleOutput,
			size_t startLine ) const
	{
		Poco::Net::HTTPClientSession session( m_uri.getHost(), m_uri.getPort() );
		{
			Utility::MTOutput::unique_lock lock( mtConsoleOutput.acquire());
			mtConsoleOutput.os() << "Thread " << pthread_self() << " starting ";
			mtConsoleOutput.flush();
		}

		std::string line;

		while( size_t lineNum = input.getline( line ))
		{
			if( lineNum < startLine )
				continue;

			std::string path;
			bool success = false;
			size_t maxTries = 4;
			while( !success && maxTries )
			{
				try
				{
					Utility::StrTuple term = Utility::delimitedText( line, '\t' );
					if( term.size() < 4 )
						break;

					std::string artist = term[2];
					std::string title = Utility::trim(term[3]);
					path = make_request( artist + ' ' + title );
					Poco::Net::HTTPRequest request( Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1 );
					Poco::Net::HTTPResponse response;
					session.sendRequest(request);
					std::istream& rs = session.receiveResponse(response);

					using Poco::Dynamic::Var;
					using namespace Poco::JSON;
					Parser parser;
					Var result = parser.parse(rs);
					Object::Ptr obj = result.extract<Object::Ptr>();
					int nResults = obj->getValue<int>("resultCount");

					{
						Utility::MTOutput::unique_lock lock( mtConsoleOutput.acquire());
						mtConsoleOutput.os() << lineNum << ": " << path << ' ' << nResults
								<< " results " << std::endl;
					}

					if( nResults )
					{
						Array::Ptr arr;
						arr = obj->getArray( "results" );
						if( !arr )
						{
							std::cerr << "Could not get results " << std::endl;
							continue;
						}
						std::string releaseDate;
						std::string genre;
						std::string artistName; // on iTunes
						std::string trackName; // on iTunes
						bool found = false;
						// if there is more than one see if there is an exact match. Otherwise use the first result
						for( size_t i = 0; !found && i < nResults ; ++i )
						{
							Object::Ptr result = arr->getObject(i);
							if( !result )
							{
								std::cerr << " Could not get result " << i << std::endl;
								continue;
							}
							// get ArtistName and Title and see if they match ours
							Var item = result->get( "artistName" );
							if( item.isString() )
								artistName = item.convert< std::string >();

							item = result->get( "trackName" );

							if( item.isString() )
								trackName = item.convert< std::string >();

							if( (artistName == artist && trackName == title) ) // we have an exact match so continue
								found = true;

							// if no exact matches are found we use the first one.
							// We could use a better way to match the search eg case insensitive, removing featured acts etc.
							if( found || i == 0 )
							{
								item = result->get( "releaseDate");
								if( item.isString() )
								{
									std::string releaseDateStr = item.convert< std::string >();
									releaseDate = releaseDateStr.substr( 0, releaseDateStr.find('T') );
								}
								item = result->get( "primaryGenreName" );
								if( item.isString() )
									genre = item.convert< std::string >();
							}
						}

						if( m_filteredGenres.count( genre ) == 0 )
						{
							Utility::MTOutput::unique_lock lock( mtOutput.acquire());
						// output the result. Spotify link, artist(spotify), title(spotify)
						// artist(iTunes), title(iTunes), releaseDate, genre, numTracks (term[4])
							mtOutput.os() << lineNum << '\t' <<
									term[0] << '\t' << term[1] << '\t' << artist << '\t' << title << '\t' <<
									artistName << '\t' << trackName << '\t' << releaseDate << '\t' << genre << '\t' <<  term[4] << '\n';

							mtOutput.flush();
						}
					}
					success = true;
				}
				catch( std::exception const& err )
				{
					success = false;
					if( --maxTries )
					{
						sleep(1);
					}
					else
					{
						Utility::MTOutput::unique_lock lock( mtConsoleOutput.acquire());
						std::cerr << "ERROR: " << err.what() << lineNum << ": " << path  << std::endl;
					}
				}
			}
		}
		{
			Utility::MTOutput::unique_lock lock( mtConsoleOutput.acquire());
			mtConsoleOutput.os() << "Thread " << pthread_self() << " exiting\n";
			mtConsoleOutput.flush();
		}
	}