· 10 years ago · Jul 21, 2016, 08:36 PM
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 = 20160709.16 -- version history at end of file
18local AUTHOR_NOTE = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json) version 20160709.16 ]-"
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 }
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
535 if getmetatable(item) == isNumber then
536 -- it's already a JSON number object.
537 return item
538 elseif type(item) == 'table' and type(item.S) == 'string' and type(item.N) == 'number' then
539 -- it's a number-object table that lots its metatable, so give it one
540 return setmetatable(item, isNumber)
541 else
542 -- the normal situation... given a number or a string representation of a number....
543 local holder = {
544 S = tostring(item), -- S is the representation of the number as a string, which remains precise
545 N = tonumber(item), -- N is the number as a Lua number.
546 }
547 return setmetatable(holder, isNumber)
548 end
549end
550
551local function unicode_codepoint_as_utf8(codepoint)
552 --
553 -- codepoint is a number
554 --
555 if codepoint <= 127 then
556 return string.char(codepoint)
557
558 elseif codepoint <= 2047 then
559 --
560 -- 110yyyxx 10xxxxxx <-- useful notation from http://en.wikipedia.org/wiki/Utf8
561 --
562 local highpart = math.floor(codepoint / 0x40)
563 local lowpart = codepoint - (0x40 * highpart)
564 return string.char(0xC0 + highpart,
565 0x80 + lowpart)
566
567 elseif codepoint <= 65535 then
568 --
569 -- 1110yyyy 10yyyyxx 10xxxxxx
570 --
571 local highpart = math.floor(codepoint / 0x1000)
572 local remainder = codepoint - 0x1000 * highpart
573 local midpart = math.floor(remainder / 0x40)
574 local lowpart = remainder - 0x40 * midpart
575
576 highpart = 0xE0 + highpart
577 midpart = 0x80 + midpart
578 lowpart = 0x80 + lowpart
579
580 --
581 -- Check for an invalid character (thanks Andy R. at Adobe).
582 -- See table 3.7, page 93, in http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#G28070
583 --
584 if ( highpart == 0xE0 and midpart < 0xA0 ) or
585 ( highpart == 0xED and midpart > 0x9F ) or
586 ( highpart == 0xF0 and midpart < 0x90 ) or
587 ( highpart == 0xF4 and midpart > 0x8F )
588 then
589 return "?"
590 else
591 return string.char(highpart,
592 midpart,
593 lowpart)
594 end
595
596 else
597 --
598 -- 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
599 --
600 local highpart = math.floor(codepoint / 0x40000)
601 local remainder = codepoint - 0x40000 * highpart
602 local midA = math.floor(remainder / 0x1000)
603 remainder = remainder - 0x1000 * midA
604 local midB = math.floor(remainder / 0x40)
605 local lowpart = remainder - 0x40 * midB
606
607 return string.char(0xF0 + highpart,
608 0x80 + midA,
609 0x80 + midB,
610 0x80 + lowpart)
611 end
612end
613
614function OBJDEF:onDecodeError(message, text, location, etc)
615 if text then
616 if location then
617 message = string.format("%s at char %d of: %s", message, location, text)
618 else
619 message = string.format("%s: %s", message, text)
620 end
621 end
622
623 if etc ~= nil then
624 message = message .. " (" .. OBJDEF:encode(etc) .. ")"
625 end
626
627 if self.assert then
628 self.assert(false, message)
629 else
630 assert(false, message)
631 end
632end
633
634OBJDEF.onDecodeOfNilError = OBJDEF.onDecodeError
635OBJDEF.onDecodeOfHTMLError = OBJDEF.onDecodeError
636
637function OBJDEF:onEncodeError(message, etc)
638 if etc ~= nil then
639 message = message .. " (" .. OBJDEF:encode(etc) .. ")"
640 end
641
642 if self.assert then
643 self.assert(false, message)
644 else
645 assert(false, message)
646 end
647end
648
649local function grok_number(self, text, start, options)
650 --
651 -- Grab the integer part
652 --
653 local integer_part = text:match('^-?[1-9]%d*', start)
654 or text:match("^-?0", start)
655
656 if not integer_part then
657 self:onDecodeError("expected number", text, start, options.etc)
658 end
659
660 local i = start + integer_part:len()
661
662 --
663 -- Grab an optional decimal part
664 --
665 local decimal_part = text:match('^%.%d+', i) or ""
666
667 i = i + decimal_part:len()
668
669 --
670 -- Grab an optional exponential part
671 --
672 local exponent_part = text:match('^[eE][-+]?%d+', i) or ""
673
674 i = i + exponent_part:len()
675
676 local full_number_text = integer_part .. decimal_part .. exponent_part
677
678 if options.decodeNumbersAsObjects then
679 return OBJDEF:asNumber(full_number_text), i
680 end
681
682 --
683 -- If we're told to stringify under certain conditions, so do.
684 -- We punt a bit when there's an exponent by just stringifying no matter what.
685 -- I suppose we should really look to see whether the exponent is actually big enough one
686 -- way or the other to trip stringification, but I'll be lazy about it until someone asks.
687 --
688 if (options.decodeIntegerStringificationLength
689 and
690 (integer_part:len() >= options.decodeIntegerStringificationLength or exponent_part:len() > 0))
691
692 or
693
694 (options.decodeDecimalStringificationLength
695 and
696 (decimal_part:len() >= options.decodeDecimalStringificationLength or exponent_part:len() > 0))
697 then
698 return full_number_text, i -- this returns the exact string representation seen in the original JSON
699 end
700
701
702
703 local as_number = tonumber(full_number_text)
704
705 if not as_number then
706 self:onDecodeError("bad number", text, start, options.etc)
707 end
708
709 return as_number, i
710end
711
712
713local function grok_string(self, text, start, options)
714
715 if text:sub(start,start) ~= '"' then
716 self:onDecodeError("expected string's opening quote", text, start, options.etc)
717 end
718
719 local i = start + 1 -- +1 to bypass the initial quote
720 local text_len = text:len()
721 local VALUE = ""
722 while i <= text_len do
723 local c = text:sub(i,i)
724 if c == '"' then
725 return VALUE, i + 1
726 end
727 if c ~= '\\' then
728 VALUE = VALUE .. c
729 i = i + 1
730 elseif text:match('^\\b', i) then
731 VALUE = VALUE .. "\b"
732 i = i + 2
733 elseif text:match('^\\f', i) then
734 VALUE = VALUE .. "\f"
735 i = i + 2
736 elseif text:match('^\\n', i) then
737 VALUE = VALUE .. "\n"
738 i = i + 2
739 elseif text:match('^\\r', i) then
740 VALUE = VALUE .. "\r"
741 i = i + 2
742 elseif text:match('^\\t', i) then
743 VALUE = VALUE .. "\t"
744 i = i + 2
745 else
746 local hex = text:match('^\\u([0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
747 if hex then
748 i = i + 6 -- bypass what we just read
749
750 -- We have a Unicode codepoint. It could be standalone, or if in the proper range and
751 -- followed by another in a specific range, it'll be a two-code surrogate pair.
752 local codepoint = tonumber(hex, 16)
753 if codepoint >= 0xD800 and codepoint <= 0xDBFF then
754 -- it's a hi surrogate... see whether we have a following low
755 local lo_surrogate = text:match('^\\u([dD][cdefCDEF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
756 if lo_surrogate then
757 i = i + 6 -- bypass the low surrogate we just read
758 codepoint = 0x2400 + (codepoint - 0xD800) * 0x400 + tonumber(lo_surrogate, 16)
759 else
760 -- not a proper low, so we'll just leave the first codepoint as is and spit it out.
761 end
762 end
763 VALUE = VALUE .. unicode_codepoint_as_utf8(codepoint)
764
765 else
766
767 -- just pass through what's escaped
768 VALUE = VALUE .. text:match('^\\(.)', i)
769 i = i + 2
770 end
771 end
772 end
773
774 self:onDecodeError("unclosed string", text, start, options.etc)
775end
776
777local function skip_whitespace(text, start)
778
779 local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
780 if match_end then
781 return match_end + 1
782 else
783 return start
784 end
785end
786
787local grok_one -- assigned later
788
789local function grok_object(self, text, start, options)
790
791 if text:sub(start,start) ~= '{' then
792 self:onDecodeError("expected '{'", text, start, options.etc)
793 end
794
795 local i = skip_whitespace(text, start + 1) -- +1 to skip the '{'
796
797 local VALUE = self.strictTypes and self:newObject { } or { }
798
799 if text:sub(i,i) == '}' then
800 return VALUE, i + 1
801 end
802 local text_len = text:len()
803 while i <= text_len do
804 local key, new_i = grok_string(self, text, i, options)
805
806 i = skip_whitespace(text, new_i)
807
808 if text:sub(i, i) ~= ':' then
809 self:onDecodeError("expected colon", text, i, options.etc)
810 end
811
812 i = skip_whitespace(text, i + 1)
813
814 local new_val, new_i = grok_one(self, text, i, options)
815
816 VALUE[key] = new_val
817
818 --
819 -- Expect now either '}' to end things, or a ',' to allow us to continue.
820 --
821 i = skip_whitespace(text, new_i)
822
823 local c = text:sub(i,i)
824
825 if c == '}' then
826 return VALUE, i + 1
827 end
828
829 if text:sub(i, i) ~= ',' then
830 self:onDecodeError("expected comma or '}'", text, i, options.etc)
831 end
832
833 i = skip_whitespace(text, i + 1)
834 end
835
836 self:onDecodeError("unclosed '{'", text, start, options.etc)
837end
838
839local function grok_array(self, text, start, options)
840 if text:sub(start,start) ~= '[' then
841 self:onDecodeError("expected '['", text, start, options.etc)
842 end
843
844 local i = skip_whitespace(text, start + 1) -- +1 to skip the '['
845 local VALUE = self.strictTypes and self:newArray { } or { }
846 if text:sub(i,i) == ']' then
847 return VALUE, i + 1
848 end
849
850 local VALUE_INDEX = 1
851
852 local text_len = text:len()
853 while i <= text_len do
854 local val, new_i = grok_one(self, text, i, options)
855
856 -- can't table.insert(VALUE, val) here because it's a no-op if val is nil
857 VALUE[VALUE_INDEX] = val
858 VALUE_INDEX = VALUE_INDEX + 1
859
860 i = skip_whitespace(text, new_i)
861
862 --
863 -- Expect now either ']' to end things, or a ',' to allow us to continue.
864 --
865 local c = text:sub(i,i)
866 if c == ']' then
867 return VALUE, i + 1
868 end
869 if text:sub(i, i) ~= ',' then
870 self:onDecodeError("expected comma or '['", text, i, options.etc)
871 end
872 i = skip_whitespace(text, i + 1)
873 end
874 self:onDecodeError("unclosed '['", text, start, options.etc)
875end
876
877
878grok_one = function(self, text, start, options)
879 -- Skip any whitespace
880 start = skip_whitespace(text, start)
881
882 if start > text:len() then
883 self:onDecodeError("unexpected end of string", text, nil, options.etc)
884 end
885
886 if text:find('^"', start) then
887 return grok_string(self, text, start, options.etc)
888
889 elseif text:find('^[-0123456789 ]', start) then
890 return grok_number(self, text, start, options)
891
892 elseif text:find('^%{', start) then
893 return grok_object(self, text, start, options)
894
895 elseif text:find('^%[', start) then
896 return grok_array(self, text, start, options)
897
898 elseif text:find('^true', start) then
899 return true, start + 4
900
901 elseif text:find('^false', start) then
902 return false, start + 5
903
904 elseif text:find('^null', start) then
905 return nil, start + 4
906
907 else
908 self:onDecodeError("can't parse JSON", text, start, options.etc)
909 end
910end
911
912function OBJDEF:decode(text, etc, options)
913 --
914 -- If the user didn't pass in a table of decode options, make an empty one.
915 --
916 if type(options) ~= 'table' then
917 options = {}
918 end
919
920 --
921 -- If they passed in an 'etc' argument, stuff it into the options.
922 -- (If not, any 'etc' field in the options they passed in remains to be used)
923 --
924 if etc ~= nil then
925 options.etc = etc
926 end
927
928
929 if type(self) ~= 'table' or self.__index ~= OBJDEF then
930 OBJDEF:onDecodeError("JSON:decode must be called in method format", nil, nil, options.etc)
931 end
932
933 if text == nil then
934 self:onDecodeOfNilError(string.format("nil passed to JSON:decode()"), nil, nil, options.etc)
935 elseif type(text) ~= 'string' then
936 self:onDecodeError(string.format("expected string argument to JSON:decode(), got %s", type(text)), nil, nil, options.etc)
937 end
938
939 if text:match('^%s*$') then
940 return nil
941 end
942
943 if text:match('^%s*<') then
944 -- Can't be JSON... we'll assume it's HTML
945 self:onDecodeOfHTMLError(string.format("html passed to JSON:decode()"), text, nil, options.etc)
946 end
947
948 --
949 -- Ensure that it's not UTF-32 or UTF-16.
950 -- Those are perfectly valid encodings for JSON (as per RFC 4627 section 3),
951 -- but this package can't handle them.
952 --
953 if text:sub(1,1):byte() == 0 or (text:len() >= 2 and text:sub(2,2):byte() == 0) then
954 self:onDecodeError("JSON package groks only UTF-8, sorry", text, nil, options.etc)
955 end
956
957 --
958 -- apply global options
959 --
960 if options.decodeNumbersAsObjects == nil then
961 options.decodeNumbersAsObjects = self.decodeNumbersAsObjects
962 end
963 if options.decodeIntegerStringificationLength == nil then
964 options.decodeIntegerStringificationLength = self.decodeIntegerStringificationLength
965 end
966 if options.decodeDecimalStringificationLength == nil then
967 options.decodeDecimalStringificationLength = self.decodeDecimalStringificationLength
968 end
969
970 local success, value = pcall(grok_one, self, text, 1, options)
971
972 if success then
973 return value
974 else
975 -- 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.
976 if self.assert then
977 self.assert(false, value)
978 else
979 assert(false, value)
980 end
981 -- and if we're still here, return a nil and throw the error message on as a second arg
982 return nil, value
983 end
984end
985
986local function backslash_replacement_function(c)
987 if c == "\n" then
988 return "\\n"
989 elseif c == "\r" then
990 return "\\r"
991 elseif c == "\t" then
992 return "\\t"
993 elseif c == "\b" then
994 return "\\b"
995 elseif c == "\f" then
996 return "\\f"
997 elseif c == '"' then
998 return '\\"'
999 elseif c == '\\' then
1000 return '\\\\'
1001 else
1002 return string.format("\\u%04x", c:byte())
1003 end
1004end
1005
1006local chars_to_be_escaped_in_JSON_string
1007 = '['
1008 .. '"' -- class sub-pattern to match a double quote
1009 .. '%\\' -- class sub-pattern to match a backslash
1010 .. '%z' -- class sub-pattern to match a null
1011 .. '\001' .. '-' .. '\031' -- class sub-pattern to match control characters
1012 .. ']'
1013
1014
1015local LINE_SEPARATOR_as_utf8 = unicode_codepoint_as_utf8(0x2028)
1016local PARAGRAPH_SEPARATOR_as_utf8 = unicode_codepoint_as_utf8(0x2029)
1017local function json_string_literal(value, options)
1018 local newval = value:gsub(chars_to_be_escaped_in_JSON_string, backslash_replacement_function)
1019 if options.stringsAreUtf8 then
1020 --
1021 -- This feels really ugly to just look into a string for the sequence of bytes that we know to be a particular utf8 character,
1022 -- but utf8 was designed purposefully to make this kind of thing possible. Still, feels dirty.
1023 -- I'd rather decode the byte stream into a character stream, but it's not technically needed so
1024 -- not technically worth it.
1025 --
1026 newval = newval:gsub(LINE_SEPARATOR_as_utf8, '\\u2028'):gsub(PARAGRAPH_SEPARATOR_as_utf8,'\\u2029')
1027 end
1028 return '"' .. newval .. '"'
1029end
1030
1031local function object_or_array(self, T, etc)
1032 --
1033 -- We need to inspect all the keys... if there are any strings, we'll convert to a JSON
1034 -- object. If there are only numbers, it's a JSON array.
1035 --
1036 -- If we'll be converting to a JSON object, we'll want to sort the keys so that the
1037 -- end result is deterministic.
1038 --
1039 local string_keys = { }
1040 local number_keys = { }
1041 local number_keys_must_be_strings = false
1042 local maximum_number_key
1043
1044 for key in pairs(T) do
1045 if type(key) == 'string' then
1046 table.insert(string_keys, key)
1047 elseif type(key) == 'number' then
1048 table.insert(number_keys, key)
1049 if key <= 0 or key >= math.huge then
1050 number_keys_must_be_strings = true
1051 elseif not maximum_number_key or key > maximum_number_key then
1052 maximum_number_key = key
1053 end
1054 else
1055 self:onEncodeError("can't encode table with a key of type " .. type(key), etc)
1056 end
1057 end
1058
1059 if #string_keys == 0 and not number_keys_must_be_strings then
1060 --
1061 -- An empty table, or a numeric-only array
1062 --
1063 if #number_keys > 0 then
1064 return nil, maximum_number_key -- an array
1065 elseif tostring(T) == "JSON array" then
1066 return nil
1067 elseif tostring(T) == "JSON object" then
1068 return { }
1069 else
1070 -- have to guess, so we'll pick array, since empty arrays are likely more common than empty objects
1071 return nil
1072 end
1073 end
1074
1075 table.sort(string_keys)
1076
1077 local map
1078 if #number_keys > 0 then
1079 --
1080 -- If we're here then we have either mixed string/number keys, or numbers inappropriate for a JSON array
1081 -- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object.
1082 --
1083
1084 if self.noKeyConversion then
1085 self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc)
1086 end
1087
1088 --
1089 -- Have to make a shallow copy of the source table so we can remap the numeric keys to be strings
1090 --
1091 map = { }
1092 for key, val in pairs(T) do
1093 map[key] = val
1094 end
1095
1096 table.sort(number_keys)
1097
1098 --
1099 -- Throw numeric keys in there as strings
1100 --
1101 for _, number_key in ipairs(number_keys) do
1102 local string_key = tostring(number_key)
1103 if map[string_key] == nil then
1104 table.insert(string_keys , string_key)
1105 map[string_key] = T[number_key]
1106 else
1107 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)
1108 end
1109 end
1110 end
1111
1112 return string_keys, nil, map
1113end
1114
1115--
1116-- Encode
1117--
1118-- 'options' is nil, or a table with possible keys:
1119--
1120-- pretty -- If true, return a pretty-printed version.
1121--
1122-- indent -- A string (usually of spaces) used to indent each nested level.
1123--
1124-- align_keys -- If true, align all the keys when formatting a table.
1125--
1126-- null -- If this exists with a string value, table elements with this value are output as JSON null.
1127--
1128-- stringsAreUtf8 -- If true, consider Lua strings not as a sequence of bytes, but as a sequence of UTF-8 characters.
1129-- (Currently, the only practical effect of setting this option is that Unicode LINE and PARAGRAPH
1130-- separators, if found in a string, are encoded with a JSON escape instead of as raw UTF-8.
1131-- The JSON is valid either way, but encoding this way, apparently, allows the resulting JSON
1132-- to also be valid Java.)
1133--
1134--
1135local encode_value -- must predeclare because it calls itself
1136function encode_value(self, value, parents, etc, options, indent, for_key)
1137
1138 --
1139 -- keys in a JSON object can never be null, so we don't even consider options.null when converting a key value
1140 --
1141 if value == nil or (not for_key and options and options.null and value == options.null) then
1142 return 'null'
1143
1144 elseif type(value) == 'string' then
1145 return json_string_literal(value, options)
1146
1147 elseif type(value) == 'number' then
1148 if value ~= value then
1149 --
1150 -- NaN (Not a Number).
1151 -- JSON has no NaN, so we have to fudge the best we can. This should really be a package option.
1152 --
1153 return "null"
1154 elseif value >= math.huge then
1155 --
1156 -- Positive infinity. JSON has no INF, so we have to fudge the best we can. This should
1157 -- really be a package option. Note: at least with some implementations, positive infinity
1158 -- is both ">= math.huge" and "<= -math.huge", which makes no sense but that's how it is.
1159 -- Negative infinity is properly "<= -math.huge". So, we must be sure to check the ">="
1160 -- case first.
1161 --
1162 return "1e+9999"
1163 elseif value <= -math.huge then
1164 --
1165 -- Negative infinity.
1166 -- JSON has no INF, so we have to fudge the best we can. This should really be a package option.
1167 --
1168 return "-1e+9999"
1169 else
1170 return tostring(value)
1171 end
1172
1173 elseif type(value) == 'boolean' then
1174 return tostring(value)
1175
1176 elseif type(value) ~= 'table' then
1177 self:onEncodeError("can't convert " .. type(value) .. " to JSON", etc)
1178
1179 elseif getmetatable(value) == isNumber then
1180 return tostring(value)
1181 else
1182 --
1183 -- A table to be converted to either a JSON object or array.
1184 --
1185 local T = value
1186
1187 if type(options) ~= 'table' then
1188 options = {}
1189 end
1190 if type(indent) ~= 'string' then
1191 indent = ""
1192 end
1193
1194 if parents[T] then
1195 self:onEncodeError("table " .. tostring(T) .. " is a child of itself", etc)
1196 else
1197 parents[T] = true
1198 end
1199
1200 local result_value
1201
1202 local object_keys, maximum_number_key, map = object_or_array(self, T, etc)
1203 if maximum_number_key then
1204 --
1205 -- An array...
1206 --
1207 local ITEMS = { }
1208 for i = 1, maximum_number_key do
1209 table.insert(ITEMS, encode_value(self, T[i], parents, etc, options, indent))
1210 end
1211
1212 if options.pretty then
1213 result_value = "[ " .. table.concat(ITEMS, ", ") .. " ]"
1214 else
1215 result_value = "[" .. table.concat(ITEMS, ",") .. "]"
1216 end
1217
1218 elseif object_keys then
1219 --
1220 -- An object
1221 --
1222 local TT = map or T
1223
1224 if options.pretty then
1225
1226 local KEYS = { }
1227 local max_key_length = 0
1228 for _, key in ipairs(object_keys) do
1229 local encoded = encode_value(self, tostring(key), parents, etc, options, indent, true)
1230 if options.align_keys then
1231 max_key_length = math.max(max_key_length, #encoded)
1232 end
1233 table.insert(KEYS, encoded)
1234 end
1235 local key_indent = indent .. tostring(options.indent or "")
1236 local subtable_indent = key_indent .. string.rep(" ", max_key_length) .. (options.align_keys and " " or "")
1237 local FORMAT = "%s%" .. string.format("%d", max_key_length) .. "s: %s"
1238
1239 local COMBINED_PARTS = { }
1240 for i, key in ipairs(object_keys) do
1241 local encoded_val = encode_value(self, TT[key], parents, etc, options, subtable_indent)
1242 table.insert(COMBINED_PARTS, string.format(FORMAT, key_indent, KEYS[i], encoded_val))
1243 end
1244 result_value = "{\n" .. table.concat(COMBINED_PARTS, ",\n") .. "\n" .. indent .. "}"
1245
1246 else
1247
1248 local PARTS = { }
1249 for _, key in ipairs(object_keys) do
1250 local encoded_val = encode_value(self, TT[key], parents, etc, options, indent)
1251 local encoded_key = encode_value(self, tostring(key), parents, etc, options, indent, true)
1252 table.insert(PARTS, string.format("%s:%s", encoded_key, encoded_val))
1253 end
1254 result_value = "{" .. table.concat(PARTS, ",") .. "}"
1255
1256 end
1257 else
1258 --
1259 -- An empty array/object... we'll treat it as an array, though it should really be an option
1260 --
1261 result_value = "[]"
1262 end
1263
1264 parents[T] = false
1265 return result_value
1266 end
1267end
1268
1269
1270function OBJDEF:encode(value, etc, options)
1271 if type(self) ~= 'table' or self.__index ~= OBJDEF then
1272 OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
1273 end
1274
1275 --
1276 -- If the user didn't pass in a table of decode options, make an empty one.
1277 --
1278 if type(options) ~= 'table' then
1279 options = {}
1280 end
1281
1282 return encode_value(self, value, {}, etc, options)
1283end
1284
1285function OBJDEF:encode_pretty(value, etc, options)
1286 if type(self) ~= 'table' or self.__index ~= OBJDEF then
1287 OBJDEF:onEncodeError("JSON:encode_pretty must be called in method format", etc)
1288 end
1289
1290 --
1291 -- If the user didn't pass in a table of decode options, use the default pretty ones
1292 --
1293 if type(options) ~= 'table' then
1294 options = default_pretty_options
1295 end
1296
1297 return encode_value(self, value, {}, etc, options)
1298end
1299
1300function OBJDEF.__tostring()
1301 return "JSON encode/decode package"
1302end
1303
1304OBJDEF.__index = OBJDEF
1305
1306function OBJDEF:new(args)
1307 local new = { }
1308
1309 if args then
1310 for key, val in pairs(args) do
1311 new[key] = val
1312 end
1313 end
1314
1315 return setmetatable(new, OBJDEF)
1316end
1317
1318return OBJDEF:new()
1319
1320--
1321-- Version history:
1322--
1323-- 20160709.16 Could crash if not passed an options table (thanks jarno heikkinen <jarnoh@capturemonkey.com>).
1324--
1325-- Made JSON:asNumber() a bit more resilient to being passed the results of itself.
1326--
1327-- 20160526.15 Added the ability to easily encode null values in JSON, via the new "null" encoding option.
1328-- (Thanks to Adam B for bringing up the issue.)
1329--
1330-- Added some support for very large numbers and precise floats via
1331-- JSON.decodeNumbersAsObjects
1332-- JSON.decodeIntegerStringificationLength
1333-- JSON.decodeDecimalStringificationLength
1334--
1335-- Added the "stringsAreUtf8" encoding option. (Hat tip to http://lua-users.org/wiki/JsonModules )
1336--
1337-- 20141223.14 The encode_pretty() routine produced fine results for small datasets, but isn't really
1338-- appropriate for anything large, so with help from Alex Aulbach I've made the encode routines
1339-- more flexible, and changed the default encode_pretty() to be more generally useful.
1340--
1341-- Added a third 'options' argument to the encode() and encode_pretty() routines, to control
1342-- how the encoding takes place.
1343--
1344-- Updated docs to add assert() call to the loadfile() line, just as good practice so that
1345-- if there is a problem loading JSON.lua, the appropriate error message will percolate up.
1346--
1347-- 20140920.13 Put back (in a way that doesn't cause warnings about unused variables) the author string,
1348-- so that the source of the package, and its version number, are visible in compiled copies.
1349--
1350-- 20140911.12 Minor lua cleanup.
1351-- Fixed internal reference to 'JSON.noKeyConversion' to reference 'self' instead of 'JSON'.
1352-- (Thanks to SmugMug's David Parry for these.)
1353--
1354-- 20140418.11 JSON nulls embedded within an array were being ignored, such that
1355-- ["1",null,null,null,null,null,"seven"],
1356-- would return
1357-- {1,"seven"}
1358-- It's now fixed to properly return
1359-- {1, nil, nil, nil, nil, nil, "seven"}
1360-- Thanks to "haddock" for catching the error.
1361--
1362-- 20140116.10 The user's JSON.assert() wasn't always being used. Thanks to "blue" for the heads up.
1363--
1364-- 20131118.9 Update for Lua 5.3... it seems that tostring(2/1) produces "2.0" instead of "2",
1365-- and this caused some problems.
1366--
1367-- 20131031.8 Unified the code for encode() and encode_pretty(); they had been stupidly separate,
1368-- and had of course diverged (encode_pretty didn't get the fixes that encode got, so
1369-- sometimes produced incorrect results; thanks to Mattie for the heads up).
1370--
1371-- Handle encoding tables with non-positive numeric keys (unlikely, but possible).
1372--
1373-- If a table has both numeric and string keys, or its numeric keys are inappropriate
1374-- (such as being non-positive or infinite), the numeric keys are turned into
1375-- string keys appropriate for a JSON object. So, as before,
1376-- JSON:encode({ "one", "two", "three" })
1377-- produces the array
1378-- ["one","two","three"]
1379-- but now something with mixed key types like
1380-- JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
1381-- instead of throwing an error produces an object:
1382-- {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
1383--
1384-- To maintain the prior throw-an-error semantics, set
1385-- JSON.noKeyConversion = true
1386--
1387-- 20131004.7 Release under a Creative Commons CC-BY license, which I should have done from day one, sorry.
1388--
1389-- 20130120.6 Comment update: added a link to the specific page on my blog where this code can
1390-- be found, so that folks who come across the code outside of my blog can find updates
1391-- more easily.
1392--
1393-- 20111207.5 Added support for the 'etc' arguments, for better error reporting.
1394--
1395-- 20110731.4 More feedback from David Kolf on how to make the tests for Nan/Infinity system independent.
1396--
1397-- 20110730.3 Incorporated feedback from David Kolf at http://lua-users.org/wiki/JsonModules:
1398--
1399-- * When encoding lua for JSON, Sparse numeric arrays are now handled by
1400-- spitting out full arrays, such that
1401-- JSON:encode({"one", "two", [10] = "ten"})
1402-- returns
1403-- ["one","two",null,null,null,null,null,null,null,"ten"]
1404--
1405-- In 20100810.2 and earlier, only up to the first non-null value would have been retained.
1406--
1407-- * When encoding lua for JSON, numeric value NaN gets spit out as null, and infinity as "1+e9999".
1408-- Version 20100810.2 and earlier created invalid JSON in both cases.
1409--
1410-- * Unicode surrogate pairs are now detected when decoding JSON.
1411--
1412-- 20100810.2 added some checking to ensure that an invalid Unicode character couldn't leak in to the UTF-8 encoding
1413--
1414-- 20100731.1 initial public release
1415--