Exemplo n.º 1
0
void jsonValueToRapidJSON(JSONValue* value, rapidjson::Value& rapidValue, rapidjson::Document& document) {
	JSONNull* nullValue = dynamic_cast<JSONNull*>(value);
	if (nullValue) {
		rapidValue.SetNull();
		return;
	}
	JSONNumber* numberValue = dynamic_cast<JSONNumber*>(value);
	if (numberValue) {
		if (numberValue->canBeUInt64()) {
			rapidValue.SetUint64(numberValue->getUInt64Value());
		}
		else if (numberValue->canBeInt64()) {
			rapidValue.SetInt64(numberValue->getInt64Value());
		}
		else  {
			rapidValue.SetDouble(numberValue->getDoubleValue());
		}
		return;
	}
	JSONString* stringValue = dynamic_cast<JSONString*>(value);
	if (stringValue) {
		rapidValue.SetString(stringValue->getValue().c_str(), stringValue->getValue().size(), document.GetAllocator());
		return;
	}
	JSONBool* boolValue = dynamic_cast<JSONBool*>(value);
	if (boolValue) {
		rapidValue.SetBool(boolValue->getValue());
		return;
	}
	JSONArray* arrayValue = dynamic_cast<JSONArray*>(value);
	if (arrayValue) {
		rapidValue.SetArray();
		std::vector<JSONValue::ref> values = arrayValue->getValues();
		for (auto & value : values) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(value.get(), obj, document);
			rapidValue.PushBack(obj, document.GetAllocator());
		}
		return;
	}
	JSONObject* objectValue = dynamic_cast<JSONObject*>(value);
	if (objectValue) {
		rapidValue.SetObject();
		typedef std::map<std::string, JSONValue::ref> ValuesMap;
		ValuesMap values = objectValue->getValues();
		for (auto & value : values) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(value.second.get(), obj, document);
			rapidjson::Value key;
			key.SetString(value.first.c_str(), value.first.size(), document.GetAllocator());
			rapidValue.AddMember(key, obj, document.GetAllocator());
		}
		return;
	}
	assert(false);
}
Exemplo n.º 2
0
std::string JSONArray::serialize() {
	rapidjson::Document d;
	jsonValueToRapidJSON(this, d, d);

	rapidjson::StringBuffer buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	d.Accept(writer);
	return buffer.GetString();
}
Exemplo n.º 3
0
void jsonValueToRapidJSON(JSONValue* value, rapidjson::Value& rapidValue, rapidjson::Document& document) {
	JSONInt* intValue = dynamic_cast<JSONInt*>(value);
	if (intValue) {
		rapidValue.SetInt(intValue->getValue());
		return;
	}
	JSONString* stringValue = dynamic_cast<JSONString*>(value);
	if (stringValue) {
		rapidValue.SetString(stringValue->getValue().c_str(), stringValue->getValue().size(), document.GetAllocator());
		return;
	}
	JSONBool* boolValue = dynamic_cast<JSONBool*>(value);
	if (boolValue) {
		rapidValue.SetBool(boolValue->getValue());
		return;
	}
	JSONArray* arrayValue = dynamic_cast<JSONArray*>(value);
	if (arrayValue) {
		rapidValue.SetArray();
		std::vector<JSONValue::ref> values = arrayValue->getValues();
		for (size_t i = 0; i < values.size(); i++) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(values[i].get(), obj, document);
			rapidValue.PushBack(obj, document.GetAllocator());
		}
		return;
	}
	JSONObject* objectValue = dynamic_cast<JSONObject*>(value);
	if (objectValue) {
		rapidValue.SetObject();
		typedef std::map<std::string, JSONValue::ref> ValuesMap;
		ValuesMap values = objectValue->getValues();
		for (ValuesMap::iterator it = values.begin(); it != values.end(); it++) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(it->second.get(), obj, document);
			rapidjson::Value key;
			key.SetString(it->first.c_str(), it->first.size(), document.GetAllocator());
			rapidValue.AddMember(key, obj, document.GetAllocator());
		}
		return;
	}
	assert(false);
}