summaryrefslogtreecommitdiff
path: root/keama/json.c
blob: ff596e8023c1bbffe0c1978c27f514401636c455 (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
/*
 * Copyright (c) 2017 by Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *   Internet Systems Consortium, Inc.
 *   PO Box 360
 *   Newmarket, NH 03857 USA
 *   <info@isc.org>
 *   https://www.isc.org/
 *
 */

/* From Kea src/lib/cc/data.cc fromJSON() */

#include "keama.h"

#include <stdlib.h>
#include <string.h>

struct element *
json_parse(struct parse *cfile)
{
	struct element *elem;
	const char *val;
	enum dhcp_token token;

	elem = create();
	stackPush(cfile, elem);
	cfile->stack[0] = elem;
	cfile->stack_top = 0;

	token = next_token(&val, NULL, cfile);
	switch (token) {
	case NUMBER:
		elem = createInt(atoll(val));
		TAILQ_CONCAT(&elem->comments, &cfile->comments);
		break;
	case STRING:
		elem = createString(makeString(-1, val));
		TAILQ_CONCAT(&elem->comments, &cfile->comments);
		break;
	case NAME:
		if (strcmp(val, "null") == 0)
			elem = createNull();
		else if (strcmp(val, "true") == 0)
			elem = createBool(ISC_TRUE);
		else if (strcmp(val, "false") == 0) {
			elem = createBool(ISC_FALSE);
			elem->skip = ISC_TRUE;
		} else
			parse_error(cfile, "unknown name %s", val);
		TAILQ_CONCAT(&elem->comments, &cfile->comments);
		break;
	case LBRACKET:
		elem = json_list_parse(cfile);
		break;
	case LBRACE:
		elem = json_map_parse(cfile);
		break;
	case END_OF_FILE:
		parse_error(cfile, "unexpected end of file");
	default:
		parse_error(cfile, "unexpected %s", val);
	}
	return elem;
}

struct element *
json_list_parse(struct parse *cfile)
{
	struct element *list;
	struct element *item;
	const char *val;
	enum dhcp_token token;
	isc_boolean_t done = ISC_FALSE;

	list = createList();
	TAILQ_CONCAT(&list->comments, &cfile->comments);
	stackPush(cfile, list);
	do {
		token = peek_token(&val, NULL, cfile);
		switch (token) {
		case RBRACKET:
			done = ISC_TRUE;
			break;
		case END_OF_FILE:
			parse_error(cfile, "unexpected end of file");
		case COMMA:
			skip_token(&val, NULL, cfile);
			if (listSize(list) == 0)
				parse_error(cfile, "unexpected ','");
			item = json_parse(cfile);
			listPush(list, item);
			break;
		default:
			if (listSize(list) > 0)
				parse_error(cfile, "expected ','");
			item = json_parse(cfile);
			listPush(list, item);
			break;
		}
	} while (!done);
	skip_token(&val, NULL, cfile);
	cfile->stack_top--;
	return list;
}

struct element *
json_map_parse(struct parse *cfile)
{
	struct element *map;
	struct element *item;
	const char *val;
	const char *key;
	enum dhcp_token token;
	isc_boolean_t done = ISC_FALSE;

	map = createMap();
	TAILQ_CONCAT(&map->comments, &cfile->comments);
	stackPush(cfile, map);
	do {
		token = peek_token(&val, NULL, cfile);
		switch (token) {
		case RBRACE:
			done = ISC_TRUE;
			break;
		case END_OF_FILE:
			parse_error(cfile, "unexpected end of file");
		case COMMA:
			skip_token(&val, NULL, cfile);
			if (mapSize(map) == 0)
				parse_error(cfile, "unexpected ','");
			token = next_token(&val, NULL, cfile);
			if (token != STRING)
				parse_error(cfile, "unexpected %s, "
					    "expected \"key\":value", val);
			key = strdup(val);
			token = next_token(&val, NULL, cfile);
			if (token != COLON)
				parse_error(cfile, "unexpected %s, "
					    "expected ':'", val);
			item = json_parse(cfile);
			mapSet(map, item, key);
			break;
		case STRING:
			skip_token(&val, NULL, cfile);
			if (mapSize(map) > 0)
				parse_error(cfile, "unexpected \"%s\", "
					    "expected ','", val);
			key = strdup(val);
			token = next_token(&val, NULL, cfile);
			if (token != COLON)
				parse_error(cfile, "unexpected %s, "
					    "expected ':'", val);
			item = json_parse(cfile);
			mapSet(map, item, key);
			break;
		default:
			if (mapSize(map) == 0)
				parse_error(cfile, "unexpected %s, "
					    "expected \"key\":value or '}'",
					    val);
			else
				parse_error(cfile, "unexpected %s, "
					    "expected ',' or '}'", val);
		}
	} while (!done);
	skip_token(&val, NULL, cfile);
	cfile->stack_top--;
	return map;
}