JSONFile.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 Urho3D
  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. URHO3D_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. URHO3D_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. static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
  120. {
  121. switch (jsonValue.GetValueType())
  122. {
  123. case JSON_NULL:
  124. rapidjsonValue.SetNull();
  125. break;
  126. case JSON_BOOL:
  127. rapidjsonValue.SetBool(jsonValue.GetBool());
  128. break;
  129. case JSON_NUMBER:
  130. {
  131. switch (jsonValue.GetNumberType())
  132. {
  133. case JSONNT_INT:
  134. rapidjsonValue.SetInt(jsonValue.GetInt());
  135. break;
  136. case JSONNT_UINT:
  137. rapidjsonValue.SetUint(jsonValue.GetUInt());
  138. break;
  139. default:
  140. rapidjsonValue.SetDouble(jsonValue.GetDouble());
  141. break;
  142. }
  143. }
  144. break;
  145. case JSON_STRING:
  146. rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
  147. break;
  148. case JSON_ARRAY:
  149. {
  150. const JSONArray& jsonArray = jsonValue.GetArray();
  151. rapidjsonValue.SetArray();
  152. rapidjsonValue.Reserve(jsonArray.Size(), allocator);
  153. for (unsigned i = 0; i < jsonArray.Size(); ++i)
  154. {
  155. rapidjson::Value value;
  156. rapidjsonValue.PushBack(value, allocator);
  157. ToRapidjsonValue(rapidjsonValue[i], jsonArray[i], allocator);
  158. }
  159. }
  160. break;
  161. case JSON_OBJECT:
  162. {
  163. const JSONObject& jsonObject = jsonValue.GetObject();
  164. rapidjsonValue.SetObject();
  165. for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
  166. {
  167. const char* name = i->first_.CString();
  168. rapidjson::Value value;
  169. rapidjsonValue.AddMember(name, value, allocator);
  170. ToRapidjsonValue(rapidjsonValue[name], i->second_, allocator);
  171. }
  172. }
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. bool JSONFile::Save(Serializer& dest) const
  179. {
  180. return Save(dest, "\t");
  181. }
  182. bool JSONFile::Save(Serializer& dest, const String& indendation) const
  183. {
  184. rapidjson::Document document;
  185. ToRapidjsonValue(document, root_, document.GetAllocator());
  186. StringBuffer buffer;
  187. PrettyWriter<StringBuffer> writer(buffer, &(document.GetAllocator()));
  188. writer.SetIndent(!indendation.Empty() ? indendation.Front() : '\0', indendation.Length());
  189. document.Accept(writer);
  190. unsigned size = (unsigned)buffer.GetSize();
  191. return dest.Write(buffer.GetString(), size) == size;
  192. }
  193. }