JSONFile.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Core/Profiler.h"
  6. #include "../Core/Context.h"
  7. #include "../IO/Deserializer.h"
  8. #include "../IO/Log.h"
  9. #include "../IO/MemoryBuffer.h"
  10. #include "../Resource/JSONFile.h"
  11. #include "../Resource/ResourceCache.h"
  12. #include <rapidjson/document.h>
  13. #include <rapidjson/stringbuffer.h>
  14. #include <rapidjson/prettywriter.h>
  15. #include "../DebugNew.h"
  16. using namespace rapidjson;
  17. namespace Urho3D
  18. {
  19. JSONFile::JSONFile(Context* context) :
  20. Resource(context)
  21. {
  22. }
  23. JSONFile::~JSONFile() = default;
  24. void JSONFile::RegisterObject(Context* context)
  25. {
  26. context->RegisterFactory<JSONFile>();
  27. }
  28. // Convert rapidjson value to JSON value.
  29. static void ToJSONValue(JSONValue& jsonValue, const rapidjson::Value& rapidjsonValue)
  30. {
  31. switch (rapidjsonValue.GetType())
  32. {
  33. case kNullType:
  34. // Reset to null type
  35. jsonValue.SetType(JSON_NULL);
  36. break;
  37. case kFalseType:
  38. jsonValue = false;
  39. break;
  40. case kTrueType:
  41. jsonValue = true;
  42. break;
  43. case kNumberType:
  44. if (rapidjsonValue.IsInt())
  45. jsonValue = rapidjsonValue.GetInt();
  46. else if (rapidjsonValue.IsUint())
  47. jsonValue = rapidjsonValue.GetUint();
  48. else
  49. jsonValue = rapidjsonValue.GetDouble();
  50. break;
  51. case kStringType:
  52. jsonValue = rapidjsonValue.GetString();
  53. break;
  54. case kArrayType:
  55. {
  56. jsonValue.Resize(rapidjsonValue.Size());
  57. for (unsigned i = 0; i < rapidjsonValue.Size(); ++i)
  58. {
  59. ToJSONValue(jsonValue[i], rapidjsonValue[i]);
  60. }
  61. }
  62. break;
  63. case kObjectType:
  64. {
  65. jsonValue.SetType(JSON_OBJECT);
  66. for (rapidjson::Value::ConstMemberIterator i = rapidjsonValue.MemberBegin(); i != rapidjsonValue.MemberEnd(); ++i)
  67. {
  68. JSONValue& value = jsonValue[String(i->name.GetString())];
  69. ToJSONValue(value, i->value);
  70. }
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. bool JSONFile::BeginLoad(Deserializer& source)
  78. {
  79. unsigned dataSize = source.GetSize();
  80. if (!dataSize && !source.GetName().Empty())
  81. {
  82. URHO3D_LOGERROR("Zero sized JSON data in " + source.GetName());
  83. return false;
  84. }
  85. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  86. if (source.Read(buffer.Get(), dataSize) != dataSize)
  87. return false;
  88. buffer[dataSize] = '\0';
  89. rapidjson::Document document;
  90. if (document.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(buffer).HasParseError())
  91. {
  92. URHO3D_LOGERROR("Could not parse JSON data from " + source.GetName());
  93. return false;
  94. }
  95. ToJSONValue(root_, document);
  96. SetMemoryUse(dataSize);
  97. return true;
  98. }
  99. static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
  100. {
  101. switch (jsonValue.GetValueType())
  102. {
  103. case JSON_NULL:
  104. rapidjsonValue.SetNull();
  105. break;
  106. case JSON_BOOL:
  107. rapidjsonValue.SetBool(jsonValue.GetBool());
  108. break;
  109. case JSON_NUMBER:
  110. {
  111. switch (jsonValue.GetNumberType())
  112. {
  113. case JSONNT_INT:
  114. rapidjsonValue.SetInt(jsonValue.GetI32());
  115. break;
  116. case JSONNT_UINT:
  117. rapidjsonValue.SetUint(jsonValue.GetU32());
  118. break;
  119. default:
  120. rapidjsonValue.SetDouble(jsonValue.GetDouble());
  121. break;
  122. }
  123. }
  124. break;
  125. case JSON_STRING:
  126. rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
  127. break;
  128. case JSON_ARRAY:
  129. {
  130. const JSONArray& jsonArray = jsonValue.GetArray();
  131. rapidjsonValue.SetArray();
  132. rapidjsonValue.Reserve(jsonArray.Size(), allocator);
  133. for (const JSONValue& jsonValue : jsonArray)
  134. {
  135. rapidjson::Value value;
  136. ToRapidjsonValue(value, jsonValue, allocator);
  137. rapidjsonValue.PushBack(value, allocator);
  138. }
  139. }
  140. break;
  141. case JSON_OBJECT:
  142. {
  143. const JSONObject& jsonObject = jsonValue.GetObject();
  144. rapidjsonValue.SetObject();
  145. for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
  146. {
  147. const char* name = i->first_.CString();
  148. rapidjson::Value value;
  149. ToRapidjsonValue(value, i->second_, allocator);
  150. rapidjsonValue.AddMember(StringRef(name), value, allocator);
  151. }
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. bool JSONFile::Save(Serializer& dest) const
  159. {
  160. return Save(dest, "\t");
  161. }
  162. bool JSONFile::Save(Serializer& dest, const String& indendation) const
  163. {
  164. rapidjson::Document document;
  165. ToRapidjsonValue(document, root_, document.GetAllocator());
  166. StringBuffer buffer;
  167. PrettyWriter<StringBuffer> writer(buffer);
  168. writer.SetIndent(!indendation.Empty() ? indendation.Front() : '\0', indendation.Length());
  169. document.Accept(writer);
  170. auto size = (unsigned)buffer.GetSize();
  171. return dest.Write(buffer.GetString(), size) == size;
  172. }
  173. bool JSONFile::FromString(const String & source)
  174. {
  175. if (source.Empty())
  176. return false;
  177. MemoryBuffer buffer(source.CString(), source.Length());
  178. return Load(buffer);
  179. }
  180. String JSONFile::ToString(const String& indendation) const
  181. {
  182. rapidjson::Document document;
  183. ToRapidjsonValue(document, root_, document.GetAllocator());
  184. StringBuffer buffer;
  185. PrettyWriter<StringBuffer> writer(buffer);
  186. writer.SetIndent(!indendation.Empty() ? indendation.Front() : '\0', indendation.Length());
  187. document.Accept(writer);
  188. return buffer.GetString();
  189. }
  190. }