forked from bastibe/transplant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsejson_test.m
52 lines (43 loc) · 1.24 KB
/
parsejson_test.m
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
%% String parsing
if ~strcmp(parsejson('"ABCdef123"'), 'ABCdef123')
error('String parsing failed')
end
%% String parsing with unicode
if ~strcmp(parsejson('"Bl\u00E4\u00DFhuhn"'), 'Bläßhuhn')
error('String parsing with unicode failed')
end
%% String parsing with escaped characters
if ~strcmp(parsejson('"\\\t\r\n\f\b\"\/\\"'), sprintf('\\\t\r\n\f\b"/\\'))
error('String parsing with escaped characters failed')
end
%% Number parsing
if parsejson('-12.34e56') ~= -12.34e56
error('Number parsing failed')
end
%% Bool parsing
bool = parsejson('true');
if bool ~= true || ~islogical(bool)
error('Boolean parsing failed')
end
bool = parsejson('false');
if bool ~= false || ~islogical(bool)
error('Boolean parsing failed')
end
%% Null parsing
if parsejson('null') ~= []
error('Null parsing failed')
end
%% Array parsing
if ~isequal(parsejson('[1, "a", true]'), {1 'a' true})
error('Array parsing failed')
end
%% Object parsing
s = parsejson('{"test": 1, "foo": "bar"}');
if s('test') ~= 1 || ~strcmp(s('foo'), 'bar') || length(keys(s)) ~= 2
error('Object parsing failed')
end
%% Data roundtrip
data = '[{"test":1},true,null,"\u00FCber"]';
if ~isequal(data, dumpjson(parsejson(data)))
error('Data roundtrip failed')
end