aboutsummaryrefslogtreecommitdiff
path: root/core/encoding/json/validator.odin
blob: 31cf038b061c264e8b403bf69165462c835f53ea (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
package json

import "core:mem"

// NOTE(bill): is_valid will not check for duplicate keys
is_valid :: proc(data: []byte, spec := Specification.JSON) -> bool {
	p := make_parser(data, spec, mem.nil_allocator());
	if p.spec == Specification.JSON5 {
		return validate_value(&p);
	}
	return validate_object(&p);
}

validate_object_key :: proc(p: ^Parser) -> bool {
	tok := p.curr_token;
	if p.spec == Specification.JSON5 {
		if tok.kind == .String {
			expect_token(p, .String);
			return true;
		} else if tok.kind == .Ident {
			expect_token(p, .Ident);
			return true;
		}
	}
	err := expect_token(p, .String);
	return err == Error.None;
}
validate_object :: proc(p: ^Parser) -> bool {
	if err := expect_token(p, .Open_Brace); err != Error.None {
		return false;
	}

	for p.curr_token.kind != .Close_Brace {
		if !validate_object_key(p) {
			return false;
		}
		if colon_err := expect_token(p, .Colon); colon_err != Error.None {
			return false;
		}

		if !validate_value(p) {
			return false;
		}

		if p.spec == Specification.JSON5 {
			// Allow trailing commas
			if allow_token(p, .Comma) {
				continue;
			}
		} else {
			// Disallow trailing commas
			if allow_token(p, .Comma) {
				continue;
			} else {
				break;
			}
		}
	}

	if err := expect_token(p, .Close_Brace); err != Error.None {
		return false;
	}
	return true;
}

validate_array :: proc(p: ^Parser) -> bool {
	if err := expect_token(p, .Open_Bracket); err != Error.None {
		return false;
	}

	for p.curr_token.kind != .Close_Bracket {
		if !validate_value(p) {
			return false;
		}

		// Disallow trailing commas for the time being
		if allow_token(p, .Comma) {
			continue;
		} else {
			break;
		}
	}

	if err := expect_token(p, .Close_Bracket); err != Error.None {
		return false;
	}

	return true;
}

validate_value :: proc(p: ^Parser) -> bool {
	token := p.curr_token;

	#partial switch token.kind {
	case .Null, .False, .True:
		advance_token(p);
		return true;
	case .Integer, .Float:
		advance_token(p);
		return true;
	case .String:
		advance_token(p);
		return is_valid_string_literal(token.text, p.spec);

	case .Open_Brace:
		return validate_object(p);

	case .Open_Bracket:
		return validate_array(p);

	case:
		if p.spec == Specification.JSON5 {
			#partial switch token.kind {
			case .Infinity, .NaN:
				advance_token(p);
				return true;
			}
		}
	}

	return false;
}