Comparison

util/json.lua @ 4284:20979f124ad9

util.json: Fixed handling of truncated JSON.
author Waqas Hussain <waqas20@gmail.com>
date Thu, 02 Jun 2011 05:36:15 +0500
parent 4147:7f119ebcf55f
child 4404:5356664ef9d4
comparison
equal deleted inserted replaced
4282:f5a46b73432b 4284:20979f124ad9
132 132
133 ----------------------------------- 133 -----------------------------------
134 134
135 135
136 function json.decode(json) 136 function json.decode(json)
137 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
137 local pos = 1; 138 local pos = 1;
138 local current = {}; 139 local current = {};
139 local stack = {}; 140 local stack = {};
140 local ch, peek; 141 local ch, peek;
141 local function next() 142 local function next()
142 ch = json:sub(pos, pos); 143 ch = json:sub(pos, pos);
144 if ch == "" then error("Unexpected EOF"); end
143 pos = pos+1; 145 pos = pos+1;
144 peek = json:sub(pos, pos); 146 peek = json:sub(pos, pos);
145 return ch; 147 return ch;
146 end 148 end
147 149