JSONNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "JSONNode.h"
  2. #define IMPLEMENT_CTOR(type)\
  3. JSONNode::JSONNode(const json_string & name_t, type value_t) json_nothrow : internal(internalJSONNode::newInternal()){\
  4. internal -> Set(value_t);\
  5. internal -> setname(name_t);\
  6. LIBJSON_CTOR;\
  7. }
  8. IMPLEMENT_FOR_ALL_TYPES(IMPLEMENT_CTOR)
  9. #ifndef JSON_LIBRARY
  10. JSONNode::JSONNode(const json_string & name_t, const json_char * value_t) json_nothrow : internal(internalJSONNode::newInternal()){
  11. internal -> Set(json_string(value_t));
  12. internal -> setname(name_t);
  13. LIBJSON_CTOR;
  14. }
  15. #endif
  16. #if (defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY))
  17. #include "JSONWorker.h"
  18. JSONNode JSONNode::stringType(const json_string & str){
  19. JSONNode res;
  20. res.set_name(json_global(EMPTY_JSON_STRING));
  21. #ifdef JSON_LESS_MEMORY
  22. res = JSONWorker::FixString(str, res.internal, false);
  23. #else
  24. res = JSONWorker::FixString(str, res.internal -> _string_encoded);
  25. #endif
  26. return res;
  27. }
  28. void JSONNode::set_name_(const json_string & newname) json_nothrow {
  29. #ifdef JSON_LESS_MEMORY
  30. json_string _newname = JSONWorker::FixString(newname, internal, true);
  31. #else
  32. json_string _newname = JSONWorker::FixString(newname, internal -> _name_encoded);
  33. #endif
  34. set_name(_newname);
  35. }
  36. #endif
  37. #ifdef JSON_CASTABLE
  38. JSONNode JSONNode::as_node(void) const json_nothrow {
  39. JSON_CHECK_INTERNAL();
  40. if (type() == JSON_NODE){
  41. return *this;
  42. } else if (type() == JSON_ARRAY){
  43. JSONNode res(duplicate());
  44. res.internal -> _type = JSON_NODE;
  45. return res;
  46. }
  47. #ifdef JSON_MUTEX_CALLBACKS
  48. if (internal -> mylock != 0){
  49. JSONNode res(JSON_NODE);
  50. res.set_mutex(internal -> mylock);
  51. return res;
  52. }
  53. #endif
  54. return JSONNode(JSON_NODE);
  55. }
  56. JSONNode JSONNode::as_array(void) const json_nothrow {
  57. JSON_CHECK_INTERNAL();
  58. if (type() == JSON_ARRAY){
  59. return *this;
  60. } else if (type() == JSON_NODE){
  61. JSONNode res(duplicate());
  62. res.internal -> _type = JSON_ARRAY;
  63. json_foreach(res.internal -> CHILDREN, runner){
  64. (*runner) -> clear_name();
  65. }
  66. return res;
  67. }
  68. #ifdef JSON_MUTEX_CALLBACKS
  69. if (internal -> mylock != 0){
  70. JSONNode res(JSON_ARRAY);
  71. res.set_mutex(internal -> mylock);
  72. return res;
  73. }
  74. #endif
  75. return JSONNode(JSON_ARRAY);
  76. }
  77. void JSONNode::cast(char newtype) json_nothrow {
  78. JSON_CHECK_INTERNAL();
  79. if (newtype == type()) return;
  80. switch(newtype){
  81. case JSON_NULL:
  82. nullify();
  83. return;
  84. case JSON_STRING:
  85. *this = as_string();
  86. return;
  87. case JSON_NUMBER:
  88. *this = as_float();
  89. return;
  90. case JSON_BOOL:
  91. *this = as_bool();
  92. return;
  93. case JSON_ARRAY:
  94. *this = as_array();
  95. return;
  96. case JSON_NODE:
  97. *this = as_node();
  98. return;
  99. }
  100. JSON_FAIL(JSON_TEXT("cast to unknown type"));
  101. }
  102. #endif
  103. //different just to supress the warning
  104. #ifdef JSON_REF_COUNT
  105. void JSONNode::merge(JSONNode & other) json_nothrow {
  106. #else
  107. void JSONNode::merge(JSONNode &) json_nothrow {
  108. #endif
  109. JSON_CHECK_INTERNAL();
  110. #ifdef JSON_REF_COUNT
  111. if (internal == other.internal) return;
  112. JSON_ASSERT(*this == other, JSON_TEXT("merging two nodes that aren't equal"));
  113. if (internal -> refcount < other.internal -> refcount){
  114. *this = other;
  115. } else {
  116. other = *this;
  117. }
  118. #endif
  119. }
  120. #ifdef JSON_REF_COUNT
  121. void JSONNode::merge(JSONNode * other) json_nothrow {
  122. JSON_CHECK_INTERNAL();
  123. if (internal == other -> internal) return;
  124. *other = *this;
  125. }
  126. //different just to supress the warning
  127. void JSONNode::merge(unsigned int num, ...) json_nothrow {
  128. #else
  129. void JSONNode::merge(unsigned int, ...) json_nothrow {
  130. #endif
  131. JSON_CHECK_INTERNAL();
  132. #ifdef JSON_REF_COUNT
  133. va_list args;
  134. va_start(args, num);
  135. for(unsigned int i = 0; i < num; ++i){
  136. merge(va_arg(args, JSONNode*));
  137. }
  138. va_end(args);
  139. #endif
  140. }
  141. JSONNode JSONNode::duplicate(void) const json_nothrow {
  142. JSON_CHECK_INTERNAL();
  143. JSONNode mycopy(*this);
  144. #ifdef JSON_REF_COUNT
  145. JSON_ASSERT(internal == mycopy.internal, JSON_TEXT("copy ctor failed to ref count correctly"));
  146. mycopy.makeUniqueInternal();
  147. #endif
  148. JSON_ASSERT(internal != mycopy.internal, JSON_TEXT("makeUniqueInternal failed"));
  149. return mycopy;
  150. }
  151. JSONNode & JSONNode::at(json_index_t pos) json_throws(std::out_of_range) {
  152. JSON_CHECK_INTERNAL();
  153. if (json_unlikely(pos >= internal -> size())){
  154. JSON_FAIL(JSON_TEXT("at() out of bounds"));
  155. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  156. }
  157. return (*this)[pos];
  158. }
  159. const JSONNode & JSONNode::at(json_index_t pos) const json_throws(std::out_of_range) {
  160. JSON_CHECK_INTERNAL();
  161. if (json_unlikely(pos >= internal -> size())){
  162. JSON_FAIL(JSON_TEXT("at() const out of bounds"));
  163. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  164. }
  165. return (*this)[pos];
  166. }
  167. JSONNode & JSONNode::operator[](json_index_t pos) json_nothrow {
  168. JSON_CHECK_INTERNAL();
  169. JSON_ASSERT(pos < internal -> size(), JSON_TEXT("[] out of bounds"));
  170. makeUniqueInternal();
  171. return *(internal -> at(pos));
  172. }
  173. const JSONNode & JSONNode::operator[](json_index_t pos) const json_nothrow {
  174. JSON_CHECK_INTERNAL();
  175. JSON_ASSERT(pos < internal -> size(), JSON_TEXT("[] const out of bounds"));
  176. return *(internal -> at(pos));
  177. }
  178. JSONNode & JSONNode::at(const json_string & name_t) json_throws(std::out_of_range) {
  179. JSON_CHECK_INTERNAL();
  180. JSON_ASSERT(type() == JSON_NODE, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("at"));
  181. makeUniqueInternal();
  182. if (JSONNode ** res = internal -> at(name_t)){
  183. return *(*res);
  184. }
  185. JSON_FAIL(json_string(JSON_TEXT("at could not find child by name: ")) + name_t);
  186. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  187. }
  188. const JSONNode & JSONNode::at(const json_string & name_t) const json_throws(std::out_of_range) {
  189. JSON_CHECK_INTERNAL();
  190. JSON_ASSERT(type() == JSON_NODE, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("at"));
  191. if (JSONNode ** res = internal -> at(name_t)){
  192. return *(*res);
  193. }
  194. JSON_FAIL(json_string(JSON_TEXT("at const could not find child by name: ")) + name_t);
  195. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  196. }
  197. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  198. JSONNode & JSONNode::at_nocase(const json_string & name_t) json_throws(std::out_of_range) {
  199. JSON_CHECK_INTERNAL();
  200. JSON_ASSERT(type() == JSON_NODE, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("at_nocase"));
  201. makeUniqueInternal();
  202. if (JSONNode ** res = internal -> at_nocase(name_t)){
  203. return *(*res);
  204. }
  205. JSON_FAIL(json_string(JSON_TEXT("at_nocase could not find child by name: ")) + name_t);
  206. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  207. }
  208. const JSONNode & JSONNode::at_nocase(const json_string & name_t) const json_throws(std::out_of_range) {
  209. JSON_CHECK_INTERNAL();
  210. JSON_ASSERT(type() == JSON_NODE, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("at_nocase"));
  211. if (JSONNode ** res = internal -> at_nocase(name_t)){
  212. return *(*res);
  213. }
  214. JSON_FAIL(json_string(JSON_TEXT("at_nocase const could not find child by name: ")) + name_t);
  215. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  216. }
  217. #endif
  218. #ifndef JSON_LIBRARY
  219. struct auto_delete {
  220. public:
  221. auto_delete(JSONNode * node) json_nothrow : mynode(node){};
  222. ~auto_delete(void) json_nothrow { JSONNode::deleteJSONNode(mynode); };
  223. JSONNode * mynode;
  224. private:
  225. auto_delete(const auto_delete &);
  226. auto_delete & operator = (const auto_delete &);
  227. };
  228. #endif
  229. JSONNode JSON_PTR_LIB JSONNode::pop_back(json_index_t pos) json_throws(std::out_of_range) {
  230. JSON_CHECK_INTERNAL();
  231. if (json_unlikely(pos >= internal -> size())){
  232. JSON_FAIL(JSON_TEXT("pop_back out of bounds"));
  233. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  234. }
  235. makeUniqueInternal();
  236. #ifdef JSON_LIBRARY
  237. return internal -> pop_back(pos);
  238. #else
  239. auto_delete temp(internal -> pop_back(pos));
  240. return *temp.mynode;
  241. #endif
  242. }
  243. JSONNode JSON_PTR_LIB JSONNode::pop_back(const json_string & name_t) json_throws(std::out_of_range) {
  244. JSON_CHECK_INTERNAL();
  245. JSON_ASSERT(type() == JSON_NODE, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("pop_back"));
  246. #ifdef JSON_LIBRARY
  247. return internal -> pop_back(name_t);
  248. #else
  249. if (JSONNode * res = internal -> pop_back(name_t)){
  250. auto_delete temp(res);
  251. return *(temp.mynode);
  252. }
  253. JSON_FAIL(json_string(JSON_TEXT("pop_back const could not find child by name: ")) + name_t);
  254. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  255. #endif
  256. }
  257. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  258. JSONNode JSON_PTR_LIB JSONNode::pop_back_nocase(const json_string & name_t) json_throws(std::out_of_range) {
  259. JSON_CHECK_INTERNAL();
  260. JSON_ASSERT(type() == JSON_NODE, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("pop_back_no_case"));
  261. #ifdef JSON_LIBRARY
  262. return internal -> pop_back_nocase(name_t);
  263. #else
  264. if (JSONNode * res = internal -> pop_back_nocase(name_t)){
  265. auto_delete temp(res);
  266. return *(temp.mynode);
  267. }
  268. JSON_FAIL(json_string(JSON_TEXT("pop_back_nocase could not find child by name: ")) + name_t);
  269. json_throw(std::out_of_range(json_global(EMPTY_STD_STRING)));
  270. #endif
  271. }
  272. #endif
  273. #ifdef JSON_MEMORY_POOL
  274. #include "JSONMemoryPool.h"
  275. memory_pool<NODEPOOL> json_node_mempool;
  276. #endif
  277. void JSONNode::deleteJSONNode(JSONNode * ptr) json_nothrow {
  278. #ifdef JSON_MEMORY_POOL
  279. ptr -> ~JSONNode();
  280. json_node_mempool.deallocate((void*)ptr);
  281. #elif defined(JSON_MEMORY_CALLBACKS)
  282. ptr -> ~JSONNode();
  283. libjson_free<JSONNode>(ptr);
  284. #else
  285. delete ptr;
  286. #endif
  287. }
  288. inline JSONNode * _newJSONNode(const JSONNode & orig) {
  289. #ifdef JSON_MEMORY_POOL
  290. return new((JSONNode*)json_node_mempool.allocate()) JSONNode(orig);
  291. #elif defined(JSON_MEMORY_CALLBACKS)
  292. return new(json_malloc<JSONNode>(1)) JSONNode(orig);
  293. #else
  294. return new JSONNode(orig);
  295. #endif
  296. }
  297. JSONNode * JSONNode::newJSONNode(const JSONNode & orig JSON_MUTEX_COPY_DECL) {
  298. #ifdef JSON_MUTEX_CALLBACKS
  299. if (parentMutex != 0){
  300. JSONNode * temp = _newJSONNode(orig);
  301. temp -> set_mutex(parentMutex);
  302. return temp;
  303. }
  304. #endif
  305. return _newJSONNode(orig);
  306. }
  307. JSONNode * JSONNode::newJSONNode(internalJSONNode * internal_t) {
  308. #ifdef JSON_MEMORY_POOL
  309. return new((JSONNode*)json_node_mempool.allocate()) JSONNode(internal_t);
  310. #elif defined(JSON_MEMORY_CALLBACKS)
  311. return new(json_malloc<JSONNode>(1)) JSONNode(internal_t);
  312. #else
  313. return new JSONNode(internal_t);
  314. #endif
  315. }
  316. JSONNode * JSONNode::newJSONNode_Shallow(const JSONNode & orig) {
  317. #ifdef JSON_MEMORY_POOL
  318. return new((JSONNode*)json_node_mempool.allocate()) JSONNode(true, const_cast<JSONNode &>(orig));
  319. #elif defined(JSON_MEMORY_CALLBACKS)
  320. return new(json_malloc<JSONNode>(1)) JSONNode(true, const_cast<JSONNode &>(orig));
  321. #else
  322. return new JSONNode(true, const_cast<JSONNode &>(orig));
  323. #endif
  324. }