Esempio n. 1
0
void JSONTest::testStringElement()
{
	std::string json = "[ \"value\" ]";
	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>();
	Var test = array->get(0);
	assert(test.isString());
	std::string value = test.convert<std::string>();
	assert(value.compare("value") == 0);
}
Esempio n. 2
0
void JSONTest::testObjectProperty()
{
	std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
	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(Object::Ptr));

	Object::Ptr object = result.extract<Object::Ptr>();
	Var test = object->get("test");
	assert(test.type() == typeid(Object::Ptr));
	object = test.extract<Object::Ptr>();

	test = object->get("property");
	assert(test.isString());
	std::string value = test.convert<std::string>();
	assert(value.compare("value") == 0);
}
Esempio n. 3
0
void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent, int step, int options)
{
	bool escapeUnicode = ((options & Poco::JSON_ESCAPE_UNICODE) != 0);

	if (step == -1) step = indent;

	if (any.type() == typeid(Object))
	{
		Object& o = const_cast<Object&>(any.extract<Object>());
		o.setEscapeUnicode(escapeUnicode);
		o.stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if (any.type() == typeid(Array))
	{
		Array& a = const_cast<Array&>(any.extract<Array>());
		a.setEscapeUnicode(escapeUnicode);
		a.stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if (any.type() == typeid(Object::Ptr))
	{
		Object::Ptr& o = const_cast<Object::Ptr&>(any.extract<Object::Ptr>());
		o->setEscapeUnicode(escapeUnicode);
		o->stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if (any.type() == typeid(Array::Ptr))
	{
		Array::Ptr& a = const_cast<Array::Ptr&>(any.extract<Array::Ptr>());
		a->setEscapeUnicode(escapeUnicode);
		a->stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if (any.isEmpty())
	{
		out << "null";
	}
	else if (any.isNumeric() || any.isBoolean())
	{
		std::string value = any.convert<std::string>();
		if (any.type() == typeid(char)) formatString(value, out, options);
		else out << value;
	}
	else if (any.isString() || any.isDateTime() || any.isDate() || any.isTime())
	{
		std::string value = any.convert<std::string>();
		formatString(value, out, options);
	}
	else
	{
		out << any.convert<std::string>();
	}
}
Esempio n. 4
0
void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent, int step, bool preserveInsertionOrder)
{
	if (step == -1) step = indent;

	if ( any.type() == typeid(Object) )
	{
		const Object& o = any.extract<Object>();
		o.stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if ( any.type() == typeid(Array) )
	{
		const Array& a = any.extract<Array>();
		a.stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if ( any.type() == typeid(Object::Ptr) )
	{
		const Object::Ptr& o = any.extract<Object::Ptr>();
		o->stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if ( any.type() == typeid(Array::Ptr) )
	{
		const Array::Ptr& a = any.extract<Array::Ptr>();
		a->stringify(out, indent == 0 ? 0 : indent, step);
	}
	else if ( any.isEmpty() )
	{
		out << "null";
	}
	else if ( any.isString() )
	{
		std::string value = any.convert<std::string>();
		formatString(value, out);
	}
	else
	{
		out << any.convert<std::string>();
	}
}
Esempio n. 5
0
void VarTest::testEmpty()
{
	Var da;
	assert (da.isEmpty());
	assert (da.type() == typeid(void));
	assert (!da.isArray());
	assert (!da.isInteger());
	assert (!da.isNumeric());
	assert (!da.isSigned());
	assert (!da.isString());
	assert (!(da == da));
	assert (!(da != da));

	da = "123";
	int i = da.convert<int>();
	assert (123 == i);
	std::string s = da.extract<std::string>();
	assert ("123" == s);
	assert (!da.isEmpty());
	da.empty();
	assert (da.isEmpty());
	assert (da.type() == typeid(void));
	assert (!da.isArray());
	assert (!da.isInteger());
	assert (!da.isNumeric());
	assert (!da.isSigned());
	assert (!da.isString());
	assert (!(da == da));
	assert (!(da != da));

	assert (da != "");
	assert ("" != da);
	assert (!(da == ""));
	assert (!("" == da));
	

	testEmptyComparisons<unsigned char>();
	testEmptyComparisons<char>();
	testEmptyComparisons<Poco::UInt8>();
	testEmptyComparisons<Poco::Int8>();
	testEmptyComparisons<Poco::UInt16>();
	testEmptyComparisons<Poco::Int16>();
	testEmptyComparisons<Poco::UInt32>();
	testEmptyComparisons<Poco::Int32>();
	testEmptyComparisons<Poco::UInt64>();
	testEmptyComparisons<Poco::Int64>();
#ifdef POCO_LONG_IS_64_BIT
	testEmptyComparisons<unsigned long>();
	testEmptyComparisons<long>();
#endif
	testEmptyComparisons<float>();
	testEmptyComparisons<double>();

	try
	{
		int i = da;
		fail ("must fail");
	} catch (InvalidAccessException&) { }

	try
	{
		int i = da.extract<int>();
		fail ("must fail");
	} catch (InvalidAccessException&) { }
}
void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent)
{
	if ( any.type() == typeid(Object) )
	{
		const Object& o = any.extract<Object>();
		o.stringify(out, indent == 0 ? 0 : indent + 2);
	}
	else if ( any.type() == typeid(Array) )
	{
		const Array& a = any.extract<Array>();
		a.stringify(out, indent == 0 ? 0 : indent + 2);
	}
	else if ( any.type() == typeid(Object::Ptr) )
	{
		const Object::Ptr& o = any.extract<Object::Ptr>();
		o->stringify(out, indent == 0 ? 0 : indent + 2);
	}
	else if ( any.type() == typeid(Array::Ptr) )
	{
		const Array::Ptr& a = any.extract<Array::Ptr>();
		a->stringify(out, indent == 0 ? 0 : indent + 2);
	}
	else if ( any.isEmpty() )
	{
		out << "null";
	}
	else if ( any.isString() )
	{
		out << '"';
		std::string value = any.convert<std::string>();
		for(std::string::const_iterator it = value.begin(); it != value.end(); ++it)
		{
			switch (*it)
			{
			case '"':
				out << "\\\"";
				break;
			case '\\':
				out << "\\\\";
				break;
			case '\b':
				out << "\\b";
				break;
			case '\f':
				out << "\\f";
				break;
			case '\n':
				out << "\\n";
				break;
			case '\r':
				out << "\\r";
				break;
			case '\t':
				out << "\\t";
				break;
			default:
			{
				if ( *it > 0 && *it <= 0x1F )
				{
					out << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*it);
				}
				else
				{
					out << *it;
				}
				break;
			}
			}
		}
		out << '"';
	}
	else
	{
		out << any.convert<std::string>();
	}
}
Esempio n. 7
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();
		}
	}