JSONFile.cpp 6.9 KB

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