2
0

JSONParser.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "JSONParser.h"
  2. #include "DiskFile.h"
  3. #include "OS.h"
  4. #include "StringUtils.h"
  5. #include "Assert.h"
  6. #include "Log.h"
  7. namespace crown
  8. {
  9. //--------------------------------------------------------------------------
  10. JSONParser::JSONParser(Allocator& allocator, const char* s) :
  11. m_buffer(s),
  12. m_nodes(allocator)
  13. {
  14. }
  15. //--------------------------------------------------------------------------
  16. JSONParser& JSONParser::root()
  17. {
  18. m_nodes.clear();
  19. List<JSONPair> tmp(default_allocator());
  20. JSON::parse_object(m_buffer, tmp);
  21. JSONNode node;
  22. for (int i = 0; i < tmp.size(); i++)
  23. {
  24. node.type = JSON::type(tmp[i].val);
  25. node.key = tmp[i].key;
  26. node.val = tmp[i].val;
  27. m_nodes.push_back(node);
  28. }
  29. return *this;
  30. }
  31. //--------------------------------------------------------------------------
  32. JSONParser& JSONParser::object(const char* key)
  33. {
  34. bool found = false;
  35. for (int i = 0; i < m_nodes.size(); i++)
  36. {
  37. if (m_nodes[i].type == JT_OBJECT)
  38. {
  39. List<char> str(default_allocator());
  40. JSON::parse_string(m_nodes[i].key, str);
  41. if (string::strcmp(key, str.begin()) == 0)
  42. {
  43. List<JSONPair> obj(default_allocator());
  44. JSON::parse_object(m_nodes[i].val, obj);
  45. m_nodes.clear();
  46. JSONNode node;
  47. for (int j = 0; j < obj.size(); j++)
  48. {
  49. node.type = JSON::type(obj[j].val);
  50. node.key = obj[j].key;
  51. node.val = obj[j].val;
  52. m_nodes.push_back(node);
  53. }
  54. found = true;
  55. break;
  56. }
  57. }
  58. }
  59. CE_ASSERT(found, "Object called %s not found", key);
  60. return *this;
  61. }
  62. //--------------------------------------------------------------------------
  63. JSONParser& JSONParser::array(const char* key, uint32_t index)
  64. {
  65. bool found = false;
  66. for (int i = 0; i < m_nodes.size(); i++)
  67. {
  68. if (m_nodes[i].type == JT_ARRAY)
  69. {
  70. List<char> str(default_allocator());
  71. JSON::parse_string(m_nodes[i].key, str);
  72. if (string::strcmp(key, str.begin()) == 0)
  73. {
  74. List<const char*> arr(default_allocator());
  75. JSON::parse_array(m_nodes[i].val, arr);
  76. m_nodes.clear();
  77. JSONNode node;
  78. node.type = JSON::type(arr[index]);
  79. node.key = NULL;
  80. node.val = arr[index];
  81. m_nodes.push_back(node);
  82. found = true;
  83. break;
  84. }
  85. }
  86. }
  87. CE_ASSERT(found, "Array called %s not found", key);
  88. return *this;
  89. }
  90. //--------------------------------------------------------------------------
  91. const char* JSONParser::string(const char* key, List<char>& str)
  92. {
  93. bool found = false;
  94. for (int i = 0; i < m_nodes.size(); i++)
  95. {
  96. if (JSON::type(m_nodes[i].val) == JT_STRING)
  97. {
  98. List<char> tmp(default_allocator());
  99. JSON::parse_string(m_nodes[i].key, tmp);
  100. if (string::strcmp(key, tmp.begin()) == 0)
  101. {
  102. JSON::parse_string(m_nodes[i].val, str);
  103. found = true;
  104. break;
  105. }
  106. }
  107. }
  108. CE_ASSERT(found, "String not found");
  109. }
  110. //--------------------------------------------------------------------------
  111. double JSONParser::number(const char* key)
  112. {
  113. for (int i = 0; i < m_nodes.size(); i++)
  114. {
  115. if (JSON::type(m_nodes[i].val) == JT_NUMBER)
  116. {
  117. if (m_nodes[i].key == NULL)
  118. {
  119. return JSON::parse_number(m_nodes[i].val);
  120. }
  121. List<char> tmp(default_allocator());
  122. JSON::parse_string(m_nodes[i].key, tmp);
  123. if (string::strcmp(key, tmp.begin()) == 0)
  124. {
  125. return JSON::parse_number(m_nodes[i].val);
  126. }
  127. }
  128. }
  129. CE_ASSERT(found, "Number not found");
  130. }
  131. //--------------------------------------------------------------------------
  132. bool JSONParser::boolean(const char* key)
  133. {
  134. for (int i = 0; i < m_nodes.size(); i++)
  135. {
  136. if (JSON::type(m_nodes[i].val) == JT_BOOL)
  137. {
  138. if (m_nodes[i].key == NULL)
  139. {
  140. return JSON::parse_bool(m_nodes[i].val);
  141. }
  142. List<char> tmp(default_allocator());
  143. JSON::parse_string(m_nodes[i].key, tmp);
  144. if (string::strcmp(key, tmp.begin()) == 0)
  145. {
  146. return JSON::parse_bool(m_nodes[i].val);
  147. }
  148. }
  149. }
  150. CE_ASSERT(found, "Boolean not found");
  151. }
  152. //--------------------------------------------------------------------------
  153. void JSONParser::print_nodes()
  154. {
  155. for (int i = 0; i < m_nodes.size(); i++)
  156. {
  157. Log::i("Index: %d", i);
  158. Log::i("Type : %d", m_nodes[i].type);
  159. Log::i("Key : %s", m_nodes[i].key);
  160. Log::i("Val : %s", m_nodes[i].val);
  161. }
  162. }
  163. } //namespace crown