JsonValue.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "JsonValue.h"
  6. #include <cstdio>
  7. #include <cstring>
  8. size_t JsonValue::printBoolTo(Print& p) const
  9. {
  10. return p.write(content.asBool ? "true" : "false");
  11. }
  12. size_t JsonValue::printDoubleTo(Print& p) const
  13. {
  14. char tmp[32];
  15. sprintf(tmp, "%.17lg", content.asDouble);
  16. return p.write(tmp);
  17. }
  18. size_t JsonValue::printFloatTo(Print& p) const
  19. {
  20. char tmp[16];
  21. sprintf(tmp, "%.9g", content.asFloat);
  22. return p.write(tmp);
  23. }
  24. size_t JsonValue::printLongTo(Print& p) const
  25. {
  26. char tmp[32];
  27. sprintf(tmp, "%ld", content.asLong);
  28. return p.write(tmp);
  29. }
  30. size_t JsonValue::printPrintableTo(Print& p) const
  31. {
  32. if (content.asPrintable)
  33. return ((Printable*) content.asPrintable)->printTo(p);
  34. else
  35. return p.write("null");
  36. }
  37. size_t JsonValue::printStringTo(Print& p) const
  38. {
  39. const char* s = content.asString;
  40. if (!s)
  41. {
  42. return p.write("null");
  43. }
  44. size_t n = 0;
  45. n += p.write('\"');
  46. while (*s)
  47. {
  48. switch (*s)
  49. {
  50. case '"':
  51. n += p.write("\\\"");
  52. break;
  53. case '\\':
  54. n += p.write("\\\\");
  55. break;
  56. case '\b':
  57. n += p.write("\\b");
  58. break;
  59. case '\f':
  60. n += p.write("\\f");
  61. break;
  62. case '\n':
  63. n += p.write("\\n");
  64. break;
  65. case '\r':
  66. n += p.write("\\r");
  67. break;
  68. case '\t':
  69. n += p.write("\\t");
  70. break;
  71. default:
  72. n += p.write(*s);
  73. break;
  74. }
  75. s++;
  76. }
  77. n += p.write('\"');
  78. return n;
  79. }