JsonArray.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "JsonArray.h"
  2. #include "JsonObject.h"
  3. #include "JsonValue.h"
  4. JsonValue JsonArray::operator[](int index) const
  5. {
  6. for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
  7. {
  8. if (!index) return JsonValue(*it);
  9. index--;
  10. }
  11. return JsonValue();
  12. }
  13. void JsonArray::add(bool value)
  14. {
  15. JsonNode* node = createNode(JSON_BOOLEAN);
  16. if (!node) return;
  17. node->content.asBoolean = value;
  18. addChild(node);
  19. }
  20. void JsonArray::add(char const* value)
  21. {
  22. JsonNode* node = createNode(JSON_STRING);
  23. if (!node) return;
  24. node->content.asString = value;
  25. addChild(node);
  26. }
  27. void JsonArray::add(double value, int decimals)
  28. {
  29. JsonNode* node = createNode((JsonNodeType)(JSON_DOUBLE_0_DECIMALS + decimals));
  30. if (!node) return;
  31. node->content.asDouble = value;
  32. addChild(node);
  33. }
  34. void JsonArray::add(long value)
  35. {
  36. JsonNode* node = createNode(JSON_INTEGER);
  37. if (!node) return;
  38. node->content.asInteger = value;
  39. addChild(node);
  40. }
  41. void JsonArray::add(JsonContainer innerContainer)
  42. {
  43. JsonNode* node = createNode(JSON_PROXY);
  44. if (!node) return;
  45. node->content.asProxy.target = innerContainer._node;
  46. addChild(node);
  47. }
  48. JsonArray JsonArray::createNestedArray()
  49. {
  50. JsonNode* node = createNestedContainer(JSON_ARRAY);
  51. return JsonArray(node);
  52. }
  53. JsonObject JsonArray::createNestedObject()
  54. {
  55. JsonNode* node = createNestedContainer(JSON_OBJECT);
  56. return JsonObject(node);
  57. }
  58. JsonNode* JsonArray::createNestedContainer(JsonNodeType type)
  59. {
  60. JsonNode* node = createNode(type);
  61. if (!node) return 0;
  62. node->content.asContainer.buffer = _node->content.asContainer.buffer;
  63. addChild(node);
  64. return node;
  65. }