Annotate

util/json.lua @ 5563:678867c552d1

util.json: Make setmetatable local.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 06 May 2013 19:43:59 -0400
parent 5562:a6b8fb827e2a
child 5565:6dd806829226
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5436
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
1 -- Prosody IM
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
4 --
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
6 -- COPYING file in the source package for more information.
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
7 --
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local type = type;
4474
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
10 local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local s_char = string.char;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local tostring, tonumber = tostring, tonumber;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local pairs, ipairs = pairs, ipairs;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local next = next;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local error = error;
5563
678867c552d1 util.json: Make setmetatable local.
Waqas Hussain <waqas20@gmail.com>
parents: 5562
diff changeset
16 local newproxy, getmetatable, setmetatable = newproxy, getmetatable, setmetatable;
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local print = print;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
5516
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
19 local has_array, array = pcall(require, "util.array");
5562
a6b8fb827e2a util.json: Fix variable name typo which broke util.json when util.array was missing.
Waqas Hussain <waqas20@gmail.com>
parents: 5561
diff changeset
20 local array_mt = has_array and getmetatable(array()) or {};
5516
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
21
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 --module("json")
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local json = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local null = newproxy and newproxy(true) or {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 if getmetatable and getmetatable(null) then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 getmetatable(null).__tostring = function() return "null"; end;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 json.null = null;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 local escapes = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b",
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 ["\f"] = "\\f", ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local unescapes = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 ["\""] = "\"", ["\\"] = "\\", ["/"] = "/",
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 b = "\b", f = "\f", n = "\n", r = "\r", t = "\t"};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 for i=0,31 do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local ch = s_char(i);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
5517
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
42 local function codepoint_to_utf8(code)
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
43 if code < 0x80 then return s_char(code); end
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
44 local bits0_6 = code % 64;
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
45 if code < 0x800 then
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
46 local bits6_5 = (code - bits0_6) / 64;
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
47 return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6);
5436
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
48 end
5517
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
49 local bits0_12 = code % 4096;
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
50 local bits6_6 = (bits0_12 - bits0_6) / 64;
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
51 local bits12_4 = (code - bits0_12) / 4096;
9d7349bbe4d2 util.json: New, improved, fixed codepoint to UTF-8 conversion.
Waqas Hussain <waqas20@gmail.com>
parents: 5516
diff changeset
52 return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6);
5436
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
53 end
a4ba5819bf50 util.json: Convert \uXXXX to UTF-8 when decoding
Matthew Wild <mwild1@gmail.com>
parents: 5395
diff changeset
54
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 local valid_types = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 number = true,
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 string = true,
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 table = true,
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 boolean = true
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 };
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 local special_keys = {
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 __array = true;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 __hash = true;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 };
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 local simplesave, tablesave, arraysave, stringsave;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 function stringsave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 -- FIXME do proper utf-8 and binary data detection
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 t_insert(buffer, "\""..(o:gsub(".", escapes)).."\"");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 function arraysave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 t_insert(buffer, "[");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 if next(o) then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 for i,v in ipairs(o) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 simplesave(v, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 t_remove(buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 t_insert(buffer, "]");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 function tablesave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 local __array = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 local __hash = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 local hash = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 for i,v in ipairs(o) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 __array[i] = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 for k,v in pairs(o) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 local ktype, vtype = type(k), type(v);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 if valid_types[vtype] or v == null then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 if ktype == "string" and not special_keys[k] then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 hash[k] = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 elseif (valid_types[ktype] or k == null) and __array[k] == nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 __hash[k] = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 t_insert(buffer, "{");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 local mark = #buffer;
4474
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
105 if buffer.ordered then
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
106 local keys = {};
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
107 for k in pairs(hash) do
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
108 t_insert(keys, k);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
109 end
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
110 t_sort(keys);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
111 for _,k in ipairs(keys) do
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
112 stringsave(k, buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
113 t_insert(buffer, ":");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
114 simplesave(hash[k], buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
115 t_insert(buffer, ",");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
116 end
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
117 else
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
118 for k,v in pairs(hash) do
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
119 stringsave(k, buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
120 t_insert(buffer, ":");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
121 simplesave(v, buffer);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
122 t_insert(buffer, ",");
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
123 end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 if next(__hash) ~= nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 t_insert(buffer, "\"__hash\":[");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 for k,v in pairs(__hash) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 simplesave(k, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 simplesave(v, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 t_remove(buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 t_insert(buffer, "]");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 if next(__array) then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 t_insert(buffer, "\"__array\":");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 arraysave(__array, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 t_insert(buffer, ",");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 if mark ~= #buffer then t_remove(buffer); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 t_insert(buffer, "}");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 arraysave(__array, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 function simplesave(o, buffer)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 local t = type(o);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 if t == "number" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 t_insert(buffer, tostring(o));
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 elseif t == "string" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 stringsave(o, buffer);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 elseif t == "table" then
5516
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
156 local mt = getmetatable(o);
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
157 if mt == array_mt then
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
158 arraysave(o, buffer);
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
159 else
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
160 tablesave(o, buffer);
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
161 end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 elseif t == "boolean" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 t_insert(buffer, (o and "true" or "false"));
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 t_insert(buffer, "null");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 function json.encode(obj)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 local t = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 simplesave(obj, t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 return t_concat(t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 end
4474
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
174 function json.encode_ordered(obj)
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
175 local t = { ordered = true };
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
176 simplesave(obj, t);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
177 return t_concat(t);
b08a46cf06e6 util.json: Added function encode_ordered(object).
Waqas Hussain <waqas20@gmail.com>
parents: 4404
diff changeset
178 end
5395
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
179 function json.encode_array(obj)
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
180 local t = {};
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
181 arraysave(obj, t);
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
182 return t_concat(t);
ec33d72a08b6 util.json: Add json.encode_array() (thanks B)
Matthew Wild <mwild1@gmail.com>
parents: 4474
diff changeset
183 end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 -----------------------------------
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 function json.decode(json)
4284
20979f124ad9 util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents: 4147
diff changeset
189 json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 local pos = 1;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 local current = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 local stack = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 local ch, peek;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 local function next()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 ch = json:sub(pos, pos);
4284
20979f124ad9 util.json: Fixed handling of truncated JSON.
Waqas Hussain <waqas20@gmail.com>
parents: 4147
diff changeset
196 if ch == "" then error("Unexpected EOF"); end
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 pos = pos+1;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 peek = json:sub(pos, pos);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 return ch;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 local function skipwhitespace()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 local function skiplinecomment()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 repeat next(); until not(ch) or ch == "\r" or ch == "\n";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 skipwhitespace();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 local function skipstarcomment()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 next(); next(); -- skip '/', '*'
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 while peek and ch ~= "*" and peek ~= "/" do next(); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 if not peek then error("eof in star comment") end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 next(); next(); -- skip '*', '/'
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 skipwhitespace();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 local function skipstuff()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 while true do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 skipwhitespace();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 if ch == "/" and peek == "*" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 skipstarcomment();
4404
5356664ef9d4 util.json: Fix for single-line comments (thanks Norbert Kiesel)
Matthew Wild <mwild1@gmail.com>
parents: 4284
diff changeset
223 elseif ch == "/" and peek == "/" then
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 skiplinecomment();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 return;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 local readvalue;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 local function readarray()
5516
9733836629f9 util.json: Make encode(decode("[]"))=="[]".
Waqas Hussain <waqas20@gmail.com>
parents: 5436
diff changeset
233 local t = setmetatable({}, array_mt);
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 next(); -- skip '['
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 if ch == "]" then next(); return t; end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 t_insert(t, readvalue());
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 while true do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 if ch == "]" then next(); return t; end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 if not ch then error("eof while reading array");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 elseif ch == "," then next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 elseif ch then error("unexpected character in array, comma expected"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 if not ch then error("eof while reading array"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 t_insert(t, readvalue());
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 local function checkandskip(c)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 local x = ch or "eof";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 local function readliteral(lit, val)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 for c in lit:gmatch(".") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 checkandskip(c);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 return val;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 local function readstring()
5561
52eef11cd8af util.json: Optimize long string parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 5517
diff changeset
261 local s = {};
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262 checkandskip("\"");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 while ch do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 while ch and ch ~= "\\" and ch ~= "\"" do
5561
52eef11cd8af util.json: Optimize long string parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 5517
diff changeset
265 t_insert(s, ch); next();
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
266 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
267 if ch == "\\" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
269 if unescapes[ch] then
5561
52eef11cd8af util.json: Optimize long string parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 5517
diff changeset
270 t_insert(s, unescapes[ch]);
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
272 elseif ch == "u" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
273 local seq = "";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
274 for i=1,4 do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
275 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
276 if not ch then error("unexpected eof in string"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
277 if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
278 seq = seq..ch;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
279 end
5561
52eef11cd8af util.json: Optimize long string parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 5517
diff changeset
280 t_insert(s, codepoint_to_utf8(tonumber(seq, 16)));
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
281 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
282 else error("invalid escape sequence in string"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
283 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
284 if ch == "\"" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
285 next();
5561
52eef11cd8af util.json: Optimize long string parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 5517
diff changeset
286 return t_concat(s);
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
287 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
288 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
289 error("eof while reading string");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
290 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
291 local function readnumber()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 local s = "";
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
293 if ch == "-" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
294 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
295 if not ch:match("[0-9]") then error("number format error"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
296 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
297 if ch == "0" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
298 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
299 if ch:match("[0-9]") then error("number format error"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
300 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
301 while ch and ch:match("[0-9]") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
302 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
303 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
304 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
305 if ch == "." then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
306 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
307 if not ch:match("[0-9]") then error("number format error"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
308 while ch and ch:match("[0-9]") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
309 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
311 if ch == "e" or ch == "E" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
312 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
313 if ch == "+" or ch == "-" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
314 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
315 if not ch:match("[0-9]") then error("number format error"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
316 while ch and ch:match("[0-9]") do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
317 s = s..ch; next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
318 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
319 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
320 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
321 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
322 return tonumber(s);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
323 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
324 local function readmember(t)
4147
7f119ebcf55f util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3979
diff changeset
325 skipstuff();
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
326 local k = readstring();
4147
7f119ebcf55f util.json: Fixed a parse error caused by unexpected whitespace.
Waqas Hussain <waqas20@gmail.com>
parents: 3979
diff changeset
327 skipstuff();
3979
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
328 checkandskip(":");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
329 t[k] = readvalue();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
330 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
331 local function fixobject(obj)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
332 local __array = obj.__array;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
333 if __array then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
334 obj.__array = nil;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
335 for i,v in ipairs(__array) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 t_insert(obj, v);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
339 local __hash = obj.__hash;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
340 if __hash then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
341 obj.__hash = nil;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
342 local k;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
343 for i,v in ipairs(__hash) do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
344 if k ~= nil then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
345 obj[k] = v; k = nil;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
346 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
347 k = v;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
348 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
349 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
350 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
351 return obj;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
352 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
353 local function readobject()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
354 local t = {};
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355 next(); -- skip '{'
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
356 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
357 if ch == "}" then next(); return t; end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
358 if not ch then error("eof while reading object"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
359 readmember(t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
360 while true do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
361 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
362 if ch == "}" then next(); return fixobject(t); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
363 if not ch then error("eof while reading object");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
364 elseif ch == "," then next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
365 elseif ch then error("unexpected character in object, comma expected"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
366 if not ch then error("eof while reading object"); end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
367 readmember(t);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
368 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
369 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
370
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
371 function readvalue()
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
372 skipstuff();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
373 while ch do
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
374 if ch == "{" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
375 return readobject();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
376 elseif ch == "[" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
377 return readarray();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
378 elseif ch == "\"" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
379 return readstring();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
380 elseif ch:match("[%-0-9%.]") then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
381 return readnumber();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
382 elseif ch == "n" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
383 return readliteral("null", null);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
384 elseif ch == "t" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
385 return readliteral("true", true);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
386 elseif ch == "f" then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
387 return readliteral("false", false);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
388 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
389 error("invalid character at value start: "..ch);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
390 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
391 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
392 error("eof while reading value");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
393 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
394 next();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
395 return readvalue();
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
396 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
397
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
398 function json.test(object)
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
399 local encoded = json.encode(object);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
400 local decoded = json.decode(encoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
401 local recoded = json.encode(decoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
402 if encoded ~= recoded then
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
403 print("FAILED");
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
404 print("encoded:", encoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
405 print("recoded:", recoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
406 else
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
407 print(encoded);
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
408 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
409 return encoded == recoded;
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
410 end
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
411
bf223e6c2b4c util.json: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
412 return json;