JSONWriter.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "JSONNode.h"
  2. #ifdef JSON_WRITE_PRIORITY
  3. #include "JSONWorker.h"
  4. #include "JSONGlobals.h"
  5. extern bool used_ascii_one;
  6. #ifdef JSON_INDENT
  7. inline json_string makeIndent(unsigned int amount) json_nothrow json_write_priority;
  8. inline json_string makeIndent(unsigned int amount) json_nothrow {
  9. if (amount == 0xFFFFFFFF) return json_global(EMPTY_JSON_STRING);
  10. json_string result;
  11. result.reserve(amount * json_global(INDENT).length());
  12. for(unsigned int i = 0; i < amount; ++i){
  13. result += json_global(INDENT);
  14. }
  15. JSON_ASSERT(result.capacity() == amount * json_global(INDENT).length(), JSON_TEXT("makeIndent made a string too big"));
  16. return result;
  17. }
  18. #else
  19. inline json_string makeIndent(unsigned int amount) json_nothrow {
  20. if (amount == 0xFFFFFFFF) return json_global(EMPTY_JSON_STRING);
  21. if (json_likely(amount < 8)){
  22. static const json_string cache[] = {
  23. json_string(),
  24. json_string(JSON_TEXT("\t")),
  25. json_string(JSON_TEXT("\t\t")),
  26. json_string(JSON_TEXT("\t\t\t")),
  27. json_string(JSON_TEXT("\t\t\t\t")),
  28. json_string(JSON_TEXT("\t\t\t\t\t")),
  29. json_string(JSON_TEXT("\t\t\t\t\t\t")),
  30. json_string(JSON_TEXT("\t\t\t\t\t\t\t"))
  31. };
  32. return cache[amount];
  33. }
  34. #ifndef JSON_LESS_MEMORY
  35. if (json_likely(amount < 16)){
  36. static const json_string cache[] = {
  37. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t")),
  38. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t")),
  39. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t")),
  40. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t")),
  41. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t")),
  42. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  43. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  44. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"))
  45. };
  46. return cache[amount - 8];
  47. }
  48. #if JSON_WRITE_PRIORITY == HIGH
  49. if (json_likely(amount < 24)){
  50. static const json_string cache[] = {
  51. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  52. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  53. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  54. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  55. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  56. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  57. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")),
  58. json_string(JSON_TEXT("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"))
  59. };
  60. return cache[amount - 16];
  61. }
  62. #endif
  63. #endif
  64. return json_string(amount, JSON_TEXT('\t'));
  65. }
  66. #endif
  67. void internalJSONNode::WriteName(bool formatted, bool arrayChild, json_string & output) const json_nothrow {
  68. if (!arrayChild){
  69. output += JSON_TEXT("\"");
  70. JSONWorker::UnfixString(_name, _name_encoded, output);
  71. output += ((formatted) ? JSON_TEXT("\" : ") : JSON_TEXT("\":"));
  72. }
  73. }
  74. void internalJSONNode::WriteChildren(unsigned int indent, json_string & output) const json_nothrow {
  75. //Iterate through the children and write them
  76. if (json_likely(CHILDREN -> empty())) return;
  77. json_string indent_plus_one;
  78. //handle whether or not it's formatted JSON
  79. if (indent != 0xFFFFFFFF){ //it's formatted, make the indentation strings
  80. indent_plus_one = json_global(NEW_LINE) + makeIndent(++indent);
  81. }
  82. //else it's not formatted, leave the indentation strings empty
  83. const size_t size_minus_one = CHILDREN -> size() - 1;
  84. size_t i = 0;
  85. JSONNode ** it = CHILDREN -> begin();
  86. for(JSONNode ** it_end = CHILDREN -> end(); it != it_end; ++it, ++i){
  87. output += indent_plus_one;
  88. (*it) -> internal -> Write(indent, type() == JSON_ARRAY, output);
  89. if (json_likely(i < size_minus_one)) output += JSON_TEXT(','); //the last one does not get a comma, but all of the others do
  90. }
  91. if (indent != 0xFFFFFFFF){
  92. output += json_global(NEW_LINE);
  93. output += makeIndent(indent - 1);
  94. }
  95. }
  96. #ifdef JSON_ARRAY_SIZE_ON_ONE_LINE
  97. void internalJSONNode::WriteChildrenOneLine(unsigned int indent, json_string & output) const json_nothrow {
  98. //Iterate through the children and write them
  99. if (json_likely(CHILDREN -> empty())) return;
  100. if ((*CHILDREN -> begin()) -> internal -> isContainer()) return WriteChildren(indent, output);
  101. json_string comma(JSON_TEXT(","));
  102. if (indent != 0xFFFFFFFF){
  103. comma += JSON_TEXT(' ');
  104. }
  105. //else it's not formatted, leave the indentation strings empty
  106. const size_t size_minus_one = CHILDREN -> size() - 1;
  107. size_t i = 0;
  108. JSONNode ** it = CHILDREN -> begin();
  109. for(JSONNode ** it_end = CHILDREN -> end(); it != it_end; ++it, ++i){
  110. (*it) -> internal -> Write(indent, type() == JSON_ARRAY, output);
  111. if (json_likely(i < size_minus_one)) output += comma; //the last one does not get a comma, but all of the others do
  112. }
  113. }
  114. #endif
  115. #ifdef JSON_COMMENTS
  116. void internalJSONNode::WriteComment(unsigned int indent, json_string & output) const json_nothrow {
  117. if (indent == 0xFFFFFFFF) return;
  118. if (json_likely(_comment.empty())) return;
  119. size_t pos = _comment.find(JSON_TEXT('\n'));
  120. const json_string current_indent(json_global(NEW_LINE) + makeIndent(indent));
  121. if (json_likely(pos == json_string::npos)){ //Single line comment
  122. output += current_indent;
  123. output += json_global(SINGLELINE_COMMENT);
  124. output.append(_comment.begin(), _comment.end());
  125. output += current_indent;
  126. return;
  127. }
  128. /*
  129. Multiline comments
  130. */
  131. output += current_indent;
  132. #if !(defined(JSON_WRITE_BASH_COMMENTS) || defined(JSON_WRITE_SINGLE_LINE_COMMENTS))
  133. const json_string current_indent_plus_one(json_global(NEW_LINE) + makeIndent(indent + 1));
  134. output += JSON_TEXT("/*");
  135. output += current_indent_plus_one;
  136. #endif
  137. size_t old = 0;
  138. while(pos != json_string::npos){
  139. if (json_unlikely(pos && _comment[pos - 1] == JSON_TEXT('\r'))) --pos;
  140. #if defined(JSON_WRITE_BASH_COMMENTS) || defined(JSON_WRITE_SINGLE_LINE_COMMENTS)
  141. output += json_global(SINGLELINE_COMMENT);
  142. #endif
  143. output.append(_comment.begin() + old, _comment.begin() + pos);
  144. #if defined(JSON_WRITE_BASH_COMMENTS) || defined(JSON_WRITE_SINGLE_LINE_COMMENTS)
  145. output += current_indent;
  146. #else
  147. output += current_indent_plus_one;
  148. #endif
  149. old = (_comment[pos] == JSON_TEXT('\r')) ? pos + 2 : pos + 1;
  150. pos = _comment.find(JSON_TEXT('\n'), old);
  151. }
  152. #if defined(JSON_WRITE_BASH_COMMENTS) || defined(JSON_WRITE_SINGLE_LINE_COMMENTS)
  153. output += json_global(SINGLELINE_COMMENT);
  154. #endif
  155. output.append(_comment.begin() + old, _comment.end());
  156. output += current_indent;
  157. #if !(defined(JSON_WRITE_BASH_COMMENTS) || defined(JSON_WRITE_SINGLE_LINE_COMMENTS))
  158. output += JSON_TEXT("*/");
  159. output += current_indent;
  160. #endif
  161. }
  162. #else
  163. inline void internalJSONNode::WriteComment(unsigned int, json_string &) const json_nothrow {}
  164. #endif
  165. void internalJSONNode::DumpRawString(json_string & output) const json_nothrow {
  166. //first remove the \1 characters
  167. if (used_ascii_one){ //if it hasn't been used yet, don't bother checking
  168. json_string result(_string.begin(), _string.end());
  169. for(json_string::iterator beg = result.begin(), en = result.end(); beg != en; ++beg){
  170. if (*beg == JSON_TEXT('\1')) *beg = JSON_TEXT('\"');
  171. }
  172. output += result;
  173. return;
  174. } else {
  175. output.append(_string.begin(), _string.end());
  176. }
  177. }
  178. void internalJSONNode::Write(unsigned int indent, bool arrayChild, json_string & output) const json_nothrow {
  179. const bool formatted = indent != 0xFFFFFFFF;
  180. WriteComment(indent, output);
  181. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  182. if (!(formatted || fetched)){ //It's not formatted or fetched, just do a raw dump
  183. WriteName(false, arrayChild, output);
  184. //first remove the \1 characters
  185. DumpRawString(output);
  186. return;
  187. }
  188. #endif
  189. WriteName(formatted, arrayChild, output);
  190. //It's either formatted or fetched
  191. switch (_type){
  192. case JSON_NODE: //got members, write the members
  193. Fetch();
  194. output += JSON_TEXT("{");
  195. WriteChildren(indent, output);
  196. output += JSON_TEXT("}");
  197. return;
  198. case JSON_ARRAY: //write out the child nodes int he array
  199. Fetch();
  200. output += JSON_TEXT("[");
  201. #ifdef JSON_ARRAY_SIZE_ON_ONE_LINE
  202. if (size() <= JSON_ARRAY_SIZE_ON_ONE_LINE){
  203. WriteChildrenOneLine(indent, output);
  204. } else {
  205. #endif
  206. WriteChildren(indent, output);
  207. #ifdef JSON_ARRAY_SIZE_ON_ONE_LINE
  208. }
  209. #endif
  210. output += JSON_TEXT("]");
  211. return;
  212. case JSON_NUMBER: //write out a literal, without quotes
  213. case JSON_NULL:
  214. case JSON_BOOL:
  215. output.append(_string.begin(), _string.end());
  216. return;
  217. }
  218. JSON_ASSERT(_type == JSON_STRING, JSON_TEXT("Unknown json node type"));
  219. //If it go here, then it's a json_string
  220. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  221. if (json_likely(fetched)){
  222. #endif
  223. output += JSON_TEXT("\"");
  224. JSONWorker::UnfixString(_string, _string_encoded, output); //It's already been fetched, meaning that it's unescaped
  225. output += JSON_TEXT("\"");
  226. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  227. } else {
  228. DumpRawString(output); //it hasn't yet been fetched, so it's already unescaped, just do a dump
  229. }
  230. #endif
  231. }
  232. #endif