Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local std=require "libraries/std"
- function parse(source)
- local source,value = consumeValue(source)
- source=consumeWhiteSpace(source)
- if (string.len(source)>0) then error("invalid json input: " .. source) end
- return value;
- end
- function consumeValue(source)
- source = consumeWhiteSpace(source)
- local first = string.byte(source);
- if first == 123 then --'{'
- source = consumeWhiteSpace(string.sub(source, 2))
- local values={}
- while (string.byte(source) ~= 125) do --'}'
- local key, value
- source, key = consumeValue(source)
- source = consumeWhiteSpace(source);
- if string.byte(source) ~= 58 then error("invalid json input: " .. source) end --':'
- source = consumeWhiteSpace(string.sub(source, 2));
- source, value = consumeValue(source)
- source = consumeWhiteSpace(source);
- first=string.byte(source)
- if first ~= 44 and first~=125 then error("invalid json input: " .. source) end --',' ':'
- if first==44 then
- source = consumeWhiteSpace(string.sub(source,2));
- end
- values[key]=value
- end
- return string.sub(source,2,#source),values
- elseif first == 34 then --'"'
- local escaped = false
- local l = 2
- local value = ""
- while (true) do
- local s = string.sub(source, l, l)
- if (s == "\\") then
- escaped = true
- elseif not escaped and s == "\"" then
- break;
- else value = value .. s
- end
- l = l + 1;
- end
- return string.sub(source, l + 1), value
- elseif 48<=first and first<=57 then
- local l=1
- while(48<=first and first<=57) do
- first=string.byte(source,l)
- l=l+1
- end
- return string.sub(source,l-1),string.sub(source,1,l-2)
- else error("invalid json input: " .. source)
- end
- end
- function consumeWhiteSpace(source)
- local i = 1;
- while (string.len(source) >i-1) do
- local c = string.byte(source, i)
- if c == 32 or c == 10 or c == 13 then --' \n\r'
- i = i + 1;
- else break;
- end
- end
- return string.sub(source, i)
- end
- return {
- parse=parse
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement