Guest User

Untitled

a guest
Jun 11th, 2014
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.85 KB | None | 0 0
  1. if not JSON then
  2.     local VERSION = 20140418.11  -- version history at end of file
  3.     local OBJDEF = { VERSION = VERSION }
  4.     local author = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json), version " .. tostring(VERSION) .. " ]-"
  5.     local isArray  = { __tostring = function() return "JSON array"  end }    isArray.__index  = isArray
  6.     local isObject = { __tostring = function() return "JSON object" end }    isObject.__index = isObject
  7.  
  8.  
  9.     function OBJDEF:newArray(tbl)
  10.        return setmetatable(tbl or {}, isArray)
  11.     end
  12.  
  13.     function OBJDEF:newObject(tbl)
  14.        return setmetatable(tbl or {}, isObject)
  15.     end
  16.  
  17.     local function unicode_codepoint_as_utf8(codepoint)
  18.        --
  19.        -- codepoint is a number
  20.        --
  21.        if codepoint <= 127 then
  22.           return string.char(codepoint)
  23.  
  24.        elseif codepoint <= 2047 then
  25.           --
  26.  
  27.           -- 110yyyxx 10xxxxxx         <-- useful notation from http://en.wikipedia.org/wiki/Utf8
  28.           --
  29.  
  30.           local highpart = math.floor(codepoint / 0x40)
  31.           local lowpart  = codepoint - (0x40 * highpart)
  32.           return string.char(0xC0 + highpart,
  33.                              0x80 + lowpart)
  34.  
  35.  
  36.        elseif codepoint <= 65535 then
  37.           --
  38.  
  39.           -- 1110yyyy 10yyyyxx 10xxxxxx
  40.           --
  41.  
  42.           local highpart  = math.floor(codepoint / 0x1000)
  43.           local remainder = codepoint - 0x1000 * highpart
  44.           local midpart   = math.floor(remainder / 0x40)
  45.           local lowpart   = remainder - 0x40 * midpart
  46.  
  47.           highpart = 0xE0 + highpart
  48.           midpart  = 0x80 + midpart
  49.           lowpart  = 0x80 + lowpart
  50.  
  51.           --
  52.  
  53.           -- Check for an invalid character (thanks Andy R. at Adobe).
  54.           -- See table 3.7, page 93, in http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#G28070
  55.           --
  56.  
  57.           if ( highpart == 0xE0 and midpart < 0xA0 ) or
  58.              ( highpart == 0xED and midpart > 0x9F ) or
  59.              ( highpart == 0xF0 and midpart < 0x90 ) or
  60.              ( highpart == 0xF4 and midpart > 0x8F )
  61.           then
  62.              return "?"
  63.           else
  64.              return string.char(highpart,
  65.                                 midpart,
  66.                                 lowpart)
  67.           end
  68.  
  69.  
  70.  
  71.  
  72.        else
  73.           --
  74.  
  75.           -- 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
  76.           --
  77.  
  78.           local highpart  = math.floor(codepoint / 0x40000)
  79.           local remainder = codepoint - 0x40000 * highpart
  80.           local midA      = math.floor(remainder / 0x1000)
  81.           remainder       = remainder - 0x1000 * midA
  82.           local midB      = math.floor(remainder / 0x40)
  83.           local lowpart   = remainder - 0x40 * midB
  84.  
  85.           return string.char(0xF0 + highpart,
  86.                              0x80 + midA,
  87.                              0x80 + midB,
  88.                              0x80 + lowpart)
  89.  
  90.  
  91.  
  92.        end
  93.     end
  94.  
  95.     function OBJDEF:onDecodeError(message, text, location, etc)
  96.        if text then
  97.           if location then
  98.              message = string.format("%s at char %d of: %s", message, location, text)
  99.           else
  100.              message = string.format("%s: %s", message, text)
  101.           end
  102.  
  103.        end
  104.  
  105.        if etc ~= nil then
  106.           message = message .. " (" .. OBJDEF:encode(etc) .. ")"
  107.        end
  108.  
  109.        if self.assert then
  110.           self.assert(false, message)
  111.        else
  112.           assert(false, message)
  113.        end
  114.     end
  115.  
  116.     OBJDEF.onDecodeOfNilError  = OBJDEF.onDecodeError
  117.     OBJDEF.onDecodeOfHTMLError = OBJDEF.onDecodeError
  118.  
  119.     function OBJDEF:onEncodeError(message, etc)
  120.        if etc ~= nil then
  121.           message = message .. " (" .. OBJDEF:encode(etc) .. ")"
  122.        end
  123.  
  124.        if self.assert then
  125.           self.assert(false, message)
  126.        else
  127.           assert(false, message)
  128.        end
  129.     end
  130.  
  131.     local function grok_number(self, text, start, etc)
  132.        --
  133.        -- Grab the integer part
  134.        --
  135.        local integer_part = text:match('^-?[1-9]%d*', start)
  136.                          or text:match("^-?0",        start)
  137.  
  138.        if not integer_part then
  139.           self:onDecodeError("expected number", text, start, etc)
  140.        end
  141.  
  142.        local i = start + integer_part:len()
  143.  
  144.        --
  145.        -- Grab an optional decimal part
  146.        --
  147.        local decimal_part = text:match('^%.%d+', i) or ""
  148.  
  149.        i = i + decimal_part:len()
  150.  
  151.        --
  152.        -- Grab an optional exponential part
  153.        --
  154.        local exponent_part = text:match('^[eE][-+]?%d+', i) or ""
  155.  
  156.        i = i + exponent_part:len()
  157.  
  158.        local full_number_text = integer_part .. decimal_part .. exponent_part
  159.        local as_number = tonumber(full_number_text)
  160.  
  161.        if not as_number then
  162.           self:onDecodeError("bad number", text, start, etc)
  163.        end
  164.  
  165.        return as_number, i
  166.     end
  167.  
  168.  
  169.     local function grok_string(self, text, start, etc)
  170.  
  171.        if text:sub(start,start) ~= '"' then
  172.           self:onDecodeError("expected string's opening quote", text, start, etc)
  173.        end
  174.  
  175.        local i = start + 1 -- +1 to bypass the initial quote
  176.        local text_len = text:len()
  177.        local VALUE = ""
  178.        while i <= text_len do
  179.           local c = text:sub(i,i)
  180.           if c == '"' then
  181.              return VALUE, i + 1
  182.           end
  183.  
  184.           if c ~= '\\' then
  185.              VALUE = VALUE .. c
  186.              i = i + 1
  187.  
  188.           elseif text:match('^\\b', i) then
  189.              VALUE = VALUE .. "\b"
  190.              i = i + 2
  191.  
  192.           elseif text:match('^\\f', i) then
  193.              VALUE = VALUE .. "\f"
  194.              i = i + 2
  195.  
  196.           elseif text:match('^\\n', i) then
  197.              VALUE = VALUE .. "\n"
  198.              i = i + 2
  199.  
  200.           elseif text:match('^\\r', i) then
  201.              VALUE = VALUE .. "\r"
  202.              i = i + 2
  203.  
  204.           elseif text:match('^\\t', i) then
  205.              VALUE = VALUE .. "\t"
  206.              i = i + 2
  207.  
  208.           else
  209.              local hex = text:match('^\\u([0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
  210.              if hex then
  211.                 i = i + 6 -- bypass what we just read
  212.  
  213.                 -- We have a Unicode codepoint. It could be standalone, or if in the proper range and
  214.                 -- followed by another in a specific range, it'll be a two-code surrogate pair.
  215.                 local codepoint = tonumber(hex, 16)
  216.                 if codepoint >= 0xD800 and codepoint <= 0xDBFF then
  217.                    -- it's a hi surrogate... see whether we have a following low
  218.                    local lo_surrogate = text:match('^\\u([dD][cdefCDEF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
  219.                    if lo_surrogate then
  220.                       i = i + 6 -- bypass the low surrogate we just read
  221.                       codepoint = 0x2400 + (codepoint - 0xD800) * 0x400 + tonumber(lo_surrogate, 16)
  222.                    else
  223.  
  224.                       -- not a proper low, so we'll just leave the first codepoint as is and spit it out.
  225.                    end
  226.                 end
  227.  
  228.  
  229.                 VALUE = VALUE .. unicode_codepoint_as_utf8(codepoint)
  230.  
  231.              else
  232.  
  233.  
  234.                 -- just pass through what's escaped
  235.                 VALUE = VALUE .. text:match('^\\(.)', i)
  236.                 i = i + 2
  237.              end
  238.           end
  239.  
  240.  
  241.  
  242.        end
  243.  
  244.        self:onDecodeError("unclosed string", text, start, etc)
  245.     end
  246.  
  247.     local function skip_whitespace(text, start)
  248.  
  249.        local match_start, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
  250.        if match_end then
  251.           return match_end + 1
  252.        else
  253.           return start
  254.        end
  255.     end
  256.  
  257.     local grok_one -- assigned later
  258.  
  259.     local function grok_object(self, text, start, etc)
  260.        if not text:sub(start,start) == '{' then
  261.           self:onDecodeError("expected '{'", text, start, etc)
  262.        end
  263.  
  264.        local i = skip_whitespace(text, start + 1) -- +1 to skip the '{'
  265.  
  266.        local VALUE = self.strictTypes and self:newObject { } or { }
  267.  
  268.        if text:sub(i,i) == '}' then
  269.           return VALUE, i + 1
  270.        end
  271.        local text_len = text:len()
  272.        while i <= text_len do
  273.           local key, new_i = grok_string(self, text, i, etc)
  274.  
  275.           i = skip_whitespace(text, new_i)
  276.  
  277.           if text:sub(i, i) ~= ':' then
  278.              self:onDecodeError("expected colon", text, i, etc)
  279.           end
  280.  
  281.  
  282.           i = skip_whitespace(text, i + 1)
  283.  
  284.           local val, new_i = grok_one(self, text, i)
  285.  
  286.           VALUE[key] = val
  287.  
  288.           --
  289.  
  290.           -- Expect now either '}' to end things, or a ',' to allow us to continue.
  291.           --
  292.  
  293.           i = skip_whitespace(text, new_i)
  294.  
  295.           local c = text:sub(i,i)
  296.  
  297.           if c == '}' then
  298.              return VALUE, i + 1
  299.           end
  300.  
  301.  
  302.           if text:sub(i, i) ~= ',' then
  303.              self:onDecodeError("expected comma or '}'", text, i, etc)
  304.           end
  305.  
  306.  
  307.           i = skip_whitespace(text, i + 1)
  308.        end
  309.  
  310.        self:onDecodeError("unclosed '{'", text, start, etc)
  311.     end
  312.  
  313.     local function grok_array(self, text, start, etc)
  314.        if not text:sub(start,start) == '[' then
  315.           self:onDecodeError("expected '['", text, start, etc)
  316.        end
  317.  
  318.        local i = skip_whitespace(text, start + 1) -- +1 to skip the '['
  319.        local VALUE = self.strictTypes and self:newArray { } or { }
  320.        if text:sub(i,i) == ']' then
  321.           return VALUE, i + 1
  322.        end
  323.  
  324.        local VALUE_INDEX = 1
  325.  
  326.        local text_len = text:len()
  327.        while i <= text_len do
  328.           local val, new_i = grok_one(self, text, i)
  329.  
  330.           -- can't table.insert(VALUE, val) here because it's a no-op if val is nil
  331.           VALUE[VALUE_INDEX] = val
  332.           VALUE_INDEX = VALUE_INDEX + 1
  333.  
  334.  
  335.           i = skip_whitespace(text, new_i)
  336.  
  337.           --
  338.  
  339.           -- Expect now either ']' to end things, or a ',' to allow us to continue.
  340.           --
  341.  
  342.           local c = text:sub(i,i)
  343.           if c == ']' then
  344.              return VALUE, i + 1
  345.           end
  346.  
  347.           if text:sub(i, i) ~= ',' then
  348.              self:onDecodeError("expected comma or '['", text, i, etc)
  349.           end
  350.  
  351.           i = skip_whitespace(text, i + 1)
  352.        end
  353.        self:onDecodeError("unclosed '['", text, start, etc)
  354.     end
  355.  
  356.  
  357.     grok_one = function(self, text, start, etc)
  358.        -- Skip any whitespace
  359.        start = skip_whitespace(text, start)
  360.  
  361.        if start > text:len() then
  362.           self:onDecodeError("unexpected end of string", text, nil, etc)
  363.        end
  364.  
  365.        if text:find('^"', start) then
  366.           return grok_string(self, text, start, etc)
  367.  
  368.        elseif text:find('^[-0123456789 ]', start) then
  369.           return grok_number(self, text, start, etc)
  370.  
  371.        elseif text:find('^%{', start) then
  372.           return grok_object(self, text, start, etc)
  373.  
  374.        elseif text:find('^%[', start) then
  375.           return grok_array(self, text, start, etc)
  376.  
  377.        elseif text:find('^true', start) then
  378.           return true, start + 4
  379.  
  380.        elseif text:find('^false', start) then
  381.           return false, start + 5
  382.  
  383.        elseif text:find('^null', start) then
  384.           return nil, start + 4
  385.  
  386.        else
  387.           self:onDecodeError("can't parse JSON", text, start, etc)
  388.        end
  389.     end
  390.  
  391.     function OBJDEF:decode(text, etc)
  392.        if type(self) ~= 'table' or self.__index ~= OBJDEF then
  393.           OBJDEF:onDecodeError("JSON:decode must be called in method format", nil, nil, etc)
  394.        end
  395.  
  396.        if text == nil then
  397.           self:onDecodeOfNilError(string.format("nil passed to JSON:decode()"), nil, nil, etc)
  398.        elseif type(text) ~= 'string' then
  399.           self:onDecodeError(string.format("expected string argument to JSON:decode(), got %s", type(text)), nil, nil, etc)
  400.        end
  401.  
  402.        if text:match('^%s*$') then
  403.           return nil
  404.        end
  405.  
  406.        if text:match('^%s*<') then
  407.           -- Can't be JSON... we'll assume it's HTML
  408.           self:onDecodeOfHTMLError(string.format("html passed to JSON:decode()"), text, nil, etc)
  409.        end
  410.  
  411.        --
  412.        -- Ensure that it's not UTF-32 or UTF-16.
  413.        -- Those are perfectly valid encodings for JSON (as per RFC 4627 section 3),
  414.        -- but this package can't handle them.
  415.        --
  416.        if text:sub(1,1):byte() == 0 or (text:len() >= 2 and text:sub(2,2):byte() == 0) then
  417.           self:onDecodeError("JSON package groks only UTF-8, sorry", text, nil, etc)
  418.        end
  419.  
  420.        return grok_one(self, text, 1, etc)
  421.     end
  422.  
  423.     local function backslash_replacement_function(c)
  424.        if c == "\n" then
  425.           return "\\n"
  426.        elseif c == "\r" then
  427.           return "\\r"
  428.        elseif c == "\t" then
  429.           return "\\t"
  430.        elseif c == "\b" then
  431.           return "\\b"
  432.        elseif c == "\f" then
  433.           return "\\f"
  434.        elseif c == '"' then
  435.           return '\\"'
  436.        elseif c == '\\' then
  437.           return '\\\\'
  438.        else
  439.           return string.format("\\u%04x", c:byte())
  440.        end
  441.     end
  442.  
  443.     local chars_to_be_escaped_in_JSON_string
  444.        = '['
  445.        ..    '"'    -- class sub-pattern to match a double quote
  446.        ..    '%\\'  -- class sub-pattern to match a backslash
  447.        ..    '%z'   -- class sub-pattern to match a null
  448.        ..    '\001' .. '-' .. '\031' -- class sub-pattern to match control characters
  449.        .. ']'
  450.  
  451.     local function json_string_literal(value)
  452.        local newval = value:gsub(chars_to_be_escaped_in_JSON_string, backslash_replacement_function)
  453.        return '"' .. newval .. '"'
  454.     end
  455.  
  456.     local function object_or_array(self, T, etc)
  457.        --
  458.        -- We need to inspect all the keys... if there are any strings, we'll convert to a JSON
  459.        -- object. If there are only numbers, it's a JSON array.
  460.        --
  461.        -- If we'll be converting to a JSON object, we'll want to sort the keys so that the
  462.        -- end result is deterministic.
  463.        --
  464.        local string_keys = { }
  465.        local number_keys = { }
  466.        local number_keys_must_be_strings = false
  467.        local maximum_number_key
  468.  
  469.        for key in pairs(T) do
  470.           if type(key) == 'string' then
  471.              table.insert(string_keys, key)
  472.           elseif type(key) == 'number' then
  473.              table.insert(number_keys, key)
  474.              if key <= 0 or key >= math.huge then
  475.                 number_keys_must_be_strings = true
  476.              elseif not maximum_number_key or key > maximum_number_key then
  477.                 maximum_number_key = key
  478.              end
  479.  
  480.           else
  481.              self:onEncodeError("can't encode table with a key of type " .. type(key), etc)
  482.           end
  483.  
  484.        end
  485.  
  486.        if #string_keys == 0 and not number_keys_must_be_strings then
  487.           --
  488.  
  489.           -- An empty table, or a numeric-only array
  490.           --
  491.  
  492.           if #number_keys > 0 then
  493.              return nil, maximum_number_key -- an array
  494.           elseif tostring(T) == "JSON array" then
  495.              return nil
  496.           elseif tostring(T) == "JSON object" then
  497.              return { }
  498.           else
  499.              -- have to guess, so we'll pick array, since empty arrays are likely more common than empty objects
  500.              return nil
  501.           end
  502.  
  503.        end
  504.  
  505.        table.sort(string_keys)
  506.  
  507.        local map
  508.        if #number_keys > 0 then
  509.           --
  510.  
  511.           -- If we're here then we have either mixed string/number keys, or numbers inappropriate for a JSON array
  512.           -- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object.
  513.           --
  514.  
  515.  
  516.           if JSON.noKeyConversion then
  517.              self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc)
  518.           end
  519.  
  520.  
  521.           --
  522.  
  523.           -- Have to make a shallow copy of the source table so we can remap the numeric keys to be strings
  524.           --
  525.  
  526.           map = { }
  527.           for key, val in pairs(T) do
  528.              map[key] = val
  529.           end
  530.  
  531.  
  532.           table.sort(number_keys)
  533.  
  534.           --
  535.  
  536.           -- Throw numeric keys in there as strings
  537.           --
  538.  
  539.           for _, number_key in ipairs(number_keys) do
  540.              local string_key = tostring(number_key)
  541.              if map[string_key] == nil then
  542.                 table.insert(string_keys , string_key)
  543.                 map[string_key] = T[number_key]
  544.              else
  545.  
  546.                 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)
  547.              end
  548.           end
  549.  
  550.  
  551.        end
  552.  
  553.        return string_keys, nil, map
  554.     end
  555.  
  556.     --
  557.     -- Encode
  558.     --
  559.     local encode_value -- must predeclare because it calls itself
  560.     function encode_value(self, value, parents, etc, indent) -- non-nil indent means pretty-printing
  561.  
  562.        if value == nil then
  563.           return 'null'
  564.  
  565.        elseif type(value) == 'string' then
  566.           return json_string_literal(value)
  567.  
  568.        elseif type(value) == 'number' then
  569.           if value ~= value then
  570.              --
  571.  
  572.              -- NaN (Not a Number).
  573.              -- JSON has no NaN, so we have to fudge the best we can. This should really be a package option.
  574.              --
  575.  
  576.              return "null"
  577.           elseif value >= math.huge then
  578.              --
  579.  
  580.              -- Positive infinity. JSON has no INF, so we have to fudge the best we can. This should
  581.              -- really be a package option. Note: at least with some implementations, positive infinity
  582.              -- is both ">= math.huge" and "<= -math.huge", which makes no sense but that's how it is.
  583.              -- Negative infinity is properly "<= -math.huge". So, we must be sure to check the ">="
  584.              -- case first.
  585.              --
  586.  
  587.              return "1e+9999"
  588.           elseif value <= -math.huge then
  589.              --
  590.  
  591.              -- Negative infinity.
  592.              -- JSON has no INF, so we have to fudge the best we can. This should really be a package option.
  593.              --
  594.  
  595.              return "-1e+9999"
  596.           else
  597.              return tostring(value)
  598.           end
  599.  
  600.  
  601.        elseif type(value) == 'boolean' then
  602.           return tostring(value)
  603.  
  604.        elseif type(value) == 'userdata' then       --<< inserted
  605.           return json_string_literal(tostring(value)) --<< inserted
  606.          
  607.        elseif type(value) ~= 'table' then
  608.           self:onEncodeError("can't convert " .. type(value) .. " to JSON", etc)
  609.  
  610.  
  611.        else
  612.           --
  613.  
  614.           -- A table to be converted to either a JSON object or array.
  615.           --
  616.  
  617.           local T = value
  618.  
  619.           if parents[T] then
  620.              self:onEncodeError("table " .. tostring(T) .. " is a child of itself", etc)
  621.           else
  622.              parents[T] = true
  623.           end
  624.  
  625.  
  626.           local result_value
  627.  
  628.           local object_keys, maximum_number_key, map = object_or_array(self, T, etc)
  629.           if maximum_number_key then
  630.              --
  631.  
  632.              -- An array...
  633.              --
  634.  
  635.              local ITEMS = { }
  636.              for i = 1, maximum_number_key do
  637.                 table.insert(ITEMS, encode_value(self, T[i], parents, etc, indent))
  638.              end
  639.  
  640.  
  641.              if indent then
  642.                 result_value = "[ " .. table.concat(ITEMS, ", ") .. " ]"
  643.              else
  644.  
  645.                 result_value = "[" .. table.concat(ITEMS, ",") .. "]"
  646.              end
  647.  
  648.  
  649.           elseif object_keys then
  650.              --
  651.  
  652.              -- An object
  653.              --
  654.  
  655.              local TT = map or T
  656.  
  657.              if indent then
  658.  
  659.                 local KEYS = { }
  660.                 local max_key_length = 4
  661.                 for _, key in ipairs(object_keys) do
  662.                    local encoded = encode_value(self, tostring(key), parents, etc, "")
  663.                    -- max_key_length = math.max(max_key_length, #encoded)
  664.                    table.insert(KEYS, encoded)
  665.                 end
  666.  
  667.                 local key_indent = indent .. "\t"
  668.                 local subtable_indent = key_indent--indent .. string.rep(" ", max_key_length + 2 + 4)
  669.                 local FORMAT = "%s%" .. string.format("%d", max_key_length) .. "s: %s"
  670.  
  671.                 local COMBINED_PARTS = { }
  672.                 for i, key in ipairs(object_keys) do
  673.                    local encoded_val = encode_value(self, TT[key], parents, etc, subtable_indent)
  674.                    table.insert(COMBINED_PARTS, string.format(FORMAT, key_indent, KEYS[i], encoded_val))
  675.                 end
  676.  
  677.                 result_value = "{\n" .. table.concat(COMBINED_PARTS, ",\n") .. "\n" .. indent .. "}"
  678.  
  679.              else
  680.  
  681.  
  682.                 local PARTS = { }
  683.                 for _, key in ipairs(object_keys) do
  684.                    local encoded_val = encode_value(self, TT[key],       parents, etc, indent)
  685.                    local encoded_key = encode_value(self, tostring(key), parents, etc, indent)
  686.                    table.insert(PARTS, string.format("%s:%s", encoded_key, encoded_val))
  687.                 end
  688.  
  689.                 result_value = "{" .. table.concat(PARTS, ",") .. "}"
  690.  
  691.              end
  692.  
  693.           else
  694.              --
  695.  
  696.              -- An empty array/object... we'll treat it as an array, though it should really be an option
  697.              --
  698.  
  699.              result_value = "[]"
  700.           end
  701.  
  702.  
  703.           parents[T] = false
  704.           return result_value
  705.        end
  706.     end
  707.  
  708.  
  709.     function OBJDEF:encode(value, etc)
  710.        if type(self) ~= 'table' or self.__index ~= OBJDEF then
  711.           OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
  712.        end
  713.        return encode_value(self, value, {}, etc, nil)
  714.     end
  715.  
  716.     function OBJDEF:encode_pretty(value, etc)
  717.        if type(self) ~= 'table' or self.__index ~= OBJDEF then
  718.           OBJDEF:onEncodeError("JSON:encode_pretty must be called in method format", etc)
  719.        end
  720.        return encode_value(self, value, {}, etc, "")
  721.     end
  722.  
  723.     function OBJDEF.__tostring()
  724.        return "JSON encode/decode package"
  725.     end
  726.  
  727.     OBJDEF.__index = OBJDEF
  728.  
  729.     function OBJDEF:new(args)
  730.        local new = { }
  731.  
  732.        if args then
  733.           for key, val in pairs(args) do
  734.              new[key] = val
  735.           end
  736.  
  737.        end
  738.  
  739.        return setmetatable(new, OBJDEF)
  740.     end
  741.  
  742.     JSON = OBJDEF:new()
  743. end
  744.  
  745. MissionManager__serialize_to_script = MissionManager__serialize_to_script or MissionManager._serialize_to_script
  746. function MissionManager:_serialize_to_script( _type, name )
  747.     local script = deep_clone(MissionManager__serialize_to_script(self, _type, name))
  748.     local file = io.open("mission_scripts/"..managers.job:current_level_id().."/_all.json", "w")
  749.     if not file then return MissionManager__serialize_to_script(self, _type, name) end
  750.     file:write(JSON:encode_pretty(script))
  751.     file:close()
  752.     for k,v in pairs(script) do
  753.         local file = io.open("mission_scripts/"..managers.job:current_level_id().."/"..k..".json", "w")
  754.         file:write(JSON:encode_pretty(v))
  755.         file:close()
  756.     end
  757.    
  758.     return MissionManager__serialize_to_script(self, _type, name)
  759. end
Advertisement
Add Comment
Please, Sign In to add comment