JSONFile.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 Atomic
  37. {
  38. JSONFile::JSONFile(Context* context) :
  39. Resource(context)
  40. {
  41. }
  42. JSONFile::~JSONFile()
  43. {
  44. }
  45. void JSONFile::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<JSONFile>();
  48. }
  49. // Convert rapidjson value to JSON value.
  50. static void ToJSONValue(JSONValue& jsonValue, const rapidjson::Value& rapidjsonValue)
  51. {
  52. switch (rapidjsonValue.GetType())
  53. {
  54. case kNullType:
  55. // Reset to null type
  56. jsonValue.SetType(JSON_NULL);
  57. break;
  58. case kFalseType:
  59. jsonValue = false;
  60. break;
  61. case kTrueType:
  62. jsonValue = true;
  63. break;
  64. case kNumberType:
  65. if (rapidjsonValue.IsInt())
  66. jsonValue = rapidjsonValue.GetInt();
  67. else if (rapidjsonValue.IsUint())
  68. jsonValue = rapidjsonValue.GetUint();
  69. else
  70. jsonValue = rapidjsonValue.GetDouble();
  71. break;
  72. case kStringType:
  73. jsonValue = rapidjsonValue.GetString();
  74. break;
  75. case kArrayType:
  76. {
  77. jsonValue.Resize(rapidjsonValue.Size());
  78. for (unsigned i = 0; i < rapidjsonValue.Size(); ++i)
  79. {
  80. ToJSONValue(jsonValue[i], rapidjsonValue[i]);
  81. }
  82. }
  83. break;
  84. case kObjectType:
  85. {
  86. jsonValue.SetType(JSON_OBJECT);
  87. for (rapidjson::Value::ConstMemberIterator i = rapidjsonValue.MemberBegin(); i != rapidjsonValue.MemberEnd(); ++i)
  88. {
  89. JSONValue& value = jsonValue[String(i->name.GetString())];
  90. ToJSONValue(value, i->value);
  91. }
  92. }
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. bool JSONFile::BeginLoad(Deserializer& source)
  99. {
  100. unsigned dataSize = source.GetSize();
  101. if (!dataSize && !source.GetName().Empty())
  102. {
  103. ATOMIC_LOGERROR("Zero sized JSON data in " + source.GetName());
  104. return false;
  105. }
  106. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  107. if (source.Read(buffer.Get(), dataSize) != dataSize)
  108. return false;
  109. buffer[dataSize] = '\0';
  110. rapidjson::Document document;
  111. if (document.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(buffer).HasParseError())
  112. {
  113. ATOMIC_LOGERROR("Could not parse JSON data from " + source.GetName());
  114. return false;
  115. }
  116. ToJSONValue(root_, document);
  117. SetMemoryUse(dataSize);
  118. return true;
  119. }
  120. static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
  121. {
  122. switch (jsonValue.GetValueType())
  123. {
  124. case JSON_NULL:
  125. rapidjsonValue.SetNull();
  126. break;
  127. case JSON_BOOL:
  128. rapidjsonValue.SetBool(jsonValue.GetBool());
  129. break;
  130. case JSON_NUMBER:
  131. {
  132. switch (jsonValue.GetNumberType())
  133. {
  134. case JSONNT_INT:
  135. rapidjsonValue.SetInt(jsonValue.GetInt());
  136. break;
  137. case JSONNT_UINT:
  138. rapidjsonValue.SetUint(jsonValue.GetUInt());
  139. break;
  140. default:
  141. rapidjsonValue.SetDouble(jsonValue.GetDouble());
  142. break;
  143. }
  144. }
  145. break;
  146. case JSON_STRING:
  147. rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
  148. break;
  149. case JSON_ARRAY:
  150. {
  151. const JSONArray& jsonArray = jsonValue.GetArray();
  152. rapidjsonValue.SetArray();
  153. rapidjsonValue.Reserve(jsonArray.Size(), allocator);
  154. for (unsigned i = 0; i < jsonArray.Size(); ++i)
  155. {
  156. rapidjson::Value value;
  157. ToRapidjsonValue(value, jsonArray[i], allocator);
  158. rapidjsonValue.PushBack(value, allocator);
  159. }
  160. }
  161. break;
  162. case JSON_OBJECT:
  163. {
  164. const JSONObject& jsonObject = jsonValue.GetObject();
  165. rapidjsonValue.SetObject();
  166. for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
  167. {
  168. const char* name = i->first_.CString();
  169. rapidjson::Value value;
  170. ToRapidjsonValue(value, i->second_, allocator);
  171. rapidjsonValue.AddMember(StringRef(name), value, allocator);
  172. }
  173. }
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. bool JSONFile::Save(Serializer& dest) const
  180. {
  181. return Save(dest, "\t");
  182. }
  183. bool JSONFile::Save(Serializer& dest, const String& indendation) const
  184. {
  185. rapidjson::Document document;
  186. ToRapidjsonValue(document, root_, document.GetAllocator());
  187. StringBuffer buffer;
  188. // ATOMIC BEGIN
  189. PrettyWriter<StringBuffer, rapidjson::UTF8<>, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> > writer(buffer, &(document.GetAllocator()));
  190. // ATOMIC END
  191. writer.SetIndent(!indendation.Empty() ? indendation.Front() : '\0', indendation.Length());
  192. document.Accept(writer);
  193. unsigned size = (unsigned)buffer.GetSize();
  194. return dest.Write(buffer.GetString(), size) == size;
  195. }
  196. bool JSONFile::FromString(const String & source)
  197. {
  198. if (source.Empty())
  199. return false;
  200. MemoryBuffer buffer(source.CString(), source.Length());
  201. return Load(buffer);
  202. }
  203. // ATOMIC BEGIN
  204. bool JSONFile::ParseJSON(const String& json, JSONValue& value, bool reportError)
  205. {
  206. rapidjson::Document document;
  207. if (document.Parse<0>(json.CString()).HasParseError())
  208. {
  209. if (reportError)
  210. ATOMIC_LOGERRORF("Could not parse JSON data from string with error: %s", document.GetParseError());
  211. return false;
  212. }
  213. ToJSONValue(value, document);
  214. return true;
  215. }
  216. // ATOMIC END
  217. }