JsonNode.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "JsonNode.h"
  2. #include "JsonWriter.h"
  3. #include "../JsonArray.h"
  4. #include "../JsonObject.h"
  5. void JsonNode::writeTo(JsonWriter& writer)
  6. {
  7. switch (type)
  8. {
  9. case JSON_ARRAY:
  10. writeArrayTo(writer);
  11. break;
  12. case JSON_OBJECT:
  13. writeObjectTo(writer);
  14. break;
  15. case JSON_STRING:
  16. writer.writeString(content.asString);
  17. break;
  18. case JSON_INTEGER:
  19. writer.writeInteger(content.asInteger);
  20. break;
  21. case JSON_BOOLEAN:
  22. writer.writeBoolean(content.asBoolean);
  23. break;
  24. case JSON_PROXY:
  25. content.asProxy.target->writeTo(writer);
  26. break;
  27. default: // >= JSON_DOUBLE_0_DECIMALS
  28. writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
  29. break;
  30. }
  31. }
  32. void JsonNode::writeArrayTo(JsonWriter& writer)
  33. {
  34. JsonNode* child = content.asContainer.child;
  35. if (child)
  36. {
  37. writer.beginArray();
  38. while (true)
  39. {
  40. child->writeTo(writer);
  41. child = child->next;
  42. if (!child) break;
  43. writer.writeComma();
  44. }
  45. writer.endArray();
  46. }
  47. else
  48. {
  49. writer.writeEmptyArray();
  50. }
  51. }
  52. void JsonNode::writeObjectTo(JsonWriter& writer)
  53. {
  54. JsonNode* child = content.asContainer.child;
  55. if (child)
  56. {
  57. writer.beginObject();
  58. while (true)
  59. {
  60. writer.writeString(child->content.asKey.key);
  61. writer.writeColon();
  62. child->content.asKey.value->writeTo(writer);
  63. child = child->next;
  64. if (!child) break;
  65. writer.writeComma();
  66. }
  67. writer.endObject();
  68. }
  69. else
  70. {
  71. writer.writeEmptyObject();
  72. }
  73. }