json-lib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Simple library for printing JSON data.
  2. Copyright (C) 2014-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <string.h>
  16. #include "json-lib.h"
  17. void
  18. json_init (json_ctx_t *ctx, unsigned int indent_level, FILE *fp)
  19. {
  20. ctx->indent_level = indent_level;
  21. ctx->fp = fp;
  22. ctx->first_element = true;
  23. }
  24. static void
  25. do_indent (json_ctx_t *ctx)
  26. {
  27. char indent_buf[ctx->indent_level + 1];
  28. memset (indent_buf, ' ', ctx->indent_level + 1);
  29. indent_buf[ctx->indent_level] = '\0';
  30. fputs (indent_buf, ctx->fp);
  31. }
  32. void
  33. json_document_begin (json_ctx_t *ctx)
  34. {
  35. do_indent (ctx);
  36. fputs ("{\n", ctx->fp);
  37. ctx->indent_level++;
  38. ctx->first_element = true;
  39. }
  40. void
  41. json_document_end (json_ctx_t *ctx)
  42. {
  43. ctx->indent_level--;
  44. do_indent (ctx);
  45. fputs ("\n}", ctx->fp);
  46. }
  47. void
  48. json_attr_object_begin (json_ctx_t *ctx, const char *name)
  49. {
  50. if (!ctx->first_element)
  51. fprintf (ctx->fp, ",\n");
  52. do_indent (ctx);
  53. fprintf (ctx->fp, "\"%s\": {\n", name);
  54. ctx->indent_level++;
  55. ctx->first_element = true;
  56. }
  57. void
  58. json_attr_object_end (json_ctx_t *ctx)
  59. {
  60. ctx->indent_level--;
  61. ctx->first_element = false;
  62. fputs ("\n", ctx->fp);
  63. do_indent (ctx);
  64. fputs ("}", ctx->fp);
  65. }
  66. void
  67. json_attr_string (json_ctx_t *ctx, const char *name, const char *s)
  68. {
  69. if (!ctx->first_element)
  70. fprintf (ctx->fp, ",\n");
  71. else
  72. ctx->first_element = false;
  73. do_indent (ctx);
  74. fprintf (ctx->fp, "\"%s\": \"%s\"", name, s);
  75. }
  76. void
  77. json_attr_uint (json_ctx_t *ctx, const char *name, uint64_t d)
  78. {
  79. if (!ctx->first_element)
  80. fprintf (ctx->fp, ",\n");
  81. else
  82. ctx->first_element = false;
  83. do_indent (ctx);
  84. fprintf (ctx->fp, "\"%s\": %" PRIu64 , name, d);
  85. }
  86. void
  87. json_attr_int (json_ctx_t *ctx, const char *name, int64_t d)
  88. {
  89. if (!ctx->first_element)
  90. fprintf (ctx->fp, ",\n");
  91. else
  92. ctx->first_element = false;
  93. do_indent (ctx);
  94. fprintf (ctx->fp, "\"%s\": %" PRId64 , name, d);
  95. }
  96. void
  97. json_attr_double (json_ctx_t *ctx, const char *name, double d)
  98. {
  99. if (!ctx->first_element)
  100. fprintf (ctx->fp, ",\n");
  101. else
  102. ctx->first_element = false;
  103. do_indent (ctx);
  104. fprintf (ctx->fp, "\"%s\": %g", name, d);
  105. }
  106. void
  107. json_array_begin (json_ctx_t *ctx, const char *name)
  108. {
  109. if (!ctx->first_element)
  110. fprintf (ctx->fp, ",\n");
  111. do_indent (ctx);
  112. fprintf (ctx->fp, "\"%s\": [", name);
  113. ctx->indent_level++;
  114. ctx->first_element = true;
  115. }
  116. void
  117. json_array_end (json_ctx_t *ctx)
  118. {
  119. ctx->indent_level--;
  120. ctx->first_element = false;
  121. fputs ("]", ctx->fp);
  122. }
  123. void
  124. json_element_string (json_ctx_t *ctx, const char *s)
  125. {
  126. if (!ctx->first_element)
  127. fprintf (ctx->fp, ", \"%s\"", s);
  128. else
  129. {
  130. fprintf (ctx->fp, "\"%s\"", s);
  131. ctx->first_element = false;
  132. }
  133. }
  134. void
  135. json_element_uint (json_ctx_t *ctx, uint64_t d)
  136. {
  137. if (!ctx->first_element)
  138. fprintf (ctx->fp, ", %" PRIu64, d);
  139. else
  140. {
  141. fprintf (ctx->fp, "%" PRIu64, d);
  142. ctx->first_element = false;
  143. }
  144. }
  145. void
  146. json_element_int (json_ctx_t *ctx, int64_t d)
  147. {
  148. if (!ctx->first_element)
  149. fprintf (ctx->fp, ", %" PRId64, d);
  150. else
  151. {
  152. fprintf (ctx->fp, "%" PRId64, d);
  153. ctx->first_element = false;
  154. }
  155. }
  156. void
  157. json_element_double (json_ctx_t *ctx, double d)
  158. {
  159. if (!ctx->first_element)
  160. fprintf (ctx->fp, ", %g", d);
  161. else
  162. {
  163. fprintf (ctx->fp, "%g", d);
  164. ctx->first_element = false;
  165. }
  166. }
  167. void
  168. json_element_object_begin (json_ctx_t *ctx)
  169. {
  170. if (!ctx->first_element)
  171. fprintf (ctx->fp, ",");
  172. fputs ("\n", ctx->fp);
  173. do_indent (ctx);
  174. fputs ("{\n", ctx->fp);
  175. ctx->indent_level++;
  176. ctx->first_element = true;
  177. }
  178. void
  179. json_element_object_end (json_ctx_t *ctx)
  180. {
  181. ctx->indent_level--;
  182. ctx->first_element = false;
  183. fputs ("\n", ctx->fp);
  184. do_indent (ctx);
  185. fputs ("}", ctx->fp);
  186. }