| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Container/ArrayPtr.h"
- #include "../Core/Profiler.h"
- #include "../Core/Context.h"
- #include "../IO/Deserializer.h"
- #include "../IO/Log.h"
- #include "../IO/MemoryBuffer.h"
- #include "../Resource/JSONFile.h"
- #include "../Resource/ResourceCache.h"
- #include <rapidjson/document.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/prettywriter.h>
- #include "../DebugNew.h"
- using namespace rapidjson;
- namespace Atomic
- {
- JSONFile::JSONFile(Context* context) :
- Resource(context)
- {
- }
- JSONFile::~JSONFile()
- {
- }
- void JSONFile::RegisterObject(Context* context)
- {
- context->RegisterFactory<JSONFile>();
- }
- // Convert rapidjson value to JSON value.
- static void ToJSONValue(JSONValue& jsonValue, const rapidjson::Value& rapidjsonValue)
- {
- switch (rapidjsonValue.GetType())
- {
- case kNullType:
- // Reset to null type
- jsonValue.SetType(JSON_NULL);
- break;
- case kFalseType:
- jsonValue = false;
- break;
- case kTrueType:
- jsonValue = true;
- break;
- case kNumberType:
- if (rapidjsonValue.IsInt())
- jsonValue = rapidjsonValue.GetInt();
- else if (rapidjsonValue.IsUint())
- jsonValue = rapidjsonValue.GetUint();
- else
- jsonValue = rapidjsonValue.GetDouble();
- break;
- case kStringType:
- jsonValue = rapidjsonValue.GetString();
- break;
- case kArrayType:
- {
- jsonValue.Resize(rapidjsonValue.Size());
- for (unsigned i = 0; i < rapidjsonValue.Size(); ++i)
- {
- ToJSONValue(jsonValue[i], rapidjsonValue[i]);
- }
- }
- break;
- case kObjectType:
- {
- jsonValue.SetType(JSON_OBJECT);
- for (rapidjson::Value::ConstMemberIterator i = rapidjsonValue.MemberBegin(); i != rapidjsonValue.MemberEnd(); ++i)
- {
- JSONValue& value = jsonValue[String(i->name.GetString())];
- ToJSONValue(value, i->value);
- }
- }
- break;
- default:
- break;
- }
- }
- bool JSONFile::BeginLoad(Deserializer& source)
- {
- unsigned dataSize = source.GetSize();
- if (!dataSize && !source.GetName().Empty())
- {
- ATOMIC_LOGERROR("Zero sized JSON data in " + source.GetName());
- return false;
- }
- SharedArrayPtr<char> buffer(new char[dataSize + 1]);
- if (source.Read(buffer.Get(), dataSize) != dataSize)
- return false;
- buffer[dataSize] = '\0';
- rapidjson::Document document;
- if (document.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(buffer).HasParseError())
- {
- ATOMIC_LOGERROR("Could not parse JSON data from " + source.GetName());
- return false;
- }
- ToJSONValue(root_, document);
- SetMemoryUse(dataSize);
- return true;
- }
- static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
- {
- switch (jsonValue.GetValueType())
- {
- case JSON_NULL:
- rapidjsonValue.SetNull();
- break;
- case JSON_BOOL:
- rapidjsonValue.SetBool(jsonValue.GetBool());
- break;
- case JSON_NUMBER:
- {
- switch (jsonValue.GetNumberType())
- {
- case JSONNT_INT:
- rapidjsonValue.SetInt(jsonValue.GetInt());
- break;
- case JSONNT_UINT:
- rapidjsonValue.SetUint(jsonValue.GetUInt());
- break;
- default:
- rapidjsonValue.SetDouble(jsonValue.GetDouble());
- break;
- }
- }
- break;
- case JSON_STRING:
- rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
- break;
- case JSON_ARRAY:
- {
- const JSONArray& jsonArray = jsonValue.GetArray();
- rapidjsonValue.SetArray();
- rapidjsonValue.Reserve(jsonArray.Size(), allocator);
- for (unsigned i = 0; i < jsonArray.Size(); ++i)
- {
- rapidjson::Value value;
- ToRapidjsonValue(value, jsonArray[i], allocator);
- rapidjsonValue.PushBack(value, allocator);
- }
- }
- break;
- case JSON_OBJECT:
- {
- const JSONObject& jsonObject = jsonValue.GetObject();
- rapidjsonValue.SetObject();
- for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
- {
- const char* name = i->first_.CString();
- rapidjson::Value value;
- ToRapidjsonValue(value, i->second_, allocator);
- rapidjsonValue.AddMember(StringRef(name), value, allocator);
- }
- }
- break;
- default:
- break;
- }
- }
- bool JSONFile::Save(Serializer& dest) const
- {
- return Save(dest, "\t");
- }
- bool JSONFile::Save(Serializer& dest, const String& indendation) const
- {
- rapidjson::Document document;
- ToRapidjsonValue(document, root_, document.GetAllocator());
- StringBuffer buffer;
- // ATOMIC BEGIN
- PrettyWriter<StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> > writer(buffer, &(document.GetAllocator()));
- // ATOMIC END
- writer.SetIndent(!indendation.Empty() ? indendation.Front() : '\0', indendation.Length());
- document.Accept(writer);
- unsigned size = (unsigned)buffer.GetSize();
- return dest.Write(buffer.GetString(), size) == size;
- }
- bool JSONFile::FromString(const String & source)
- {
- if (source.Empty())
- return false;
- MemoryBuffer buffer(source.CString(), source.Length());
- return Load(buffer);
- }
- // ATOMIC BEGIN
- bool JSONFile::ParseJSON(const String& json, JSONValue& value, bool reportError)
- {
- rapidjson::Document document;
- if (document.Parse<0>(json.CString()).HasParseError())
- {
- if (reportError)
- ATOMIC_LOGERRORF("Could not parse JSON data from string with error: %s", document.GetParseError());
- return false;
- }
- ToJSONValue(value, document);
- return true;
- }
- // ATOMIC END
- }
|