66-- 
77--  includes unreleased upstream changes: https://github.com/rxi/json.lua/blob/dbf4b2dd2eb7c23be2773c89eb059dadd6436f94/json.lua
88--  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
910-- 
1011--  Permission is hereby granted, free of charge, to any person obtaining a copy of
1112--  this software and associated documentation files (the "Software"), to deal in
@@ -74,6 +75,9 @@ local function encode_nil(val)
7475  return  " null" 
7576end 
7677
78+ local  function  encode_string (val )
79+   return  ' "'  ..  val :gsub (' [%z\1 -\31 \\ "]' escape_char ) ..  ' "' 
80+ end 
7781
7882local  function  encode_table (val , stack )
7983  local  res  =  {}
@@ -84,44 +88,48 @@ local function encode_table(val, stack)
8488
8589  stack [val ] =  true 
8690
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 
90106    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 
93109      end 
94-       n  =  n  +  1 
95-     end 
96-     if  n  ~=  # val  then 
97-       error (" invalid table: sparse array" 
98110    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 
102118    end 
103119    stack [val ] =  nil 
104120    return  " ["  ..  table.concat (res , " ," ..  " ]" 
105- 
106-   else 
107-     --  Treat as an object
121+   elseif  types [" string" then 
122+     --  Treat as object
108123    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 ))
113125    end 
114126    stack [val ] =  nil 
115127    return  " {"  ..  table.concat (res , " ," ..  " }" 
128+   else 
129+     error ( string.format (" invalid table: unsupported key type %s" types [1 ]) )
116130  end 
117131end 
118132
119- 
120- local  function  encode_string (val )
121-   return  ' "'  ..  val :gsub (' [%z\1 -\31 \\ "]' escape_char ) ..  ' "' 
122- end 
123- 
124- 
125133local  function  encode_number (val )
126134  --  Check for NaN, -inf and inf
127135  if  val  ~=  val  or  val  <=  - math.huge  or  val  >=  math.huge  then 
0 commit comments