C++ (Cpp) CharReaderBuilder Example

Introduction

The c++ (cpp) charreaderbuilder example is extracted from the most popular open source projects, you can refer to the following example for usage.

Programming language: C++ (Cpp)

Namespace/package name: json

Example#1
File: UnixStreamSocket.cppProject: furqanrydhan/mediasoup

	UnixStreamSocket::UnixStreamSocket(int fd) :
		::UnixStreamSocket::UnixStreamSocket(fd, NS_MAX_SIZE)
	{
		MS_TRACE_STD();

		// Create the JSON reader.
		{
			Json::CharReaderBuilder builder;
			Json::Value settings = Json::nullValue;
			Json::Value invalid_settings;

			builder.strictMode(&settings);

			MS_ASSERT(builder.validate(&invalid_settings), "invalid Json::CharReaderBuilder");

			this->jsonReader = builder.newCharReader();
		}

		// Create the JSON writer.
		{
			Json::StreamWriterBuilder builder;
			Json::Value invalid_settings;

			builder["commentStyle"] = "None";
			builder["indentation"] = "";
			builder["enableYAMLCompatibility"] = false;
			builder["dropNullPlaceholders"] = false;

			MS_ASSERT(builder.validate(&invalid_settings), "invalid Json::StreamWriterBuilder");

			this->jsonWriter = builder.newStreamWriter();
		}
	}

Example#2
File: rest.cppProject: ksooo/pvr.pctv

int cRest::Post(const std::string& command, const std::string& arguments, Json::Value& json_response)
{
	std::string response;
	int retval;
	retval = httpRequest(command, arguments, true, response);

	if (retval != E_FAILED)
	{
		if (response.length() != 0)
		{
			std::string jsonReaderError;
			Json::CharReaderBuilder jsonReaderBuilder;
			std::unique_ptr<Json::CharReader> const reader(jsonReaderBuilder.newCharReader());

			if (!reader->parse(response.c_str(), response.c_str() + response.size(), &json_response, &jsonReaderError))
			{
				XBMC->Log(LOG_DEBUG, "Failed to parse %s: \n%s\n",
					response.c_str(),
					jsonReaderError.c_str());
				return E_FAILED;
			}
		}
		else
		{
			XBMC->Log(LOG_DEBUG, "Empty response");
			return E_EMPTYRESPONSE;
		}
	}

	return retval;
}

Example#3
File: JsonSerializer.cppProject: Thalhammer/EasyCpp

		AnyValue JsonSerializer::deserialize(const std::string & str)
		{
			Json::Value root;
			Json::CharReaderBuilder rbuilder;
			rbuilder["collectComments"] = false;
			std::unique_ptr<Json::CharReader> rdr(rbuilder.newCharReader());
			if (!rdr->parse(str.data(), str.data() + str.size(), &root, nullptr))
				throw std::runtime_error("Invalid json string");
			return this->toAny(root);
		}

Example#4
File: HTTPRequest.cppProject: hanzz/spectrum2

bool HTTPRequest::GET(std::string url, Json::Value &json) {
	if (!GET(url, m_data)) {
		return false;
	}

	Json::CharReaderBuilder rbuilder;
	std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
	if (!reader->parse(m_data.c_str(), m_data.c_str() + m_data.size(), &json, nullptr)) {
		LOG4CXX_ERROR(logger, "Error while parsing JSON");
	        LOG4CXX_ERROR(logger, m_data);
		strcpy(curl_errorbuffer, "Error while parsing JSON");
		return false;
	}

	return true;
}

Example#5
File: SlackRTM.cppProject: hanzz/spectrum2

void SlackRTM::handlePayloadReceived(const std::string &payload) {
	Json::Value d;
	Json::CharReaderBuilder rbuilder;
	std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
	if (!reader->parse(payload.c_str(), payload.c_str() + payload.size(), &d, nullptr)) {
		LOG4CXX_ERROR(logger, "Error while parsing JSON");
		LOG4CXX_ERROR(logger, payload);
		return;
	}

	STORE_STRING(d, type);

	if (type == "message") {
		STORE_STRING(d, channel);
		STORE_STRING(d, text);
		STORE_STRING(d, ts);
		STORE_STRING_OPTIONAL(d, subtype);
		STORE_STRING_OPTIONAL(d, purpose);

		Json::Value &attachments = d["attachments"];
		if (attachments.isArray()) {
			for (unsigned i = 0; i < attachments.size(); i++) {
				STORE_STRING_OPTIONAL(attachments[i], fallback);
				if (!fallback.empty()) {
					text += fallback;
				}
			}
		}

		if (subtype == "bot_message") {
			STORE_STRING(d, bot_id);
			onMessageReceived(channel, bot_id, text, ts);
		}
		else if (subtype == "me_message") {
			text = "/me " + text;
			STORE_STRING(d, user);
			onMessageReceived(channel, user, text, ts);
		}
		else if (subtype == "channel_join") {
			
		}
		else if (!purpose.empty()) {
			
		}
		else {
			STORE_STRING(d, user);
			onMessageReceived(channel, user, text, ts);
		}
	}
	else if (type == "channel_joined"
		  || type == "channel_created") {
		std::map<std::string, SlackChannelInfo> &channels = m_idManager->getChannels();
		SlackAPI::getSlackChannelInfo(NULL, true, d, payload, channels);
	}
	else if (type == "error") {
		GET_OBJECT(d, error);
		STORE_INT(error, code);

		if (code == 1) {
			LOG4CXX_INFO(logger, "Reconnecting to Slack network");
			m_pingTimer->stop();
			m_client->disconnectServer();
			start();
		}
	}
}