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