JsonNode.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_LONG:
  19. writer.writeInteger(content.asInteger);
  20. break;
  21. case JSON_BOOLEAN:
  22. writer.writeBoolean(content.asBoolean);
  23. break;
  24. default: // >= JSON_DOUBLE_0_DECIMALS
  25. writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
  26. break;
  27. }
  28. }
  29. void JsonNode::writeArrayTo(JsonWriter& writer)
  30. {
  31. JsonNode* child = content.asContainer.child;
  32. if (child)
  33. {
  34. writer.beginArray();
  35. while (true)
  36. {
  37. child->writeTo(writer);
  38. child = child->next;
  39. if (!child) break;
  40. writer.writeComma();
  41. }
  42. writer.endArray();
  43. }
  44. else
  45. {
  46. writer.writeEmptyArray();
  47. }
  48. }
  49. void JsonNode::writeObjectTo(JsonWriter& writer)
  50. {
  51. JsonNode* child = content.asContainer.child;
  52. if (child)
  53. {
  54. writer.beginObject();
  55. while (true)
  56. {
  57. writer.writeString(child->content.asKey.key);
  58. writer.writeColon();
  59. child->content.asKey.value->writeTo(writer);
  60. child = child->next;
  61. if (!child) break;
  62. writer.writeComma();
  63. }
  64. writer.endObject();
  65. }
  66. else
  67. {
  68. writer.writeEmptyObject();
  69. }
  70. }