JsonValue.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonArray.h>
  7. #include <AK/JsonObject.h>
  8. #include <AK/JsonValue.h>
  9. #ifndef KERNEL
  10. # include <AK/JsonParser.h>
  11. #endif
  12. namespace AK {
  13. JsonValue::JsonValue(Type type)
  14. : m_type(type)
  15. {
  16. }
  17. JsonValue::JsonValue(const JsonValue& other)
  18. {
  19. copy_from(other);
  20. }
  21. JsonValue& JsonValue::operator=(const JsonValue& other)
  22. {
  23. if (this != &other) {
  24. clear();
  25. copy_from(other);
  26. }
  27. return *this;
  28. }
  29. void JsonValue::copy_from(const JsonValue& other)
  30. {
  31. m_type = other.m_type;
  32. switch (m_type) {
  33. case Type::String:
  34. VERIFY(!m_value.as_string);
  35. m_value.as_string = other.m_value.as_string;
  36. m_value.as_string->ref();
  37. break;
  38. case Type::Object:
  39. m_value.as_object = new JsonObject(*other.m_value.as_object);
  40. break;
  41. case Type::Array:
  42. m_value.as_array = new JsonArray(*other.m_value.as_array);
  43. break;
  44. default:
  45. m_value.as_u64 = other.m_value.as_u64;
  46. break;
  47. }
  48. }
  49. JsonValue::JsonValue(JsonValue&& other)
  50. {
  51. m_type = exchange(other.m_type, Type::Null);
  52. m_value.as_u64 = exchange(other.m_value.as_u64, 0);
  53. }
  54. JsonValue& JsonValue::operator=(JsonValue&& other)
  55. {
  56. if (this != &other) {
  57. clear();
  58. m_type = exchange(other.m_type, Type::Null);
  59. m_value.as_u64 = exchange(other.m_value.as_u64, 0);
  60. }
  61. return *this;
  62. }
  63. bool JsonValue::equals(const JsonValue& other) const
  64. {
  65. if (is_null() && other.is_null())
  66. return true;
  67. if (is_bool() && other.is_bool() && as_bool() == other.as_bool())
  68. return true;
  69. if (is_string() && other.is_string() && as_string() == other.as_string())
  70. return true;
  71. #if !defined(KERNEL)
  72. if (is_number() && other.is_number() && to_number<double>() == other.to_number<double>()) {
  73. return true;
  74. }
  75. #else
  76. if (is_number() && other.is_number() && to_number<i64>() == other.to_number<i64>()) {
  77. return true;
  78. }
  79. #endif
  80. if (is_array() && other.is_array() && as_array().size() == other.as_array().size()) {
  81. bool result = true;
  82. for (size_t i = 0; i < as_array().size(); ++i) {
  83. result &= as_array().at(i).equals(other.as_array().at(i));
  84. }
  85. return result;
  86. }
  87. if (is_object() && other.is_object() && as_object().size() == other.as_object().size()) {
  88. bool result = true;
  89. as_object().for_each_member([&](auto& key, auto& value) {
  90. result &= value.equals(other.as_object().get(key));
  91. });
  92. return result;
  93. }
  94. return false;
  95. }
  96. JsonValue::JsonValue(int value)
  97. : m_type(Type::Int32)
  98. {
  99. m_value.as_i32 = value;
  100. }
  101. JsonValue::JsonValue(unsigned value)
  102. : m_type(Type::UnsignedInt32)
  103. {
  104. m_value.as_u32 = value;
  105. }
  106. JsonValue::JsonValue(long value)
  107. : m_type(sizeof(long) == 8 ? Type::Int64 : Type::Int32)
  108. {
  109. if constexpr (sizeof(long) == 8)
  110. m_value.as_i64 = value;
  111. else
  112. m_value.as_i32 = value;
  113. }
  114. JsonValue::JsonValue(unsigned long value)
  115. : m_type(sizeof(long) == 8 ? Type::UnsignedInt64 : Type::UnsignedInt32)
  116. {
  117. if constexpr (sizeof(long) == 8)
  118. m_value.as_u64 = value;
  119. else
  120. m_value.as_u32 = value;
  121. }
  122. JsonValue::JsonValue(long long value)
  123. : m_type(Type::Int64)
  124. {
  125. static_assert(sizeof(long long unsigned) == 8);
  126. m_value.as_i64 = value;
  127. }
  128. JsonValue::JsonValue(long long unsigned value)
  129. : m_type(Type::UnsignedInt64)
  130. {
  131. static_assert(sizeof(long long unsigned) == 8);
  132. m_value.as_u64 = value;
  133. }
  134. JsonValue::JsonValue(const char* cstring)
  135. : JsonValue(String(cstring))
  136. {
  137. }
  138. #if !defined(KERNEL)
  139. JsonValue::JsonValue(double value)
  140. : m_type(Type::Double)
  141. {
  142. m_value.as_double = value;
  143. }
  144. #endif
  145. JsonValue::JsonValue(bool value)
  146. : m_type(Type::Bool)
  147. {
  148. m_value.as_bool = value;
  149. }
  150. JsonValue::JsonValue(const String& value)
  151. {
  152. if (value.is_null()) {
  153. m_type = Type::Null;
  154. } else {
  155. m_type = Type::String;
  156. m_value.as_string = const_cast<StringImpl*>(value.impl());
  157. m_value.as_string->ref();
  158. }
  159. }
  160. JsonValue::JsonValue(const JsonObject& value)
  161. : m_type(Type::Object)
  162. {
  163. m_value.as_object = new JsonObject(value);
  164. }
  165. JsonValue::JsonValue(const JsonArray& value)
  166. : m_type(Type::Array)
  167. {
  168. m_value.as_array = new JsonArray(value);
  169. }
  170. JsonValue::JsonValue(JsonObject&& value)
  171. : m_type(Type::Object)
  172. {
  173. m_value.as_object = new JsonObject(move(value));
  174. }
  175. JsonValue::JsonValue(JsonArray&& value)
  176. : m_type(Type::Array)
  177. {
  178. m_value.as_array = new JsonArray(move(value));
  179. }
  180. void JsonValue::clear()
  181. {
  182. switch (m_type) {
  183. case Type::String:
  184. m_value.as_string->unref();
  185. break;
  186. case Type::Object:
  187. delete m_value.as_object;
  188. break;
  189. case Type::Array:
  190. delete m_value.as_array;
  191. break;
  192. default:
  193. break;
  194. }
  195. m_type = Type::Null;
  196. m_value.as_string = nullptr;
  197. }
  198. #ifndef KERNEL
  199. ErrorOr<JsonValue> JsonValue::from_string(StringView input)
  200. {
  201. if (input.is_empty())
  202. return JsonValue();
  203. return JsonParser(input).parse();
  204. }
  205. #endif
  206. }