// save .asset bool Asset::Save() { FileSystem* fs = GetSubsystem<FileSystem>(); String assetFilename = GetDotAssetFilename(); json_ = new JSONFile(context_); JSONValue& root = json_->GetRoot(); root.Set("version", JSONValue(ASSET_VERSION)); root.Set("guid", JSONValue(guid_)); // handle import if (importer_.NotNull()) { importer_->SaveSettings(root); SharedPtr<File> file(new File(context_, assetFilename, FILE_WRITE)); json_->Save(*file); file->Close(); } json_ = 0; return true; }
spark::JSONValue spark::JSONValue::parse(char *json, size_t size) { detail::JSONDataPtr d(new(std::nothrow) detail::JSONData); if (!d) { return JSONValue(); } size_t tokenCount = 0; if (!tokenize(json, size, &d->tokens, &tokenCount)) { return JSONValue(); } const jsmntok_t *t = d->tokens; // Root token if (t->type == JSMN_PRIMITIVE) { // RFC 7159 allows JSON document to consist of a single primitive value, such as a number. // In this case, original data is copied to a larger buffer to ensure room for term. null // character (see stringize() method) d->json = new(std::nothrow) char[size + 1]; if (!d->json) { return JSONValue(); } memcpy(d->json, json, size); d->freeJson = true; // Set ownership flag } else { d->json = json; } if (!stringize(d->tokens, tokenCount, d->json)) { return JSONValue(); } return JSONValue(t, d); }
JSONValue& JSONValue::operator = (const Variant& rhs) { switch (rhs.GetType()) { case VAR_NONE: *this = JSONValue(); case VAR_BOOL: *this = rhs.GetBool(); break; case VAR_INT: *this = rhs.GetInt(); break; case VAR_FLOAT: case VAR_DOUBLE: *this = rhs.GetDouble(); break; case VAR_STRING: *this = rhs.GetString(); break; default: LogError("JSONValue::operator = unsupported variant type " + rhs.GetTypeName()); break; } return *this; }
JSONValue JSONFile::CreateRoot(JSONValueType valueType) { if (valueType == JSON_OBJECT) document_->SetObject(); else document_->SetArray(); return JSONValue(this, document_); }
bool TypeScriptImporter::SaveSettingsInternal(JSONValue& jsonRoot) { if (!AssetImporter::SaveSettingsInternal(jsonRoot)) return false; JSONValue import; import.Set("IsComponentFile", JSONValue(isComponentFile_)); jsonRoot.Set("TypeScriptImporter", import); return true; }
spark::JSONValue spark::JSONValue::parseCopy(const char *json, size_t size) { detail::JSONDataPtr d(new(std::nothrow) detail::JSONData); if (!d) { return JSONValue(); } size_t tokenCount = 0; if (!tokenize(json, size, &d->tokens, &tokenCount)) { return JSONValue(); } d->json = new(std::nothrow) char[size + 1]; if (!d->json) { return JSONValue(); } memcpy(d->json, json, size); // TODO: Copy only token data d->freeJson = true; if (!stringize(d->tokens, tokenCount, d->json)) { return JSONValue(); } return JSONValue(d->tokens, d); }
JSONValue JSONValue::GetChild(unsigned index, JSONValueType valueType) const { if (index >= GetSize()) return JSONValue::EMPTY; const Value& value = (*value_)[(SizeType)index]; if (valueType != JSON_ANY && value.GetType() != ToRapidJsonType(valueType)) return JSONValue::EMPTY; return JSONValue(file_, (Value*)&value); }
JSONValue JSONValue::GetChild(const String& name, JSONValueType valueType) const { assert(IsObject()); if (!value_->HasMember(name.CString())) return JSONValue::EMPTY; Value& value = GetMember(name); if (valueType != JSON_ANY && value.GetType() != ToRapidJsonType(valueType)) return JSONValue::EMPTY; return JSONValue(file_, &value); }
JSONValue JSONFile::GetRoot(JSONValueType valueType) { if (!document_) return JSONValue::EMPTY; if ((valueType == JSON_OBJECT && document_->GetType() != kObjectType) || (valueType == JSON_ARRAY && document_->GetType() != kArrayType)) { LOGERROR("Invalid root value type"); return JSONValue::EMPTY; } return JSONValue(this, document_); }
JSONValue Global::serialize() { string json_result; JSONObject json; json["encoders"] = serializeCfgMap(_encoders); json["channels"] = serializeCfgMap(_channels); JSONObject j_obj_config; for(map<string, string>::iterator it = _config.begin(); it != _config.end(); ++it) { j_obj_config[it->first] = new JSONValue(it->second); } json["config"] = new JSONValue(j_obj_config); return JSONValue(json); }
void Global::saveScenes() { JSONObject j_scenes; for(map<string, map<string, map<string, Value> > >::iterator it_scene = _scenes.begin(); it_scene != _scenes.end(); ++it_scene) { JSONObject j_sources; for(map<string, map<string, Value> >::iterator it_source = it_scene->second.begin(); it_source != it_scene->second.end(); ++it_source) { JSONObject j_source_settings; for(map<string, Value>::iterator it_source_settings = it_source->second.begin(); it_source_settings != it_source->second.end(); ++it_source_settings) { j_source_settings[it_source_settings->first] = new JSONValue(it_source_settings->second.toDouble()); } j_sources[it_source->first] = new JSONValue(j_source_settings); } j_scenes[it_scene->first] = new JSONValue(j_sources); } string j_scenes_str = JSONValue(j_scenes).stringify(); global.writeFile("./config/scenes.cfg", j_scenes_str); }
JSON::JSONValue JSON::fromNumber(const Number& value) { return JSONValue(CGAL::to_double(value)); }