Object.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // Object.cpp
  3. //
  4. // Library: JSON
  5. // Package: JSON
  6. // Module: Object
  7. //
  8. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/JSON/Object.h"
  14. #include <iostream>
  15. #include <sstream>
  16. using Poco::Dynamic::Var;
  17. namespace Poco {
  18. namespace JSON {
  19. Object::Object(int options):
  20. _preserveInsOrder((options & Poco::JSON_PRESERVE_KEY_ORDER) != 0),
  21. _escapeUnicode((options & Poco::JSON_ESCAPE_UNICODE) != 0),
  22. _modified(false)
  23. {
  24. }
  25. Object::Object(const Object& other) : _values(other._values),
  26. _preserveInsOrder(other._preserveInsOrder),
  27. _escapeUnicode(other._escapeUnicode),
  28. _pStruct(!other._modified ? other._pStruct : 0),
  29. _modified(other._modified)
  30. {
  31. syncKeys(other._keys);
  32. }
  33. Object::Object(Object&& other) noexcept:
  34. _values(std::move(other._values)),
  35. _keys(std::move(other._keys)),
  36. _preserveInsOrder(other._preserveInsOrder),
  37. _escapeUnicode(other._escapeUnicode),
  38. _pStruct(std::move(other._pStruct)),
  39. _pOrdStruct(std::move(other._pOrdStruct)),
  40. _modified(other._modified)
  41. {
  42. }
  43. Object::~Object()
  44. {
  45. }
  46. Object &Object::operator = (const Object &other)
  47. {
  48. if (&other != this)
  49. {
  50. _values = other._values;
  51. _keys = other._keys;
  52. _preserveInsOrder = other._preserveInsOrder;
  53. _escapeUnicode = other._escapeUnicode;
  54. _pStruct = !other._modified ? other._pStruct : 0;
  55. _modified = other._modified;
  56. }
  57. return *this;
  58. }
  59. Object& Object::operator = (Object&& other) noexcept
  60. {
  61. _values = std::move(other._values);
  62. _keys = std::move(other._keys);
  63. _preserveInsOrder = other._preserveInsOrder;
  64. _escapeUnicode = other._escapeUnicode;
  65. _pStruct = std::move(other._pStruct);
  66. _pOrdStruct = std::move(other._pOrdStruct);
  67. _modified = other._modified;
  68. return *this;
  69. }
  70. void Object::syncKeys(const KeyList& keys)
  71. {
  72. if(_preserveInsOrder)
  73. {
  74. // update iterators in _keys to point to copied _values
  75. for(KeyList::const_iterator it = keys.begin(); it != keys.end(); ++it)
  76. {
  77. ValueMap::const_iterator itv = _values.find((*it)->first);
  78. poco_assert (itv != _values.end());
  79. _keys.push_back(itv);
  80. }
  81. }
  82. }
  83. Var Object::get(const std::string& key) const
  84. {
  85. ValueMap::const_iterator it = _values.find(key);
  86. if (it != _values.end())
  87. {
  88. return it->second;
  89. }
  90. return Var();
  91. }
  92. Array::Ptr Object::getArray(const std::string& key) const
  93. {
  94. ValueMap::const_iterator it = _values.find(key);
  95. if ((it != _values.end()) && (it->second.type() == typeid(Array::Ptr)))
  96. {
  97. return it->second.extract<Array::Ptr>();
  98. }
  99. return 0;
  100. }
  101. Object::Ptr Object::getObject(const std::string& key) const
  102. {
  103. ValueMap::const_iterator it = _values.find(key);
  104. if ((it != _values.end()) && (it->second.type() == typeid(Object::Ptr)))
  105. {
  106. return it->second.extract<Object::Ptr>();
  107. }
  108. return 0;
  109. }
  110. void Object::getNames(NameList& names) const
  111. {
  112. names.clear();
  113. if (_preserveInsOrder)
  114. {
  115. for(KeyList::const_iterator it = _keys.begin(); it != _keys.end(); ++it)
  116. {
  117. names.push_back((*it)->first);
  118. }
  119. }
  120. else
  121. {
  122. for(ValueMap::const_iterator it = _values.begin(); it != _values.end(); ++it)
  123. {
  124. names.push_back(it->first);
  125. }
  126. }
  127. }
  128. Object::NameList Object::getNames() const
  129. {
  130. NameList names;
  131. getNames(names);
  132. return names;
  133. }
  134. void Object::stringify(std::ostream& out, unsigned int indent, int step) const
  135. {
  136. if (step < 0) step = indent;
  137. if (!_preserveInsOrder)
  138. doStringify(_values, out, indent, step);
  139. else
  140. doStringify(_keys, out, indent, step);
  141. }
  142. const std::string& Object::getKey(KeyList::const_iterator& iter) const
  143. {
  144. ValueMap::const_iterator it = _values.begin();
  145. ValueMap::const_iterator end = _values.end();
  146. for (; it != end; ++it)
  147. {
  148. if (it == *iter) return it->first;
  149. }
  150. throw NotFoundException((*iter)->first);
  151. }
  152. Object& Object::set(const std::string& key, const Dynamic::Var& value)
  153. {
  154. std::pair<ValueMap::iterator, bool> ret = _values.insert(ValueMap::value_type(key, value));
  155. if (!ret.second) ret.first->second = value;
  156. if (_preserveInsOrder)
  157. {
  158. KeyList::iterator it = _keys.begin();
  159. KeyList::iterator end = _keys.end();
  160. for (; it != end; ++it)
  161. {
  162. if (key == (*it)->first) return *this;
  163. }
  164. _keys.push_back(ret.first);
  165. }
  166. _modified = true;
  167. return *this;
  168. }
  169. Poco::DynamicStruct Object::makeStruct(const Object::Ptr& obj)
  170. {
  171. return makeStructImpl<Poco::DynamicStruct>(obj);
  172. }
  173. Poco::OrderedDynamicStruct Object::makeOrderedStruct(const Object::Ptr& obj)
  174. {
  175. return makeStructImpl<Poco::OrderedDynamicStruct>(obj);
  176. }
  177. /*
  178. void Object::resetOrdDynStruct() const
  179. {
  180. if (!_pOrdStruct)
  181. _pOrdStruct = new Poco::OrderedDynamicStruct;
  182. else
  183. _pOrdStruct->clear();
  184. }
  185. */
  186. /*
  187. void Object::resetDynStruct() const
  188. {
  189. if (!_pStruct)
  190. _pStruct = new Poco::DynamicStruct;
  191. else
  192. _pStruct->clear();
  193. }*/
  194. Object::operator const Poco::DynamicStruct& () const
  195. {
  196. if (!_values.size())
  197. {
  198. resetDynStruct(_pStruct);
  199. }
  200. else if (_modified)
  201. {
  202. ValueMap::const_iterator it = _values.begin();
  203. ValueMap::const_iterator end = _values.end();
  204. resetDynStruct(_pStruct);
  205. for (; it != end; ++it)
  206. {
  207. if (isObject(it))
  208. {
  209. _pStruct->insert(it->first, makeStruct(getObject(it->first)));
  210. }
  211. else if (isArray(it))
  212. {
  213. _pStruct->insert(it->first, Poco::JSON::Array::makeArray(getArray(it->first)));
  214. }
  215. else
  216. {
  217. _pStruct->insert(it->first, it->second);
  218. }
  219. }
  220. }
  221. return *_pStruct;
  222. }
  223. Object::operator const Poco::OrderedDynamicStruct& () const
  224. {
  225. if (!_values.size())
  226. {
  227. resetDynStruct(_pOrdStruct);
  228. }
  229. else if (_modified)
  230. {
  231. if (_preserveInsOrder)
  232. {
  233. KeyList::const_iterator it = _keys.begin();
  234. KeyList::const_iterator end = _keys.end();
  235. resetDynStruct(_pOrdStruct);
  236. for (; it != end; ++it)
  237. {
  238. if (isObject((*it)->first))
  239. {
  240. _pOrdStruct->insert((*it)->first, makeOrderedStruct(getObject((*it)->first)));
  241. }
  242. else if (isArray((*it)->first))
  243. {
  244. _pOrdStruct->insert((*it)->first, Poco::JSON::Array::makeArray(getArray((*it)->first)));
  245. }
  246. else
  247. {
  248. _pOrdStruct->insert((*it)->first, (*it)->second);
  249. }
  250. }
  251. }
  252. else
  253. {
  254. ValueMap::const_iterator it = _values.begin();
  255. ValueMap::const_iterator end = _values.end();
  256. resetDynStruct(_pOrdStruct);
  257. for (; it != end; ++it)
  258. {
  259. if (isObject(it))
  260. {
  261. _pOrdStruct->insert(it->first, makeOrderedStruct(getObject(it->first)));
  262. }
  263. else if (isArray(it))
  264. {
  265. _pOrdStruct->insert(it->first, Poco::JSON::Array::makeArray(getArray(it->first)));
  266. }
  267. else
  268. {
  269. _pOrdStruct->insert(it->first, it->second);
  270. }
  271. }
  272. }
  273. }
  274. return *_pOrdStruct;
  275. }
  276. void Object::clear()
  277. {
  278. _values.clear();
  279. _keys.clear();
  280. _pStruct = 0;
  281. _modified = true;
  282. }
  283. } } // namespace Poco::JSON