-
Notifications
You must be signed in to change notification settings - Fork 51
/
parse_json.m
184 lines (158 loc) · 5.64 KB
/
parse_json.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
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
function [data json] = parse_json(json)
% [DATA JSON] = PARSE_JSON(json)
% This function parses a JSON string and returns a cell array with the
% parsed data. JSON objects are converted to structures and JSON arrays are
% converted to cell arrays.
%
% Example:
% google_search = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=matlab';
% matlab_results = parse_json(urlread(google_search));
% disp(matlab_results{1}.responseData.results{1}.titleNoFormatting)
% disp(matlab_results{1}.responseData.results{1}.visibleUrl)
data = cell(0,1);
while ~isempty(json)
[value json] = parse_value(json);
data{end+1} = value; %#ok<AGROW>
end
end
function [value json] = parse_value(json)
value = [];
if ~isempty(json)
id = json(1);
json(1) = [];
json = strtrim(json);
switch lower(id)
case '"'
[value json] = parse_string(json);
case '{'
[value json] = parse_object(json);
case '['
[value json] = parse_array(json);
case 't'
value = true;
if (length(json) >= 3)
json(1:3) = [];
else
ME = MException('json:parse_value',['Invalid TRUE identifier: ' id json]);
ME.throw;
end
case 'f'
value = false;
if (length(json) >= 4)
json(1:4) = [];
else
ME = MException('json:parse_value',['Invalid FALSE identifier: ' id json]);
ME.throw;
end
case 'n'
value = [];
if (length(json) >= 3)
json(1:3) = [];
else
ME = MException('json:parse_value',['Invalid NULL identifier: ' id json]);
ME.throw;
end
otherwise
[value json] = parse_number([id json]); % Need to put the id back on the string
end
end
end
function [data json] = parse_array(json)
data = cell(0,1);
while ~isempty(json)
if strcmp(json(1),']') % Check if the array is closed
json(1) = [];
return
end
[value json] = parse_value(json);
if isempty(value)
ME = MException('json:parse_array',['Parsed an empty value: ' json]);
ME.throw;
end
data{end+1} = value; %#ok<AGROW>
while ~isempty(json) && ~isempty(regexp(json(1),'[\s,]','once'))
json(1) = [];
end
end
end
function [data json] = parse_object(json)
data = [];
while ~isempty(json)
id = json(1);
json(1) = [];
switch id
case '"' % Start a name/value pair
[name value remaining_json] = parse_name_value(json);
if isempty(name)
ME = MException('json:parse_object',['Can not have an empty name: ' json]);
ME.throw;
end
data.(name) = value;
json = remaining_json;
case '}' % End of object, so exit the function
return
otherwise % Ignore other characters
end
end
end
function [name value json] = parse_name_value(json)
name = [];
value = [];
if ~isempty(json)
[name json] = parse_string(json);
% Skip spaces and the : separator
while ~isempty(json) && ~isempty(regexp(json(1),'[\s:]','once'))
json(1) = [];
end
[value json] = parse_value(json);
end
end
function [string json] = parse_string(json)
string = [];
while ~isempty(json)
letter = json(1);
json(1) = [];
switch lower(letter)
case '\' % Deal with escaped characters
if ~isempty(json)
code = json(1);
json(1) = [];
switch lower(code)
case '"'
new_char = '"';
case '\'
new_char = '\';
case '/'
new_char = '/';
case {'b' 'f' 'n' 'r' 't'}
new_char = sprintf('\%c',code);
case 'u'
if length(json) >= 4
new_char = sprintf('\\u%s',json(1:4));
json(1:4) = [];
end
otherwise
new_char = [];
end
end
case '"' % Done with the string
return
otherwise
new_char = letter;
end
% Append the new character
string = [string new_char]; %#ok<AGROW>
end
end
function [num json] = parse_number(json)
num = [];
if ~isempty(json)
% Validate the floating point number using a regular expression
[s e] = regexp(json,'^[\w]?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[\w]?','once');
if ~isempty(s)
num_str = json(s:e);
json(s:e) = [];
num = str2double(strtrim(num_str));
end
end
end