JSONFile.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Core/Profiler.h"
  25. #include "../Core/Context.h"
  26. #include "../IO/Deserializer.h"
  27. #include "../IO/Log.h"
  28. #include "../IO/MemoryBuffer.h"
  29. #include "../Resource/JSONFile.h"
  30. #include "../Resource/ResourceCache.h"
  31. #include <rapidjson/document.h>
  32. #include <rapidjson/stringbuffer.h>
  33. #include <rapidjson/prettywriter.h>
  34. #include "../DebugNew.h"
  35. using namespace rapidjson;
  36. namespace Urho3D
  37. {
  38. JSONFile::JSONFile(Context* context) :
  39. Resource(context)
  40. {
  41. }
  42. JSONFile::~JSONFile() = default;
  43. void JSONFile::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<JSONFile>();
  46. }
  47. // Convert rapidjson value to JSON value.
  48. static void ToJSONValue(JSONValue& jsonValue, const rapidjson::Value& rapidjsonValue)
  49. {
  50. switch (rapidjsonValue.GetType())
  51. {
  52. case kNullType:
  53. // Reset to null type
  54. jsonValue.SetType(JSON_NULL);
  55. break;
  56. case kFalseType:
  57. jsonValue = false;
  58. break;
  59. case kTrueType:
  60. jsonValue = true;
  61. break;
  62. case kNumberType:
  63. if (rapidjsonValue.IsInt())
  64. jsonValue = rapidjsonValue.GetInt();
  65. else if (rapidjsonValue.IsUint())
  66. jsonValue = rapidjsonValue.GetUint();
  67. else
  68. jsonValue = rapidjsonValue.GetDouble();
  69. break;
  70. case kStringType:
  71. jsonValue = rapidjsonValue.GetString();
  72. break;
  73. case kArrayType:
  74. {
  75. jsonValue.Resize(rapidjsonValue.Size());
  76. for (unsigned i = 0; i < rapidjsonValue.Size(); ++i)
  77. {
  78. ToJSONValue(jsonValue[i], rapidjsonValue[i]);
  79. }
  80. }
  81. break;
  82. case kObjectType:
  83. {
  84. jsonValue.SetType(JSON_OBJECT);
  85. for (rapidjson::Value::ConstMemberIterator i = rapidjsonValue.MemberBegin(); i != rapidjsonValue.MemberEnd(); ++i)
  86. {
  87. JSONValue& value = jsonValue[String(i->name.GetString())];
  88. ToJSONValue(value, i->value);
  89. }
  90. }
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. bool JSONFile::BeginLoad(Deserializer& source)
  97. {
  98. unsigned dataSize = source.GetSize();
  99. if (!dataSize && !source.GetName().Empty())
  100. {
  101. URHO3D_LOGERROR("Zero sized JSON data in " + source.GetName());
  102. return false;
  103. }
  104. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  105. if (source.Read(buffer.Get(), dataSize) != dataSize)
  106. return false;
  107. buffer[dataSize] = '\0';
  108. rapidjson::Document document;
  109. if (document.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(buffer).HasParseError())
  110. {
  111. URHO3D_LOGERROR("Could not parse JSON data from " + source.GetName());
  112. return false;
  113. }
  114. ToJSONValue(root_, document);
  115. SetMemoryUse(dataSize);
  116. return true;
  117. }
  118. static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
  119. {
  120. switch (jsonValue.GetValueType())
  121. {
  122. case JSON_NULL:
  123. rapidjsonValue.SetNull();
  124. break;
  125. case JSON_BOOL:
  126. rapidjsonValue.SetBool(jsonValue.GetBool());
  127. break;
  128. case JSON_NUMBER:
  129. {
  130. switch (jsonValue.GetNumberType())
  131. {
  132. case JSONNT_INT:
  133. rapidjsonValue.SetInt(jsonValue.GetInt());
  134. break;
  135. case JSONNT_UINT:
  136. rapidjsonValue.SetUint(jsonValue.GetUInt());
  137. break;
  138. default:
  139. rapidjsonValue.SetDouble(jsonValue.GetDouble());
  140. break;
  141. }
  142. }
  143. break;
  144. case JSON_STRING:
  145. rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
  146. break;
  147. case JSON_ARRAY:
  148. {
  149. const JSONArray& jsonArray = jsonValue.GetArray();
  150. rapidjsonValue.SetArray();
  151. rapidjsonValue.Reserve(jsonArray.Size(), allocator);
  152. for (unsigned i = 0; i < jsonArray.Size(); ++i)
  153. {
  154. rapidjson::Value value;
  155. ToRapidjsonValue(value, jsonArray[i], allocator);
  156. rapidjsonValue.PushBack(value, allocator);
  157. }
  158. }
  159. break;
  160. case JSON_OBJECT:
  161. {
  162. const JSONObject& jsonObject = jsonValue.GetObject();
  163. rapidjsonValue.SetObject();
  164. for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
  165. {
  166. const char* name = i->first_.CString();
  167. rapidjson::Value value;
  168. ToRapidjsonValue(value, i->second_, allocator);
  169. rapidjsonValue.AddMember(StringRef(name), value, allocator);
  170. }
  171. }
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. bool JSONFile::Save(Serializer& dest) const
  178. {
  179. return Save(dest, "\t");
  180. }
  181. bool JSONFile::Save(Serializer& dest, const String& indendation) const
  182. {
  183. rapidjson::Document document;
  184. ToRapidjsonValue(document, root_, document.GetAllocator());
  185. StringBuffer buffer;
  186. PrettyWriter<StringBuffer> writer(buffer);
  187. writer.SetIndent(!indendation.Empty() ? indendation.Front() : '\0', indendation.Length());
  188. document.Accept(writer);
  189. auto size = (unsigned)buffer.GetSize();
  190. return dest.Write(buffer.GetString(), size) == size;
  191. }
  192. bool JSONFile::FromString(const String & source)
  193. {
  194. if (source.Empty())
  195. return false;
  196. MemoryBuffer buffer(source.CString(), source.Length());
  197. return Load(buffer);
  198. }
  199. }