6
6
--
7
7
-- includes unreleased upstream changes: https://github.com/rxi/json.lua/blob/dbf4b2dd2eb7c23be2773c89eb059dadd6436f94/json.lua
8
8
-- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/51
9
+ -- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/52
9
10
--
10
11
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
11
12
-- this software and associated documentation files (the "Software"), to deal in
@@ -74,6 +75,9 @@ local function encode_nil(val)
74
75
return " null"
75
76
end
76
77
78
+ local function encode_string (val )
79
+ return ' "' .. val :gsub (' [%z\1 -\31 \\ "]' , escape_char ) .. ' "'
80
+ end
77
81
78
82
local function encode_table (val , stack )
79
83
local res = {}
@@ -84,44 +88,48 @@ local function encode_table(val, stack)
84
88
85
89
stack [val ] = true
86
90
87
- if rawget (val , 1 ) ~= nil or next (val ) == nil then
88
- -- Treat as array -- check keys are valid and it is not sparse
89
- local n = 0
91
+ if next (val ) == nil then
92
+ return ' []'
93
+ end
94
+
95
+ local types = {}
96
+
97
+ for k in pairs (val ) do
98
+ types [type (k )] = true
99
+ end
100
+
101
+ if # types > 1 then
102
+ error (" invalid table: mixed or invalid key types" )
103
+ elseif types [" number" ] then
104
+ -- Treat as array
105
+ local max_key = 0
90
106
for k in pairs (val ) do
91
- if type ( k ) ~= " number " then
92
- error ( " invalid table: mixed or invalid key types " )
107
+ if k > max_key then
108
+ max_key = k
93
109
end
94
- n = n + 1
95
- end
96
- if n ~= # val then
97
- error (" invalid table: sparse array" )
98
110
end
99
- -- Encode
100
- for i , v in ipairs (val ) do
101
- table.insert (res , encode (v , stack ))
111
+ for i = 1 , max_key do
112
+ if val [i ] == nil then
113
+ table.insert (res , " null" )
114
+ else
115
+ local v = encode (val [i ], stack )
116
+ table.insert (res , v )
117
+ end
102
118
end
103
119
stack [val ] = nil
104
120
return " [" .. table.concat (res , " ," ) .. " ]"
105
-
106
- else
107
- -- Treat as an object
121
+ elseif types [" string" ] then
122
+ -- Treat as object
108
123
for k , v in pairsByKeys (val ) do
109
- if type (k ) ~= " string" then
110
- error (" invalid table: mixed or invalid key types" )
111
- end
112
- table.insert (res , encode (k , stack ) .. " :" .. encode (v , stack ))
124
+ table.insert (res , encode_string (k ) .. " :" .. encode (v , stack ))
113
125
end
114
126
stack [val ] = nil
115
127
return " {" .. table.concat (res , " ," ) .. " }"
128
+ else
129
+ error ( string.format (" invalid table: unsupported key type %s" , types [1 ]) )
116
130
end
117
131
end
118
132
119
-
120
- local function encode_string (val )
121
- return ' "' .. val :gsub (' [%z\1 -\31 \\ "]' , escape_char ) .. ' "'
122
- end
123
-
124
-
125
133
local function encode_number (val )
126
134
-- Check for NaN, -inf and inf
127
135
if val ~= val or val <= - math.huge or val >= math.huge then
0 commit comments