summaryrefslogtreecommitdiffabout
path: root/src/jsonfmt.c
blob: 9d900ca16fcba570f5f8f109bd6dbaae23580ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2016 Sergey Poznyakoff

   Grecs is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Grecs is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Grecs. If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include "grecs.h"
#include "grecs/json.h"

static void
json_writez(struct json_format *fmt, char const *str)
{
	size_t len = strlen(str);
	fmt->write(fmt->data, str, len);
}

static void
json_writec(struct json_format *fmt, char c)
{
	fmt->write(fmt->data, &c, 1);
}

static void
json_indent(struct json_format *fmt, size_t level)
{
	level *= fmt->indent;
	while (level--)
		json_writec(fmt, ' ');
}

static void
json_format_delim(struct json_format *fmt, size_t level)
{
	json_writec(fmt, ',');
	if (fmt->indent) {
		json_writec(fmt, '\n');
		json_indent(fmt, level);
	} else
		json_writec(fmt, ' ');
}

static int
escape(char c, char *o)
{
	static char transtab[] = "\\\\\"\"b\bf\fn\nr\rt\t";
	char *p;

	for (p = transtab; *p; p += 2) {
		if (p[1] == c) {
			*o = p[0];
			return 0;
		}
	}
	return -1;
}

static void
json_format_string(struct json_format *fmt, const char *s)
{
	json_writec(fmt, '"');
	for (; *s; s++) {
		char c;
		if (!escape(*s, &c)) {
			json_writec(fmt, '\\');
			json_writec(fmt, c);
		} else
			json_writec(fmt, *s);
	}
	json_writec(fmt, '"');
}

static void
json_format_number(struct json_format *fmt, double n)
{
	char *buffer = NULL;
	size_t size = 0;
	if (fmt->precision == -1) 
		grecs_asprintf(&buffer, &size, "%e", n);
	else
		grecs_asprintf(&buffer, &size, "%.*f", fmt->precision, n);
	json_writez(fmt, buffer);
	free(buffer);
}

static int
collect_keypairs(void *sym, void *data)
{
	struct json_pair *p = sym;
	struct json_pair ***kp = data;
	**kp = p;
	++*kp;
	return 0;
}

static void json_formatter(struct json_format *fmt, struct json_value *obj,
			   size_t level);

static int
keypair_cmp_name(const void *a, const void *b)
{
	struct json_pair const * const *kpa = a;
	struct json_pair const * const *kpb = b;
	return strcmp((*kpa)->k, (*kpb)->k);
}

static void
json_format_obj(struct json_format *fmt, struct json_value *obj, size_t level)
{
	size_t count, i;
	struct json_pair **keypairs, **kp;
	
	count = grecs_symtab_count(obj->v.o);
	keypairs = grecs_calloc(count, sizeof(*keypairs));
	kp = keypairs;
	grecs_symtab_foreach(obj->v.o, collect_keypairs, &kp);
	qsort(keypairs, count, sizeof(*keypairs), keypair_cmp_name);

	json_writec(fmt, '{');
	if (count) {
		if (fmt->indent)
			json_writec(fmt, '\n');
		for (i = 0; i < count; i++) {
			(i ? json_format_delim : json_indent)(fmt, level);
			json_format_string(fmt, keypairs[i]->k);
			json_writec(fmt, ':');
			if (fmt->indent)
				json_writec(fmt, ' ');
			json_formatter(fmt, keypairs[i]->v, level);
		}
		if (fmt->indent) {
			json_writec(fmt, '\n');
			json_indent(fmt, level-1);
		}
	}
	json_writec(fmt, '}');
	grecs_free(keypairs);
}

static void
json_format_array(struct json_format *fmt, struct json_value *obj,
		  size_t level)
{
	size_t i;

	json_array_flatten(obj);
	
	json_writec(fmt, '[');
	if (obj->v.a->oc) {
		if (fmt->indent)
			json_writec(fmt, '\n');
		for (i = 0; i < obj->v.a->oc; i++) {
			(i ? json_format_delim : json_indent)(fmt, level);
			json_formatter(fmt, obj->v.a->ov[i], level);
		}
		if (fmt->indent) {
			json_writec(fmt, '\n');
			json_indent(fmt, level-1);
		}
	}
	json_writec(fmt, ']');
}

static void
json_formatter(struct json_format *fmt, struct json_value *obj, size_t level)
{
	if (!obj) {
		json_writez(fmt, "null");
		return;
	}

	++level;
	switch (obj->type) {
	case json_null:
		json_writez(fmt, "null");
		break;
		
	case json_bool:
		json_writez(fmt, obj->v.b ? "true" : "false");
		break;
		
	case json_number:
		json_format_number(fmt, obj->v.n);
		break;

	case json_string:
		json_format_string(fmt, obj->v.s);
		break;
		
	case json_arr:
		json_format_array(fmt, obj, level);
		break;

	case json_object: 
		json_format_obj(fmt, obj, level);
		break;
	}
}		

void
json_format_value(struct json_value *obj, struct json_format *fmt)
{
	json_formatter(fmt, obj, 0);
}


Return to:

Send suggestions and report system problems to the System administrator.