bool Round::JSONString::copy(JSONObject **newObj) const { JSONString *dstObj = new JSONString(); if (dstObj->set(this)) { *newObj = dstObj; return true; } delete dstObj; return false; }
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); }
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); }
JSONValue * JSONAPI::ToJsonValue(JsonNode *node) { JsonType type = node->GetType(); JSONValue *val = (JSONValue *) NULL; switch(type) { case JsonType_Object: { JSONObject *jsonobj = new JSONObject(); std::list<JsonNode *> children = node->GetChildren(); std::list<JsonNode *>::iterator iter; for (iter = children.begin(); iter != children.end(); ++iter) { jsonobj->Append(ToJsonValue(*iter)); } val = static_cast<JSONValue *>(jsonobj); } break; case JsonType_Array: { JSONArray *jsonarray = new JSONArray(); std::list<JsonNode *> children = node->GetChildren(); std::list<JsonNode *>::iterator iter; for (iter = children.begin(); iter != children.end(); ++iter) { jsonarray->Append(ToJsonValue(*iter)); } val = static_cast<JSONValue *>(jsonarray); } break; case JsonType_String: { std::string str; JSONString *jsonstr = new JSONString(); static_cast<JsonValue *>(node)->GetValue(str); jsonstr->Set(str); val = static_cast<JSONValue *>(jsonstr); } break; case JsonType_Number: { long lval; JSONNumber *jsonnum = new JSONNumber(); static_cast<JsonValue *>(node)->GetValue(lval); jsonnum->Set(lval); val = static_cast<JSONValue *>(jsonnum); } break; case JsonType_Double: { double dval; JSONDouble *jsondbl = new JSONDouble(); static_cast<JsonValue *>(node)->GetValue(dval); jsondbl->Set(dval); val = static_cast<JSONValue *>(jsondbl); } break; case JsonType_Bool: { bool bval; JSONBoolean *jsonbool = new JSONBoolean(); static_cast<JsonValue *>(node)->GetValue(bval); jsonbool->Set(bval); val = static_cast<JSONValue *>(jsonbool); } break; case JsonType_Tuple: { JsonTuple *tval = static_cast<JsonTuple *>(node); JSONTuple *jsontuple = new JSONTuple(); std::string key; key = tval->GetKey(); jsontuple->SetKey(key); jsontuple->SetValue(ToJsonValue(tval->GetKeyValue())); val = static_cast<JSONValue *>(jsontuple); } break; case JsonType_Null: { JSONNull *jsonnull = new JSONNull(); val = static_cast<JSONValue *>(jsonnull); } break; default: break; } return val; }