Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

univalue_write.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014 BitPay Inc.
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include "univalue.h"
  7. #include "univalue_escapes.h"
  8. // TODO: Using UTF8
  9. using namespace std;
  10. static string json_escape(const string& inS)
  11. {
  12. string outS;
  13. outS.reserve(inS.size() * 2);
  14. for (unsigned int i = 0; i < inS.size(); i++) {
  15. unsigned char ch = inS[i];
  16. const char *escStr = escapes[ch];
  17. if (escStr)
  18. outS += escStr;
  19. else if (isprint(ch))
  20. outS += ch;
  21. else {
  22. char tmpesc[16];
  23. sprintf(tmpesc, "\\u%04x", ch);
  24. outS += tmpesc;
  25. }
  26. }
  27. return outS;
  28. }
  29. string UniValue::write(unsigned int prettyIndent,
  30. unsigned int indentLevel) const
  31. {
  32. string s;
  33. s.reserve(1024);
  34. unsigned int modIndent = indentLevel;
  35. if (modIndent == 0)
  36. modIndent = 1;
  37. switch (typ) {
  38. case VNULL:
  39. s += "null";
  40. break;
  41. case VOBJ:
  42. writeObject(prettyIndent, modIndent, s);
  43. break;
  44. case VARR:
  45. writeArray(prettyIndent, modIndent, s);
  46. break;
  47. case VSTR:
  48. s += "\"" + json_escape(val) + "\"";
  49. break;
  50. case VNUM:
  51. s += val;
  52. break;
  53. case VBOOL:
  54. s += (val == "1" ? "true" : "false");
  55. break;
  56. }
  57. return s;
  58. }
  59. static void indentStr(unsigned int prettyIndent, unsigned int indentLevel, string& s)
  60. {
  61. s.append(prettyIndent * indentLevel, ' ');
  62. }
  63. void UniValue::writeArray(unsigned int prettyIndent, unsigned int indentLevel, string& s) const
  64. {
  65. s += "[";
  66. if (prettyIndent)
  67. s += "\n";
  68. for (unsigned int i = 0; i < values.size(); i++) {
  69. if (prettyIndent)
  70. indentStr(prettyIndent, indentLevel, s);
  71. s += values[i].write(prettyIndent, indentLevel + 1);
  72. if (i != (values.size() - 1)) {
  73. s += ",";
  74. if (prettyIndent)
  75. s += " ";
  76. }
  77. if (prettyIndent)
  78. s += "\n";
  79. }
  80. if (prettyIndent)
  81. indentStr(prettyIndent, indentLevel - 1, s);
  82. s += "]";
  83. }
  84. void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, string& s) const
  85. {
  86. s += "{";
  87. if (prettyIndent)
  88. s += "\n";
  89. for (unsigned int i = 0; i < keys.size(); i++) {
  90. if (prettyIndent)
  91. indentStr(prettyIndent, indentLevel, s);
  92. s += "\"" + json_escape(keys[i]) + "\":";
  93. if (prettyIndent)
  94. s += " ";
  95. s += values[i].write(prettyIndent, indentLevel + 1);
  96. if (i != (values.size() - 1))
  97. s += ",";
  98. if (prettyIndent)
  99. s += "\n";
  100. }
  101. if (prettyIndent)
  102. indentStr(prettyIndent, indentLevel - 1, s);
  103. s += "}";
  104. }