· 10 years ago · Jun 12, 2016, 11:09 AM
1-- -*- coding: utf-8 -*-
2--
3-- Simple JSON encoding and decoding in pure Lua.
4--
5-- Copyright 2010-2016 Jeffrey Friedl
6-- http://regex.info/blog/
7--
8-- Latest version: http://regex.info/blog/lua/json
9--
10-- This code is released under a Creative Commons CC-BY "Attribution" License:
11-- http://creativecommons.org/licenses/by/3.0/deed.en_US
12--
13-- It can be used for any purpose so long as the copyright notice above,
14-- the web-page links above, and the 'AUTHOR_NOTE' string below are
15-- maintained. Enjoy.
16--
17local VERSION = 20160526.15 -- version history at end of file
18local AUTHOR_NOTE = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json) version 20160526.15 ]-"
19
20--
21-- The 'AUTHOR_NOTE' variable exists so that information about the source
22-- of the package is maintained even in compiled versions. It's also
23-- included in OBJDEF below mostly to quiet warnings about unused variables.
24--
25local OBJDEF = {
26 VERSION = VERSION,
27 AUTHOR_NOTE = AUTHOR_NOTE,
28}
29
30
31--
32-- Simple JSON encoding and decoding in pure Lua.
33-- JSON definition: http://www.json.org/
34--
35--
36-- JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
37--
38-- local lua_value = JSON:decode(raw_json_text)
39--
40-- local raw_json_text = JSON:encode(lua_table_or_value)
41-- local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version for human readability
42--
43--
44--
45-- DECODING (from a JSON string to a Lua table)
46--
47--
48-- JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
49--
50-- local lua_value = JSON:decode(raw_json_text)
51--
52-- If the JSON text is for an object or an array, e.g.
53-- { "what": "books", "count": 3 }
54-- or
55-- [ "Larry", "Curly", "Moe" ]
56--
57-- the result is a Lua table, e.g.
58-- { what = "books", count = 3 }
59-- or
60-- { "Larry", "Curly", "Moe" }
61--
62--
63-- The encode and decode routines accept an optional second argument,
64-- "etc", which is not used during encoding or decoding, but upon error
65-- is passed along to error handlers. It can be of any type (including nil).
66--
67--
68--
69-- ERROR HANDLING
70--
71-- With most errors during decoding, this code calls
72--
73-- JSON:onDecodeError(message, text, location, etc)
74--
75-- with a message about the error, and if known, the JSON text being
76-- parsed and the byte count where the problem was discovered. You can
77-- replace the default JSON:onDecodeError() with your own function.
78--
79-- The default onDecodeError() merely augments the message with data
80-- about the text and the location if known (and if a second 'etc'
81-- argument had been provided to decode(), its value is tacked onto the
82-- message as well), and then calls JSON.assert(), which itself defaults
83-- to Lua's built-in assert(), and can also be overridden.
84--
85-- For example, in an Adobe Lightroom plugin, you might use something like
86--
87-- function JSON:onDecodeError(message, text, location, etc)
88-- LrErrors.throwUserError("Internal Error: invalid JSON data")
89-- end
90--
91-- or even just
92--
93-- function JSON.assert(message)
94-- LrErrors.throwUserError("Internal Error: " .. message)
95-- end
96--
97-- If JSON:decode() is passed a nil, this is called instead:
98--
99-- JSON:onDecodeOfNilError(message, nil, nil, etc)
100--
101-- and if JSON:decode() is passed HTML instead of JSON, this is called:
102--
103-- JSON:onDecodeOfHTMLError(message, text, nil, etc)
104--
105-- The use of the fourth 'etc' argument allows stronger coordination
106-- between decoding and error reporting, especially when you provide your
107-- own error-handling routines. Continuing with the the Adobe Lightroom
108-- plugin example:
109--
110-- function JSON:onDecodeError(message, text, location, etc)
111-- local note = "Internal Error: invalid JSON data"
112-- if type(etc) = 'table' and etc.photo then
113-- note = note .. " while processing for " .. etc.photo:getFormattedMetadata('fileName')
114-- end
115-- LrErrors.throwUserError(note)
116-- end
117--
118-- :
119-- :
120--
121-- for i, photo in ipairs(photosToProcess) do
122-- :
123-- :
124-- local data = JSON:decode(someJsonText, { photo = photo })
125-- :
126-- :
127-- end
128--
129--
130--
131--
132--
133-- DECODING AND STRICT TYPES
134--
135-- Because both JSON objects and JSON arrays are converted to Lua tables,
136-- it's not normally possible to tell which original JSON type a
137-- particular Lua table was derived from, or guarantee decode-encode
138-- round-trip equivalency.
139--
140-- However, if you enable strictTypes, e.g.
141--
142-- JSON = assert(loadfile "JSON.lua")() --load the routines
143-- JSON.strictTypes = true
144--
145-- then the Lua table resulting from the decoding of a JSON object or
146-- JSON array is marked via Lua metatable, so that when re-encoded with
147-- JSON:encode() it ends up as the appropriate JSON type.
148--
149-- (This is not the default because other routines may not work well with
150-- tables that have a metatable set, for example, Lightroom API calls.)
151--
152--
153-- ENCODING (from a lua table to a JSON string)
154--
155-- JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
156--
157-- local raw_json_text = JSON:encode(lua_table_or_value)
158-- local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version for human readability
159-- local custom_pretty = JSON:encode(lua_table_or_value, etc, { pretty = true, indent = "| ", align_keys = false })
160--
161-- On error during encoding, this code calls:
162--
163-- JSON:onEncodeError(message, etc)
164--
165-- which you can override in your local JSON object.
166--
167-- The 'etc' in the error call is the second argument to encode()
168-- and encode_pretty(), or nil if it wasn't provided.
169--
170--
171-- ENCODING OPTIONS
172--
173-- An optional third argument, a table of options, can be provided to encode().
174--
175-- encode_options = {
176-- -- options for making "pretty" human-readable JSON (see "PRETTY-PRINTING" below)
177-- pretty = true,
178-- indent = " ",
179-- align_keys = false,
180--
181-- -- other output-related options
182-- null = "\0", -- see "ENCODING JSON NULL VALUES" below
183-- stringsAreUtf8 = false, -- see "HANDLING UNICODE LINE AND PARAGRAPH SEPARATORS FOR JAVA" below
184-- }
185--
186-- json_string = JSON:encode(mytable, etc, encode_options)
187--
188--
189--
190-- For reference, the defaults are:
191--
192-- pretty = false
193-- null = nil,
194-- stringsAreUtf8 = false,
195--
196--
197--
198-- PRETTY-PRINTING
199--
200-- Enabling the 'pretty' encode option helps generate human-readable JSON.
201--
202-- pretty = JSON:encode(val, etc, {
203-- pretty = true,
204-- indent = " ",
205-- align_keys = false,
206-- })
207--
208-- encode_pretty() is also provided: it's identical to encode() except
209-- that encode_pretty() provides a default options table if none given in the call:
210--
211-- { pretty = true, align_keys = false, indent = " " }
212--
213-- For example, if
214--
215-- JSON:encode(data)
216--
217-- produces:
218--
219-- {"city":"Kyoto","climate":{"avg_temp":16,"humidity":"high","snowfall":"minimal"},"country":"Japan","wards":11}
220--
221-- then
222--
223-- JSON:encode_pretty(data)
224--
225-- produces:
226--
227-- {
228-- "city": "Kyoto",
229-- "climate": {
230-- "avg_temp": 16,
231-- "humidity": "high",
232-- "snowfall": "minimal"
233-- },
234-- "country": "Japan",
235-- "wards": 11
236-- }
237--
238-- The following three lines return identical results:
239-- JSON:encode_pretty(data)
240-- JSON:encode_pretty(data, nil, { pretty = true, align_keys = false, indent = " " })
241-- JSON:encode (data, nil, { pretty = true, align_keys = false, indent = " " })
242--
243-- An example of setting your own indent string:
244--
245-- JSON:encode_pretty(data, nil, { pretty = true, indent = "| " })
246--
247-- produces:
248--
249-- {
250-- | "city": "Kyoto",
251-- | "climate": {
252-- | | "avg_temp": 16,
253-- | | "humidity": "high",
254-- | | "snowfall": "minimal"
255-- | },
256-- | "country": "Japan",
257-- | "wards": 11
258-- }
259--
260-- An example of setting align_keys to true:
261--
262-- JSON:encode_pretty(data, nil, { pretty = true, indent = " ", align_keys = true })
263--
264-- produces:
265--
266-- {
267-- "city": "Kyoto",
268-- "climate": {
269-- "avg_temp": 16,
270-- "humidity": "high",
271-- "snowfall": "minimal"
272-- },
273-- "country": "Japan",
274-- "wards": 11
275-- }
276--
277-- which I must admit is kinda ugly, sorry. This was the default for
278-- encode_pretty() prior to version 20141223.14.
279--
280--
281-- HANDLING UNICODE LINE AND PARAGRAPH SEPARATORS FOR JAVA
282--
283-- If the 'stringsAreUtf8' encode option is set to true, consider Lua strings not as a sequence of bytes,
284-- but as a sequence of UTF-8 characters.
285--
286-- Currently, the only practical effect of setting this option is that Unicode LINE and PARAGRAPH
287-- separators, if found in a string, are encoded with a JSON escape instead of being dumped as is.
288-- The JSON is valid either way, but encoding this way, apparently, allows the resulting JSON
289-- to also be valid Java.
290--
291-- AMBIGUOUS SITUATIONS DURING THE ENCODING
292--
293-- During the encode, if a Lua table being encoded contains both string
294-- and numeric keys, it fits neither JSON's idea of an object, nor its
295-- idea of an array. To get around this, when any string key exists (or
296-- when non-positive numeric keys exist), numeric keys are converted to
297-- strings.
298--
299-- For example,
300-- JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
301-- produces the JSON object
302-- {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
303--
304-- To prohibit this conversion and instead make it an error condition, set
305-- JSON.noKeyConversion = true
306--
307--
308-- ENCODING JSON NULL VALUES
309--
310-- Lua tables completely omit keys whose value is nil, so without special handling there's
311-- no way to get a field in a JSON object with a null value. For example
312-- JSON:encode({ username = "admin", password = nil })
313-- produces
314-- {"username":"admin"}
315--
316-- In order to actually produce
317-- {"username":"admin", "password":null}
318-- one can include a string value for a "null" field in the options table passed to encode()....
319-- any Lua table entry with that value becomes null in the JSON output:
320-- JSON:encode({ username = "admin", password = "xyzzy" }, nil, { null = "xyzzy" })
321-- produces
322-- {"username":"admin", "password":null}
323--
324-- Just be sure to use a string that is otherwise unlikely to appear in your data.
325-- The string "\0" (a string with one null byte) may well be appropriate for many applications.
326--
327-- The "null" options also applies to Lua tables that become JSON arrays.
328-- JSON:encode({ "one", "two", nil, nil })
329-- produces
330-- ["one","two"]
331-- while
332-- NULL = "\0"
333-- JSON:encode({ "one", "two", NULL, NULL}, nil, { null = NULL })
334-- produces
335-- ["one","two",null,null]
336--
337--
338-- HANDLING LARGE AND/OR PRECISE NUMBERS
339--
340-- Without special handling, numbers in JSON can lose precision in Lua.
341-- For example:
342--
343-- T = JSON:decode('{ "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345 }')
344--
345-- print("small: ", type(T.small), T.small)
346-- print("big: ", type(T.big), T.big)
347-- print("precise: ", type(T.precise), T.precise)
348--
349-- produces
350--
351-- small: number 12345
352-- big: number 1.2345678901235e+28
353-- precise: number 9876.6789012346
354--
355-- Precision is lost with both 'big' and 'precise'.
356--
357-- This package offers ways to try to handle this better (for some definitions of "better")...
358--
359-- The most precise method is by setting the global:
360--
361-- JSON.decodeNumbersAsObjects = true
362--
363-- When this is set, numeric JSON data is encoded into Lua in a form that preserves the exact
364-- JSON numeric presentation when re-encoded back out to JSON, or accessed in Lua as a string.
365--
366-- (This is done by encoding the numeric data with a Lua table/metatable that returns
367-- the possibly-imprecise numeric form when accessed numerically, but the original precise
368-- representation when accessed as a string.)
369--
370-- Consider the example above, with this option turned on:
371--
372-- JSON.decodeNumbersAsObjects = true
373--
374-- T = JSON:decode('{ "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345 }')
375--
376-- print("small: ", type(T.small), T.small)
377-- print("big: ", type(T.big), T.big)
378-- print("precise: ", type(T.precise), T.precise)
379--
380-- This now produces:
381--
382-- small: table 12345
383-- big: table 12345678901234567890123456789
384-- precise: table 9876.67890123456789012345
385--
386-- However, within Lua you can still use the values (e.g. T.precise in the example above) in numeric
387-- contexts. In such cases you'll get the possibly-imprecise numeric version, but in string contexts
388-- and when the data finds its way to this package's encode() function, the original full-precision
389-- representation is used.
390--
391-- Even without using the JSON.decodeNumbersAsObjects option, you can encode numbers
392-- in your Lua table that retain high precision upon encoding to JSON, by using the JSON:asNumber()
393-- function:
394--
395-- T = {
396-- imprecise = 123456789123456789.123456789123456789,
397-- precise = JSON:asNumber("123456789123456789.123456789123456789")
398-- }
399--
400-- print(JSON:encode_pretty(T))
401--
402-- This produces:
403--
404-- {
405-- "precise": 123456789123456789.123456789123456789,
406-- "imprecise": 1.2345678912346e+17
407-- }
408--
409--
410--
411-- A different way to handle big/precise JSON numbers is to have decode() merely return
412-- the exact string representation of the number instead of the number itself.
413-- This approach might be useful when the numbers are merely some kind of opaque
414-- object identifier and you want to work with them in Lua as strings anyway.
415--
416-- This approach is enabled by setting
417--
418-- JSON.decodeIntegerStringificationLength = 10
419--
420-- The value is the number of digits (of the integer part of the number) at which to stringify numbers.
421--
422-- Consider our previous example with this option set to 10:
423--
424-- JSON.decodeIntegerStringificationLength = 10
425--
426-- T = JSON:decode('{ "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345 }')
427--
428-- print("small: ", type(T.small), T.small)
429-- print("big: ", type(T.big), T.big)
430-- print("precise: ", type(T.precise), T.precise)
431--
432-- This produces:
433--
434-- small: number 12345
435-- big: string 12345678901234567890123456789
436-- precise: number 9876.6789012346
437--
438-- The long integer of the 'big' field is at least JSON.decodeIntegerStringificationLength digits
439-- in length, so it's converted not to a Lua integer but to a Lua string. Using a value of 0 or 1 ensures
440-- that all JSON numeric data becomes strings in Lua.
441--
442-- Note that unlike
443-- JSON.decodeNumbersAsObjects = true
444-- this stringification is simple and unintelligent: the JSON number simply becomes a Lua string, and that's the end of it.
445-- If the string is then converted back to JSON, it's still a string. After running the code above, adding
446-- print(JSON:encode(T))
447-- produces
448-- {"big":"12345678901234567890123456789","precise":9876.6789012346,"small":12345}
449-- which is unlikely to be desired.
450--
451-- There's a comparable option for the length of the decimal part of a number:
452--
453-- JSON.decodeDecimalStringificationLength
454--
455-- This can be used alone or in conjunction with
456--
457-- JSON.decodeIntegerStringificationLength
458--
459-- to trip stringification on precise numbers with at least JSON.decodeIntegerStringificationLength digits after
460-- the decimal point.
461--
462-- This example:
463--
464-- JSON.decodeIntegerStringificationLength = 10
465-- JSON.decodeDecimalStringificationLength = 5
466--
467-- T = JSON:decode('{ "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345 }')
468--
469-- print("small: ", type(T.small), T.small)
470-- print("big: ", type(T.big), T.big)
471-- print("precise: ", type(T.precise), T.precise)
472--
473-- produces:
474--
475-- small: number 12345
476-- big: string 12345678901234567890123456789
477-- precise: string 9876.67890123456789012345
478--
479--
480--
481--
482--
483-- SUMMARY OF METHODS YOU CAN OVERRIDE IN YOUR LOCAL LUA JSON OBJECT
484--
485-- assert
486-- onDecodeError
487-- onDecodeOfNilError
488-- onDecodeOfHTMLError
489-- onEncodeError
490--
491-- If you want to create a separate Lua JSON object with its own error handlers,
492-- you can reload JSON.lua or use the :new() method.
493--
494---------------------------------------------------------------------------
495
496local default_pretty_indent = " "
497local default_pretty_options = { pretty = true, align_keys = false, indent = default_pretty_indent, null = nil }
498
499local isArray = { __tostring = function() return "JSON array" end } isArray.__index = isArray
500local isObject = { __tostring = function() return "JSON object" end } isObject.__index = isObject
501
502function OBJDEF:newArray(tbl)
503 return setmetatable(tbl or {}, isArray)
504end
505
506function OBJDEF:newObject(tbl)
507 return setmetatable(tbl or {}, isObject)
508end
509
510
511
512
513local function getnum(op)
514 return type(op) == 'number' and op or op.N
515end
516
517local isNumber = {
518 __index = isNumber,
519 __tostring = function(T) return T.S end,
520
521 __add = function(op1, op2) return getnum(op1) + getnum(op2) end,
522 __sub = function(op1, op2) return getnum(op1) - getnum(op2) end,
523 __mul = function(op1, op2) return getnum(op1) * getnum(op2) end,
524 __div = function(op1, op2) return getnum(op1) / getnum(op2) end,
525 __mod = function(op1, op2) return getnum(op1) % getnum(op2) end,
526 __pow = function(op1, op2) return getnum(op1) ^ getnum(op2) end,
527 __lt = function(op1, op2) return getnum(op1) < getnum(op2) end,
528 __eq = function(op1, op2) return getnum(op1) == getnum(op2) end,
529 __le = function(op1, op2) return getnum(op1) <= getnum(op2) end,
530 __unm = function(op) return getnum(op) end,
531}
532
533function OBJDEF:asNumber(item)
534 local holder = {
535 S = tostring(item), -- S is the representation of the number as a string, which remains precise
536 N = tonumber(item), -- N is the number as a Lua number.
537 }
538 return setmetatable(holder, isNumber)
539end
540
541local function unicode_codepoint_as_utf8(codepoint)
542 --
543 -- codepoint is a number
544 --
545 if codepoint <= 127 then
546 return string.char(codepoint)
547
548 elseif codepoint <= 2047 then
549 --
550 -- 110yyyxx 10xxxxxx <-- useful notation from http://en.wikipedia.org/wiki/Utf8
551 --
552 local highpart = math.floor(codepoint / 0x40)
553 local lowpart = codepoint - (0x40 * highpart)
554 return string.char(0xC0 + highpart,
555 0x80 + lowpart)
556
557 elseif codepoint <= 65535 then
558 --
559 -- 1110yyyy 10yyyyxx 10xxxxxx
560 --
561 local highpart = math.floor(codepoint / 0x1000)
562 local remainder = codepoint - 0x1000 * highpart
563 local midpart = math.floor(remainder / 0x40)
564 local lowpart = remainder - 0x40 * midpart
565
566 highpart = 0xE0 + highpart
567 midpart = 0x80 + midpart
568 lowpart = 0x80 + lowpart
569
570 --
571 -- Check for an invalid character (thanks Andy R. at Adobe).
572 -- See table 3.7, page 93, in http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#G28070
573 --
574 if ( highpart == 0xE0 and midpart < 0xA0 ) or
575 ( highpart == 0xED and midpart > 0x9F ) or
576 ( highpart == 0xF0 and midpart < 0x90 ) or
577 ( highpart == 0xF4 and midpart > 0x8F )
578 then
579 return "?"
580 else
581 return string.char(highpart,
582 midpart,
583 lowpart)
584 end
585
586 else
587 --
588 -- 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
589 --
590 local highpart = math.floor(codepoint / 0x40000)
591 local remainder = codepoint - 0x40000 * highpart
592 local midA = math.floor(remainder / 0x1000)
593 remainder = remainder - 0x1000 * midA
594 local midB = math.floor(remainder / 0x40)
595 local lowpart = remainder - 0x40 * midB
596
597 return string.char(0xF0 + highpart,
598 0x80 + midA,
599 0x80 + midB,
600 0x80 + lowpart)
601 end
602end
603
604function OBJDEF:onDecodeError(message, text, location, etc)
605 if text then
606 if location then
607 message = string.format("%s at char %d of: %s", message, location, text)
608 else
609 message = string.format("%s: %s", message, text)
610 end
611 end
612
613 if etc ~= nil then
614 message = message .. " (" .. OBJDEF:encode(etc) .. ")"
615 end
616
617 if self.assert then
618 self.assert(false, message)
619 else
620 assert(false, message)
621 end
622end
623
624OBJDEF.onDecodeOfNilError = OBJDEF.onDecodeError
625OBJDEF.onDecodeOfHTMLError = OBJDEF.onDecodeError
626
627function OBJDEF:onEncodeError(message, etc)
628 if etc ~= nil then
629 message = message .. " (" .. OBJDEF:encode(etc) .. ")"
630 end
631
632 if self.assert then
633 self.assert(false, message)
634 else
635 assert(false, message)
636 end
637end
638
639local function grok_number(self, text, start, options)
640 --
641 -- Grab the integer part
642 --
643 local integer_part = text:match('^-?[1-9]%d*', start)
644 or text:match("^-?0", start)
645
646 if not integer_part then
647 self:onDecodeError("expected number", text, start, options.etc)
648 end
649
650 local i = start + integer_part:len()
651
652 --
653 -- Grab an optional decimal part
654 --
655 local decimal_part = text:match('^%.%d+', i) or ""
656
657 i = i + decimal_part:len()
658
659 --
660 -- Grab an optional exponential part
661 --
662 local exponent_part = text:match('^[eE][-+]?%d+', i) or ""
663
664 i = i + exponent_part:len()
665
666 local full_number_text = integer_part .. decimal_part .. exponent_part
667
668 if options.decodeNumbersAsObjects then
669 return OBJDEF:asNumber(full_number_text), i
670 end
671
672 --
673 -- If we're told to stringify under certain conditions, so do.
674 -- We punt a bit when there's an exponent by just stringifying no matter what.
675 -- I suppose we should really look to see whether the exponent is actually big enough one
676 -- way or the other to trip stringification, but I'll be lazy about it until someone asks.
677 --
678 if (options.decodeIntegerStringificationLength
679 and
680 (integer_part:len() >= options.decodeIntegerStringificationLength or exponent_part:len() > 0))
681
682 or
683
684 (options.decodeDecimalStringificationLength
685 and
686 (decimal_part:len() >= options.decodeDecimalStringificationLength or exponent_part:len() > 0))
687 then
688 return full_number_text, i -- this returns the exact string representation seen in the original JSON
689 end
690
691
692
693 local as_number = tonumber(full_number_text)
694
695 if not as_number then
696 self:onDecodeError("bad number", text, start, options.etc)
697 end
698
699 return as_number, i
700end
701
702
703local function grok_string(self, text, start, options)
704
705 if text:sub(start,start) ~= '"' then
706 self:onDecodeError("expected string's opening quote", text, start, options.etc)
707 end
708
709 local i = start + 1 -- +1 to bypass the initial quote
710 local text_len = text:len()
711 local VALUE = ""
712 while i <= text_len do
713 local c = text:sub(i,i)
714 if c == '"' then
715 return VALUE, i + 1
716 end
717 if c ~= '\\' then
718 VALUE = VALUE .. c
719 i = i + 1
720 elseif text:match('^\\b', i) then
721 VALUE = VALUE .. "\b"
722 i = i + 2
723 elseif text:match('^\\f', i) then
724 VALUE = VALUE .. "\f"
725 i = i + 2
726 elseif text:match('^\\n', i) then
727 VALUE = VALUE .. "\n"
728 i = i + 2
729 elseif text:match('^\\r', i) then
730 VALUE = VALUE .. "\r"
731 i = i + 2
732 elseif text:match('^\\t', i) then
733 VALUE = VALUE .. "\t"
734 i = i + 2
735 else
736 local hex = text:match('^\\u([0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
737 if hex then
738 i = i + 6 -- bypass what we just read
739
740 -- We have a Unicode codepoint. It could be standalone, or if in the proper range and
741 -- followed by another in a specific range, it'll be a two-code surrogate pair.
742 local codepoint = tonumber(hex, 16)
743 if codepoint >= 0xD800 and codepoint <= 0xDBFF then
744 -- it's a hi surrogate... see whether we have a following low
745 local lo_surrogate = text:match('^\\u([dD][cdefCDEF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
746 if lo_surrogate then
747 i = i + 6 -- bypass the low surrogate we just read
748 codepoint = 0x2400 + (codepoint - 0xD800) * 0x400 + tonumber(lo_surrogate, 16)
749 else
750 -- not a proper low, so we'll just leave the first codepoint as is and spit it out.
751 end
752 end
753 VALUE = VALUE .. unicode_codepoint_as_utf8(codepoint)
754
755 else
756
757 -- just pass through what's escaped
758 VALUE = VALUE .. text:match('^\\(.)', i)
759 i = i + 2
760 end
761 end
762 end
763
764 self:onDecodeError("unclosed string", text, start, options.etc)
765end
766
767local function skip_whitespace(text, start)
768
769 local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
770 if match_end then
771 return match_end + 1
772 else
773 return start
774 end
775end
776
777local grok_one -- assigned later
778
779local function grok_object(self, text, start, options)
780 if text:sub(start,start) ~= '{' then
781 self:onDecodeError("expected '{'", text, start, options.etc)
782 end
783
784 local i = skip_whitespace(text, start + 1) -- +1 to skip the '{'
785
786 local VALUE = self.strictTypes and self:newObject { } or { }
787
788 if text:sub(i,i) == '}' then
789 return VALUE, i + 1
790 end
791 local text_len = text:len()
792 while i <= text_len do
793 local key, new_i = grok_string(self, text, i, options)
794
795 i = skip_whitespace(text, new_i)
796
797 if text:sub(i, i) ~= ':' then
798 self:onDecodeError("expected colon", text, i, options.etc)
799 end
800
801 i = skip_whitespace(text, i + 1)
802
803 local new_val, new_i = grok_one(self, text, i, options)
804
805 VALUE[key] = new_val
806
807 --
808 -- Expect now either '}' to end things, or a ',' to allow us to continue.
809 --
810 i = skip_whitespace(text, new_i)
811
812 local c = text:sub(i,i)
813
814 if c == '}' then
815 return VALUE, i + 1
816 end
817
818 if text:sub(i, i) ~= ',' then
819 self:onDecodeError("expected comma or '}'", text, i, options.etc)
820 end
821
822 i = skip_whitespace(text, i + 1)
823 end
824
825 self:onDecodeError("unclosed '{'", text, start, options.etc)
826end
827
828local function grok_array(self, text, start, options)
829 if text:sub(start,start) ~= '[' then
830 self:onDecodeError("expected '['", text, start, options.etc)
831 end
832
833 local i = skip_whitespace(text, start + 1) -- +1 to skip the '['
834 local VALUE = self.strictTypes and self:newArray { } or { }
835 if text:sub(i,i) == ']' then
836 return VALUE, i + 1
837 end
838
839 local VALUE_INDEX = 1
840
841 local text_len = text:len()
842 while i <= text_len do
843 local val, new_i = grok_one(self, text, i, options)
844
845 -- can't table.insert(VALUE, val) here because it's a no-op if val is nil
846 VALUE[VALUE_INDEX] = val
847 VALUE_INDEX = VALUE_INDEX + 1
848
849 i = skip_whitespace(text, new_i)
850
851 --
852 -- Expect now either ']' to end things, or a ',' to allow us to continue.
853 --
854 local c = text:sub(i,i)
855 if c == ']' then
856 return VALUE, i + 1
857 end
858 if text:sub(i, i) ~= ',' then
859 self:onDecodeError("expected comma or '['", text, i, options.etc)
860 end
861 i = skip_whitespace(text, i + 1)
862 end
863 self:onDecodeError("unclosed '['", text, start, options.etc)
864end
865
866
867grok_one = function(self, text, start, options)
868 -- Skip any whitespace
869 start = skip_whitespace(text, start)
870
871 if start > text:len() then
872 self:onDecodeError("unexpected end of string", text, nil, options.etc)
873 end
874
875 if text:find('^"', start) then
876 return grok_string(self, text, start, options.etc)
877
878 elseif text:find('^[-0123456789 ]', start) then
879 return grok_number(self, text, start, options)
880
881 elseif text:find('^%{', start) then
882 return grok_object(self, text, start, options)
883
884 elseif text:find('^%[', start) then
885 return grok_array(self, text, start, options)
886
887 elseif text:find('^true', start) then
888 return true, start + 4
889
890 elseif text:find('^false', start) then
891 return false, start + 5
892
893 elseif text:find('^null', start) then
894 return nil, start + 4
895
896 else
897 self:onDecodeError("can't parse JSON", text, start, options.etc)
898 end
899end
900
901function OBJDEF:decode(text, etc, options)
902 --
903 -- If the user didn't pass in a table of decode options, make an empty one.
904 --
905 if type(options) ~= 'table' then
906 options = {}
907 end
908
909 --
910 -- If they passed in an 'etc' argument, stuff it into the options.
911 -- (If not, any 'etc' field in the options they passed in remains to be used)
912 --
913 if etc ~= nil then
914 options.etc = etc
915 end
916
917
918 if type(self) ~= 'table' or self.__index ~= OBJDEF then
919 OBJDEF:onDecodeError("JSON:decode must be called in method format", nil, nil, options.etc)
920 end
921
922 if text == nil then
923 self:onDecodeOfNilError(string.format("nil passed to JSON:decode()"), nil, nil, options.etc)
924 elseif type(text) ~= 'string' then
925 self:onDecodeError(string.format("expected string argument to JSON:decode(), got %s", type(text)), nil, nil, options.etc)
926 end
927
928 if text:match('^%s*$') then
929 return nil
930 end
931
932 if text:match('^%s*<') then
933 -- Can't be JSON... we'll assume it's HTML
934 self:onDecodeOfHTMLError(string.format("html passed to JSON:decode()"), text, nil, options.etc)
935 end
936
937 --
938 -- Ensure that it's not UTF-32 or UTF-16.
939 -- Those are perfectly valid encodings for JSON (as per RFC 4627 section 3),
940 -- but this package can't handle them.
941 --
942 if text:sub(1,1):byte() == 0 or (text:len() >= 2 and text:sub(2,2):byte() == 0) then
943 self:onDecodeError("JSON package groks only UTF-8, sorry", text, nil, options.etc)
944 end
945
946 --
947 -- apply global options
948 --
949 if options.decodeNumbersAsObjects == nil then
950 options.decodeNumbersAsObjects = self.decodeNumbersAsObjects
951 end
952 if options.decodeIntegerStringificationLength == nil then
953 options.decodeIntegerStringificationLength = self.decodeIntegerStringificationLength
954 end
955 if options.decodeDecimalStringificationLength == nil then
956 options.decodeDecimalStringificationLength = self.decodeDecimalStringificationLength
957 end
958
959 local success, value = pcall(grok_one, self, text, 1, options)
960
961 if success then
962 return value
963 else
964 -- if JSON:onDecodeError() didn't abort out of the pcall, we'll have received the error message here as "value", so pass it along as an assert.
965 if self.assert then
966 self.assert(false, value)
967 else
968 assert(false, value)
969 end
970 -- and if we're still here, return a nil and throw the error message on as a second arg
971 return nil, value
972 end
973end
974
975local function backslash_replacement_function(c)
976 if c == "\n" then
977 return "\\n"
978 elseif c == "\r" then
979 return "\\r"
980 elseif c == "\t" then
981 return "\\t"
982 elseif c == "\b" then
983 return "\\b"
984 elseif c == "\f" then
985 return "\\f"
986 elseif c == '"' then
987 return '\\"'
988 elseif c == '\\' then
989 return '\\\\'
990 else
991 return string.format("\\u%04x", c:byte())
992 end
993end
994
995local chars_to_be_escaped_in_JSON_string
996 = '['
997 .. '"' -- class sub-pattern to match a double quote
998 .. '%\\' -- class sub-pattern to match a backslash
999 .. '%z' -- class sub-pattern to match a null
1000 .. '\001' .. '-' .. '\031' -- class sub-pattern to match control characters
1001 .. ']'
1002
1003
1004local LINE_SEPARATOR_as_utf8 = unicode_codepoint_as_utf8(0x2028)
1005local PARAGRAPH_SEPARATOR_as_utf8 = unicode_codepoint_as_utf8(0x2029)
1006local function json_string_literal(value, options)
1007 local newval = value:gsub(chars_to_be_escaped_in_JSON_string, backslash_replacement_function)
1008 if options.stringsAreUtf8 then
1009 --
1010 -- This feels really ugly to just look into a string for the sequence of bytes that we know to be a particular utf8 character,
1011 -- but utf8 was designed purposefully to make this kind of thing possible. Still, feels dirty.
1012 -- I'd rather decode the byte stream into a character stream, but it's not technically needed so
1013 -- not technically worth it.
1014 --
1015 newval = newval:gsub(LINE_SEPARATOR_as_utf8, '\\u2028'):gsub(PARAGRAPH_SEPARATOR_as_utf8,'\\u2029')
1016 end
1017 return '"' .. newval .. '"'
1018end
1019
1020local function object_or_array(self, T, etc)
1021 --
1022 -- We need to inspect all the keys... if there are any strings, we'll convert to a JSON
1023 -- object. If there are only numbers, it's a JSON array.
1024 --
1025 -- If we'll be converting to a JSON object, we'll want to sort the keys so that the
1026 -- end result is deterministic.
1027 --
1028 local string_keys = { }
1029 local number_keys = { }
1030 local number_keys_must_be_strings = false
1031 local maximum_number_key
1032
1033 for key in pairs(T) do
1034 if type(key) == 'string' then
1035 table.insert(string_keys, key)
1036 elseif type(key) == 'number' then
1037 table.insert(number_keys, key)
1038 if key <= 0 or key >= math.huge then
1039 number_keys_must_be_strings = true
1040 elseif not maximum_number_key or key > maximum_number_key then
1041 maximum_number_key = key
1042 end
1043 else
1044 self:onEncodeError("can't encode table with a key of type " .. type(key), etc)
1045 end
1046 end
1047
1048 if #string_keys == 0 and not number_keys_must_be_strings then
1049 --
1050 -- An empty table, or a numeric-only array
1051 --
1052 if #number_keys > 0 then
1053 return nil, maximum_number_key -- an array
1054 elseif tostring(T) == "JSON array" then
1055 return nil
1056 elseif tostring(T) == "JSON object" then
1057 return { }
1058 else
1059 -- have to guess, so we'll pick array, since empty arrays are likely more common than empty objects
1060 return nil
1061 end
1062 end
1063
1064 table.sort(string_keys)
1065
1066 local map
1067 if #number_keys > 0 then
1068 --
1069 -- If we're here then we have either mixed string/number keys, or numbers inappropriate for a JSON array
1070 -- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object.
1071 --
1072
1073 if self.noKeyConversion then
1074 self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc)
1075 end
1076
1077 --
1078 -- Have to make a shallow copy of the source table so we can remap the numeric keys to be strings
1079 --
1080 map = { }
1081 for key, val in pairs(T) do
1082 map[key] = val
1083 end
1084
1085 table.sort(number_keys)
1086
1087 --
1088 -- Throw numeric keys in there as strings
1089 --
1090 for _, number_key in ipairs(number_keys) do
1091 local string_key = tostring(number_key)
1092 if map[string_key] == nil then
1093 table.insert(string_keys , string_key)
1094 map[string_key] = T[number_key]
1095 else
1096 self:onEncodeError("conflict converting table with mixed-type keys into a JSON object: key " .. number_key .. " exists both as a string and a number.", etc)
1097 end
1098 end
1099 end
1100
1101 return string_keys, nil, map
1102end
1103
1104--
1105-- Encode
1106--
1107-- 'options' is nil, or a table with possible keys:
1108--
1109-- pretty -- If true, return a pretty-printed version.
1110--
1111-- indent -- A string (usually of spaces) used to indent each nested level.
1112--
1113-- align_keys -- If true, align all the keys when formatting a table.
1114--
1115-- null -- If this exists with a string value, table elements with this value are output as JSON null.
1116--
1117-- stringsAreUtf8 -- If true, consider Lua strings not as a sequence of bytes, but as a sequence of UTF-8 characters.
1118-- (Currently, the only practical effect of setting this option is that Unicode LINE and PARAGRAPH
1119-- separators, if found in a string, are encoded with a JSON escape instead of as raw UTF-8.
1120-- The JSON is valid either way, but encoding this way, apparently, allows the resulting JSON
1121-- to also be valid Java.)
1122--
1123--
1124local encode_value -- must predeclare because it calls itself
1125function encode_value(self, value, parents, etc, options, indent, for_key)
1126
1127 --
1128 -- keys in a JSON object can never be null, so we don't even consider options.null when converting a key value
1129 --
1130 if value == nil or (not for_key and options and options.null and value == options.null) then
1131 return 'null'
1132
1133 elseif type(value) == 'string' then
1134 return json_string_literal(value, options)
1135
1136 elseif type(value) == 'number' then
1137 if value ~= value then
1138 --
1139 -- NaN (Not a Number).
1140 -- JSON has no NaN, so we have to fudge the best we can. This should really be a package option.
1141 --
1142 return "null"
1143 elseif value >= math.huge then
1144 --
1145 -- Positive infinity. JSON has no INF, so we have to fudge the best we can. This should
1146 -- really be a package option. Note: at least with some implementations, positive infinity
1147 -- is both ">= math.huge" and "<= -math.huge", which makes no sense but that's how it is.
1148 -- Negative infinity is properly "<= -math.huge". So, we must be sure to check the ">="
1149 -- case first.
1150 --
1151 return "1e+9999"
1152 elseif value <= -math.huge then
1153 --
1154 -- Negative infinity.
1155 -- JSON has no INF, so we have to fudge the best we can. This should really be a package option.
1156 --
1157 return "-1e+9999"
1158 else
1159 return tostring(value)
1160 end
1161
1162 elseif type(value) == 'boolean' then
1163 return tostring(value)
1164
1165 elseif type(value) ~= 'table' then
1166 self:onEncodeError("can't convert " .. type(value) .. " to JSON", etc)
1167
1168 elseif getmetatable(value) == isNumber then
1169 return tostring(value)
1170 else
1171 --
1172 -- A table to be converted to either a JSON object or array.
1173 --
1174 local T = value
1175
1176 if type(options) ~= 'table' then
1177 options = {}
1178 end
1179 if type(indent) ~= 'string' then
1180 indent = ""
1181 end
1182
1183 if parents[T] then
1184 self:onEncodeError("table " .. tostring(T) .. " is a child of itself", etc)
1185 else
1186 parents[T] = true
1187 end
1188
1189 local result_value
1190
1191 local object_keys, maximum_number_key, map = object_or_array(self, T, etc)
1192 if maximum_number_key then
1193 --
1194 -- An array...
1195 --
1196 local ITEMS = { }
1197 for i = 1, maximum_number_key do
1198 table.insert(ITEMS, encode_value(self, T[i], parents, etc, options, indent))
1199 end
1200
1201 if options.pretty then
1202 result_value = "[ " .. table.concat(ITEMS, ", ") .. " ]"
1203 else
1204 result_value = "[" .. table.concat(ITEMS, ",") .. "]"
1205 end
1206
1207 elseif object_keys then
1208 --
1209 -- An object
1210 --
1211 local TT = map or T
1212
1213 if options.pretty then
1214
1215 local KEYS = { }
1216 local max_key_length = 0
1217 for _, key in ipairs(object_keys) do
1218 local encoded = encode_value(self, tostring(key), parents, etc, options, indent, true)
1219 if options.align_keys then
1220 max_key_length = math.max(max_key_length, #encoded)
1221 end
1222 table.insert(KEYS, encoded)
1223 end
1224 local key_indent = indent .. tostring(options.indent or "")
1225 local subtable_indent = key_indent .. string.rep(" ", max_key_length) .. (options.align_keys and " " or "")
1226 local FORMAT = "%s%" .. string.format("%d", max_key_length) .. "s: %s"
1227
1228 local COMBINED_PARTS = { }
1229 for i, key in ipairs(object_keys) do
1230 local encoded_val = encode_value(self, TT[key], parents, etc, options, subtable_indent)
1231 table.insert(COMBINED_PARTS, string.format(FORMAT, key_indent, KEYS[i], encoded_val))
1232 end
1233 result_value = "{\n" .. table.concat(COMBINED_PARTS, ",\n") .. "\n" .. indent .. "}"
1234
1235 else
1236
1237 local PARTS = { }
1238 for _, key in ipairs(object_keys) do
1239 local encoded_val = encode_value(self, TT[key], parents, etc, options, indent)
1240 local encoded_key = encode_value(self, tostring(key), parents, etc, options, indent, true)
1241 table.insert(PARTS, string.format("%s:%s", encoded_key, encoded_val))
1242 end
1243 result_value = "{" .. table.concat(PARTS, ",") .. "}"
1244
1245 end
1246 else
1247 --
1248 -- An empty array/object... we'll treat it as an array, though it should really be an option
1249 --
1250 result_value = "[]"
1251 end
1252
1253 parents[T] = false
1254 return result_value
1255 end
1256end
1257
1258
1259function OBJDEF:encode(value, etc, options)
1260 if type(self) ~= 'table' or self.__index ~= OBJDEF then
1261 OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
1262 end
1263 return encode_value(self, value, {}, etc, options or nil)
1264end
1265
1266function OBJDEF:encode_pretty(value, etc, options)
1267 if type(self) ~= 'table' or self.__index ~= OBJDEF then
1268 OBJDEF:onEncodeError("JSON:encode_pretty must be called in method format", etc)
1269 end
1270 return encode_value(self, value, {}, etc, options or default_pretty_options)
1271end
1272
1273function OBJDEF.__tostring()
1274 return "JSON encode/decode package"
1275end
1276
1277OBJDEF.__index = OBJDEF
1278
1279function OBJDEF:new(args)
1280 local new = { }
1281
1282 if args then
1283 for key, val in pairs(args) do
1284 new[key] = val
1285 end
1286 end
1287
1288 return setmetatable(new, OBJDEF)
1289end
1290
1291return OBJDEF:new()
1292
1293--
1294-- Version history:
1295--
1296-- 20160526.15 Added the ability to easily encode null values in JSON, via the new "null" encoding option.
1297-- (Thanks to Adam B for bringing up the issue.)
1298--
1299-- Added some support for very large numbers and precise floats via
1300-- JSON.decodeNumbersAsObjects
1301-- JSON.decodeIntegerStringificationLength
1302-- JSON.decodeDecimalStringificationLength
1303--
1304-- Added the "stringsAreUtf8" encoding option. (Hat tip to http://lua-users.org/wiki/JsonModules )
1305--
1306-- 20141223.14 The encode_pretty() routine produced fine results for small datasets, but isn't really
1307-- appropriate for anything large, so with help from Alex Aulbach I've made the encode routines
1308-- more flexible, and changed the default encode_pretty() to be more generally useful.
1309--
1310-- Added a third 'options' argument to the encode() and encode_pretty() routines, to control
1311-- how the encoding takes place.
1312--
1313-- Updated docs to add assert() call to the loadfile() line, just as good practice so that
1314-- if there is a problem loading JSON.lua, the appropriate error message will percolate up.
1315--
1316-- 20140920.13 Put back (in a way that doesn't cause warnings about unused variables) the author string,
1317-- so that the source of the package, and its version number, are visible in compiled copies.
1318--
1319-- 20140911.12 Minor lua cleanup.
1320-- Fixed internal reference to 'JSON.noKeyConversion' to reference 'self' instead of 'JSON'.
1321-- (Thanks to SmugMug's David Parry for these.)
1322--
1323-- 20140418.11 JSON nulls embedded within an array were being ignored, such that
1324-- ["1",null,null,null,null,null,"seven"],
1325-- would return
1326-- {1,"seven"}
1327-- It's now fixed to properly return
1328-- {1, nil, nil, nil, nil, nil, "seven"}
1329-- Thanks to "haddock" for catching the error.
1330--
1331-- 20140116.10 The user's JSON.assert() wasn't always being used. Thanks to "blue" for the heads up.
1332--
1333-- 20131118.9 Update for Lua 5.3... it seems that tostring(2/1) produces "2.0" instead of "2",
1334-- and this caused some problems.
1335--
1336-- 20131031.8 Unified the code for encode() and encode_pretty(); they had been stupidly separate,
1337-- and had of course diverged (encode_pretty didn't get the fixes that encode got, so
1338-- sometimes produced incorrect results; thanks to Mattie for the heads up).
1339--
1340-- Handle encoding tables with non-positive numeric keys (unlikely, but possible).
1341--
1342-- If a table has both numeric and string keys, or its numeric keys are inappropriate
1343-- (such as being non-positive or infinite), the numeric keys are turned into
1344-- string keys appropriate for a JSON object. So, as before,
1345-- JSON:encode({ "one", "two", "three" })
1346-- produces the array
1347-- ["one","two","three"]
1348-- but now something with mixed key types like
1349-- JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
1350-- instead of throwing an error produces an object:
1351-- {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
1352--
1353-- To maintain the prior throw-an-error semantics, set
1354-- JSON.noKeyConversion = true
1355--
1356-- 20131004.7 Release under a Creative Commons CC-BY license, which I should have done from day one, sorry.
1357--
1358-- 20130120.6 Comment update: added a link to the specific page on my blog where this code can
1359-- be found, so that folks who come across the code outside of my blog can find updates
1360-- more easily.
1361--
1362-- 20111207.5 Added support for the 'etc' arguments, for better error reporting.
1363--
1364-- 20110731.4 More feedback from David Kolf on how to make the tests for Nan/Infinity system independent.
1365--
1366-- 20110730.3 Incorporated feedback from David Kolf at http://lua-users.org/wiki/JsonModules:
1367--
1368-- * When encoding lua for JSON, Sparse numeric arrays are now handled by
1369-- spitting out full arrays, such that
1370-- JSON:encode({"one", "two", [10] = "ten"})
1371-- returns
1372-- ["one","two",null,null,null,null,null,null,null,"ten"]
1373--
1374-- In 20100810.2 and earlier, only up to the first non-null value would have been retained.
1375--
1376-- * When encoding lua for JSON, numeric value NaN gets spit out as null, and infinity as "1+e9999".
1377-- Version 20100810.2 and earlier created invalid JSON in both cases.
1378--
1379-- * Unicode surrogate pairs are now detected when decoding JSON.
1380--
1381-- 20100810.2 added some checking to ensure that an invalid Unicode character couldn't leak in to the UTF-8 encoding
1382--
1383-- 20100731.1 initial public release
1384--