rapidjson::Value SceneConvertor::_GameObject_to_JSON( const GameObject* gameObject, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); Value stringNode; stringNode.SetString( gameObject->GetName().ToChar(), al ); json.AddMember( "name", stringNode, al ); json.AddMember( "enabled", gameObject->IsEnabled(), al ); if (_useAll) { json.AddMember( "savable", gameObject->IsSavable(), al ); } Value objectNode; objectNode.SetObject(); for (uint i = 0; i < gameObject->GetComponentCount(); ++i) { I_Component* comp = gameObject->GetComponentAt(i); if ( comp->WhatIs() == E_Component::Transform ) objectNode.AddMember( "Transform", _Transform_to_JSON( static_cast<Transform*>( comp ), al ), al ); if ( comp->WhatIs() == E_Component::MeshDrawing ) objectNode.AddMember( "MeshDrawing", _MeshDrawing_to_JSON( static_cast<MeshDrawing*>( comp ), al ), al ); if ( comp->WhatIs() == E_Component::Camera ) objectNode.AddMember( "Camera", _Camera_to_JSON( static_cast<Camera*>( comp ), al ), al ); if ( comp->WhatIs() == E_Component::Light ) objectNode.AddMember( "Light", _Light_to_JSON( static_cast<Light*>( comp ), al ), al ); if ( comp->WhatIs() == E_Component::Script ) objectNode.AddMember( "Script", _Script_to_JSON( static_cast<Script*>( comp ), al ), al ); } json.AddMember( "components", objectNode, al ); Value arrayNode; arrayNode.SetArray(); for (uint i = 0; i < gameObject->GetChildNodeCount(); ++i) { GameObject* child = gameObject->GetChildNodeAt(i); if ( _useAll || child->IsSavable() ) { arrayNode.PushBack( _GameObject_to_JSON(child, al), al ); } } json.AddMember( "childNodes", arrayNode, al ); return json; }
rapidjson::Value SceneConvertor::_Transform_to_JSON( const Transform* transform, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); json.AddMember( "positionWorld", _Vec3_to_JSON(transform->GetPositionWorld(), al), al); json.AddMember( "rotationWorld", _Vec3_to_JSON(transform->GetEulerRotationWorld(), al), al); json.AddMember( "scaleWorld", _Vec3_to_JSON(transform->GetScaleWorld(), al), al); return json; }
rapidjson::Value SceneConvertor::_Vec3_to_JSON( const Vector3& value, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); json.AddMember( "x", value.x(), al ); json.AddMember( "y", value.y(), al ); json.AddMember( "z", value.z(), al ); return json; }
void SG2VGJSON::addPath(const string& name, const vector<SGSegment>& path, int rank) { Value jpath; jpath.SetObject(); addString(jpath, "name", name); Value mappings; mappings.SetArray(); int inputPathLength = 0; int outputPathLength = 0; for (int i = 0; i < path.size(); ++i) { sg_int_t sgSeqID = path[i].getSide().getBase().getSeqID(); if (path[i].getLength() != _sg->getSequence(sgSeqID)->getLength()) { stringstream ss; ss << "Sanity check fail for Mapping " << i << " of path " << name << ": Segment size " << path[i].getLength() << " does not span " << "all of node " << (sgSeqID + 1) << " which has length " << _sg->getSequence(sgSeqID)->getLength(); throw runtime_error(ss.str()); } inputPathLength += path[i].getLength(); Value position; position.SetObject(); // node id's are 1-based in VG! addInt(position, "node_id", sgSeqID + 1); // Offsets are along the strand of the node that is being visited. // We always use the whole node. addInt(position, "offset", 0); addBool(position, "is_reverse", !path[i].getSide().getForward()); outputPathLength += _sg->getSequence(sgSeqID)->getLength(); Value mapping; mapping.SetObject(); mapping.AddMember("position", position, allocator()); addInt(mapping, "rank", rank + i + 1); mappings.PushBack(mapping, allocator()); } if (inputPathLength != outputPathLength) { stringstream ss; ss << "Sanity check fail for path " << name << ": input length (" << inputPathLength << ") != output length (" << outputPathLength << ")"; throw runtime_error(ss.str()); } jpath.AddMember("mapping", mappings, allocator()); paths().PushBack(jpath, allocator()); }
rapidjson::Value SceneConvertor::_Script_to_JSON( const Script* script, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); Value stringNode; stringNode.SetString( script->GetName().ToChar(), al ); json.AddMember( "name", stringNode, al ); json.AddMember( "enabled", script->IsEnabled(), al ); return json; }
void accept_binary_message(binary_message const& msg,Value& val,Document& doc,vector<shared_ptr<const string> >& buffers) { val.SetObject(); Value boolVal; boolVal.SetBool(true); val.AddMember(kBIN_PLACE_HOLDER, boolVal, doc.GetAllocator()); Value numVal; numVal.SetInt((int)buffers.size()); val.AddMember("num", numVal, doc.GetAllocator()); //FIXME can not avoid binary copy here. shared_ptr<string> write_buffer = make_shared<string>(); write_buffer->reserve(msg.get_binary()->size()+1); char frame_char = packet::frame_message; write_buffer->append(&frame_char,1); write_buffer->append(*(msg.get_binary())); buffers.push_back(write_buffer); }
void SG2VGJSON::addString(Value& value, const string& name, const string& v) { Value stringValue; stringValue.SetString(v.c_str(), v.length(), allocator()); Value nameValue; nameValue.SetString(name.c_str(), name.length(), allocator()); value.AddMember(nameValue, stringValue, allocator()); }
void SG2VGJSON::addInt(Value& value, const string& name, int v) { Value intValue; intValue.SetInt(v); Value nameValue; nameValue.SetString(name.c_str(), name.length(), allocator()); value.AddMember(nameValue, intValue, allocator()); }
void SG2VGJSON::addBool(Value& value, const string& name, bool v) { Value boolValue; boolValue.SetBool(v); Value nameValue; nameValue.SetString(name.c_str(), name.length(), allocator()); value.AddMember(nameValue, boolValue, allocator()); }
Value addControl(Document& root, const char* type, int x, int y, int w, int h, int id){ Document::AllocatorType& a = root.GetAllocator(); Value ctl; ctl.SetObject(); ctl.AddMember("type", StringRef(type), a); ctl.AddMember("id", id, a); Value frame; frame.SetObject(); frame.AddMember("x", x, a); frame.AddMember("y", y, a); frame.AddMember("w", w, a); frame.AddMember("h", h, a); ctl.AddMember("frame", frame, a); return ctl; }
bool ZatData::WriteDataJson() { void* file; if (!(file = XBMC->OpenFileForWrite(data_file.c_str(), true))) { XBMC->Log(LOG_ERROR, "Save data.json failed."); return false; } Document d; d.SetObject(); Value a(kArrayType); Document::AllocatorType& allocator = d.GetAllocator(); for (auto const& item : recordingsData) { if (!item.second->stillValid) { continue; } Value r; r.SetObject(); Value recordingId; recordingId.SetString(item.second->recordingId.c_str(), item.second->recordingId.length(), allocator); r.AddMember("recordingId", recordingId, allocator); r.AddMember("playCount", item.second->playCount, allocator); r.AddMember("lastPlayedPosition", item.second->lastPlayedPosition, allocator); a.PushBack(r, allocator); } d.AddMember("recordings", a, allocator); StringBuffer buffer; Writer<StringBuffer> writer(buffer); d.Accept(writer); const char* output = buffer.GetString(); XBMC->WriteFile(file, output, strlen(output)); XBMC->CloseFile(file); return true; }
void accept_object_message(object_message const& msg,Value& val,Document& doc,vector<shared_ptr<const string> >& buffers) { val.SetObject(); for (map<string,message::ptr>::const_iterator it = msg.get_map().begin(); it!= msg.get_map().end(); ++it) { Value nameVal; nameVal.SetString(it->first.data(), (SizeType)it->first.length(), doc.GetAllocator()); Value valueVal; accept_message(*(it->second), valueVal, doc,buffers); val.AddMember(nameVal, valueVal, doc.GetAllocator()); } }
rapidjson::Value SceneConvertor::_Camera_to_JSON( const Camera* camera, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); json.AddMember( "enabled", camera->IsEnabled(), al ); Value stringNode; stringNode.SetString( EnumConvertor::e2s_CameraType[ camera->GetType() ].ToChar(), al ); json.AddMember( "type", stringNode, al ); json.AddMember( "fovYAngle", camera->GetFovYAngle(), al ); json.AddMember( "nearClipping", camera->GetNearClipping(), al ); json.AddMember( "farClipping", camera->GetFarClipping(), al ); json.AddMember( "aspectRatio", camera->GetAspectRatio(), al ); json.AddMember( "orthoSize", _Vec2_to_JSON(camera->GetOrthoSize(), al), al ); //jsonAux.SetArray(); //for (uint i = 0; i < camera->GetImageEffectsCount(); ++i) //{ // jsonAux.PushBack( _ImageEffectToJSON( camera->GetImageEffectAt(i) ), al ); //} //json.AddMember("imageEffects", jsonAux, allocator); return json; }
rapidjson::Value SceneConvertor::_Light_to_JSON( const Light* light, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); json.AddMember( "enabled", light->IsEnabled(), al ); Value stringNode; stringNode.SetString( EnumConvertor::e2s_LightType[ light->GetType() ].ToChar(), al ); json.AddMember( "type", stringNode, al ); json.AddMember( "color", _Vec3_to_JSON(light->GetColor(), al), al ); json.AddMember( "intensity", light->GetIntensity(), al ); json.AddMember( "range", light->GetRange(), al ); json.AddMember( "spotAngle", light->GetSpotAngle(), al ); json.AddMember( "castShadows", light->IsCastingShadows(), al ); return json; }
/** * Specialisation of Options::SetImpl to store strings. * This specialisation is necessary because the value must be copied. * rapidjson requires that (a.) it explicitly be copied, or that (b.) it will * retain a reference that is guaranteed to be valid for longer than itself. * @param key The key to the value. * @param val The value to be stored. */ void Options::Set(const char *key, const char *val) { Document *d = static_cast<Document*>(m_doc); Value *fi = static_cast<Value*>(m_family_inst); if (!fi) { //Family doesn't exist, so create it Value family_key(m_family.c_str(), d->GetAllocator()); Value family_val(Type::kObjectType); Value entry_key(key, d->GetAllocator()); Value entry_val(val, d->GetAllocator()); family_val.AddMember(entry_key, entry_val, d->GetAllocator()); d->AddMember(family_key, family_val, d->GetAllocator()); m_family_inst = &(*d)[m_family.c_str()]; } else { //We have the family Value *existing = LGetValue(fi, key); if (existing) { //The entry was already there, so just update value existing->SetString(val, d->GetAllocator()); } else { //Create the new entry Value entry_key(key, d->GetAllocator()); Value entry_val(val, d->GetAllocator()); fi->AddMember(entry_key, entry_val, d->GetAllocator()); } } }
rapidjson::Value SceneConvertor::_MeshDrawing_to_JSON( const MeshDrawing* meshDrawing, rapidjson::Document::AllocatorType& al ) { Value json; json.SetObject(); json.AddMember( "enabled", meshDrawing->IsEnabled(), al ); json.AddMember( "castShadows", meshDrawing->IsCastingShadows(), al ); json.AddMember( "receiveShadows", meshDrawing->IsReceivingShadows(), al ); Value nullNode; nullNode.SetNull(); Value stringNode; if (meshDrawing->GetMesh() != NULL) { stringNode.SetString( meshDrawing->GetMesh()->GetName().ToChar(), al ); json.AddMember( "mesh", stringNode, al ); } else { json.AddMember( "mesh", nullNode, al ); } if (meshDrawing->GetMaterial() != NULL) { stringNode.SetString( meshDrawing->GetMaterial()->GetName().ToChar(), al ); json.AddMember( "material", stringNode, al ); } else { json.AddMember( "material", nullNode, al ); } return json; }
rapidjson::Document SettingsConvertor::Convert() { using namespace rapidjson; Document json; json.SetObject(); Document::AllocatorType& al = json.GetAllocator(); #if (ME3D_BUILD == ME3D_BUILD_EDITOR) json.AddMember( "FILE_TYPE", "EDITOR_CONFIG", al ); json.AddMember( "editorWidth", EditorApp::Settings::width, al ); json.AddMember( "editorHeight", EditorApp::Settings::height, al ); json.AddMember( "editorPosX", EditorApp::Settings::posX, al ); json.AddMember( "editorPosY", EditorApp::Settings::posY, al ); Value objectNode; Value stringNode; { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Hierarchy ]; objectNode.AddMember( "visible", wProp.visible, al ); json.AddMember( "window_hierarchy", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Resources ]; objectNode.AddMember( "visible", wProp.visible, al ); json.AddMember( "window_resources", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Console ]; objectNode.AddMember( "visible", wProp.visible, al ); json.AddMember( "window_console", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Properties ]; objectNode.AddMember( "visible", wProp.visible, al ); json.AddMember( "window_properties", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Game ]; objectNode.AddMember( "visible", wProp.visible, al ); json.AddMember( "window_game", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Scene_1 ]; objectNode.AddMember( "visible", wProp.visible, al ); stringNode.SetString( wProp.renderer.ToChar(), al ); objectNode.AddMember( "renderer", stringNode, al ); objectNode.AddMember( "camera", wProp.camera, al ); json.AddMember( "window_scene_1", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Scene_2 ]; objectNode.AddMember( "visible", wProp.visible, al ); stringNode.SetString( wProp.renderer.ToChar(), al ); objectNode.AddMember( "renderer", stringNode, al ); objectNode.AddMember( "camera", wProp.camera, al ); json.AddMember( "window_scene_2", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Scene_3 ]; objectNode.AddMember( "visible", wProp.visible, al ); stringNode.SetString( wProp.renderer.ToChar(), al ); objectNode.AddMember( "renderer", stringNode, al ); objectNode.AddMember( "camera", wProp.camera, al ); json.AddMember( "window_scene_3", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Scene_4 ]; objectNode.AddMember( "visible", wProp.visible, al ); stringNode.SetString( wProp.renderer.ToChar(), al ); objectNode.AddMember( "renderer", stringNode, al ); objectNode.AddMember( "camera", wProp.camera, al ); json.AddMember( "window_scene_4", objectNode, al ); } { objectNode.SetObject(); EditorApp::WindowProperty wProp = EditorApp::Settings::windows[ Windows::Preview ]; objectNode.AddMember( "visible", wProp.visible, al ); json.AddMember( "window_preview", objectNode, al ); } stringNode.SetString( EditorApp::Settings::startScene.ToChar(), al ); json.AddMember( "startScene", stringNode, al ); #endif return json; }
int main() { // Start the logger up first so all other code can be logged InitializeLogger(); // Log the time for the executing the function log_time(INFO); Value value; Document doc; // Create map with various values of different types value["integer"] = 32; value["string"] = "test string\nsecond line"; value["double"] = 5.532e-5; value["dateTime"].SetDateTime(time(NULL)); Value binary; char* binData = (char*)"\x0a\x0b\x0c\x0d\xff\x00\xee\xdd"; binary.SetBinary( (unsigned char*)binData, 8); value.AddMember( "binary", binary ); Value array; array.SetSize(4); array[0] = 5; array[1] = 10; array[2] = 15; array[3] = 20; value["array"] = array; // Write data to stdout cout << "Json data to stdout: " << endl; JsonWriter jsonWriter(stdoutStream); jsonWriter << value; cout << endl; // Write data to a string WriteStringStream strStream; JsonWriter jsonStrWriter(strStream); jsonStrWriter << value; const char *json = strStream.GetBuffer(); int jsonLength = strStream.Length(); cout << "Json data in string:" << endl << json << endl; // Use string data and parse in place. // This will modify the string and reference value strings directly from the input string. InSituStringStream sstream((char*)json, jsonLength); JsonReader jsonreader(sstream); jsonreader >> doc; if (jsonreader.HasParseError()) { cout << "Parse Error: " << jsonreader.GetParseErrorStr(); cout << "; at position " << jsonreader.GetErrorOffset() << endl; } else { // Display value using internal converter - close to Json format but not exact cout << "Value traversal after reading: " << endl; cout << doc.GetValue() << endl; } return 0; }