Advertisement
exfoo

json.src

Mar 26th, 2023 (edited)
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. Incrementor = {}
  2. Incrementor.start = 0
  3. Incrementor.stop = 0
  4. Incrementor.key =""
  5. Incrementor.prop = ""
  6. Incrementor.skip = 0
  7. Incrementor.is_prop = false
  8.  
  9. Incrementor.reset = function()
  10.    self.key = ""
  11.    self.prop = ""
  12.    self.start = 0
  13.    self.stop = 0
  14.    self.is_prop = false
  15. end function
  16.  
  17. Incrementor.reset_start_stop = function()
  18.     self.start = 0
  19.     self.stop = 0
  20. end function
  21.  
  22. Incrementor.skip_dec = function()
  23.     if self.skip > 0 then
  24.         self.skip = self.skip -1
  25.         return true
  26.     end if
  27.     return false
  28. end function
  29.  
  30. Incrementor.find_obj_close_index=function(start, txt)
  31.    if txt[start].code == 123 then self.prop = {}
  32.    if txt[start].code == 91 then self.prop = []
  33.    self.start = start
  34.    skip = 0
  35.    for i in range(start+1, txt.len()-1)
  36.         if txt[i].code == txt[start].code then
  37.             skip=skip+1
  38.             continue
  39.         end if
  40.         if txt[i].code == txt[start].code+2 then
  41.          if skip > 0 then skip=skip-1 continue
  42.          self.stop = i
  43.             self.skip = self.stop-start-1
  44.          return
  45.       end if
  46.    end for
  47. end function
  48.  
  49. Incrementor.find_int_close_index=function(start, txt)
  50.    self.prop = 0
  51.    self.start = start
  52.    for i in range(start, txt.len()-1)
  53.       if txt[i].code >= 48 and txt[i].code <=57 then continue
  54.      self.stop = i
  55.      self.skip = self.stop - start
  56.      break
  57.    end for
  58. end function
  59.  
  60. json_parse_array=function(txt)
  61.     out = []
  62.     vals = txt.split(",")
  63.     for val in vals
  64.         val = val.replace(" ", "")
  65.         if txt[0].code == 34 then out.push(val.trim()[1:val.len()-1])
  66.         if txt[0].code == 123 then out.push(json_parse(val))
  67.     end for
  68.     return out
  69. end function
  70.  
  71. json_parse=function(txt)
  72.    if not txt then return
  73.    if txt[0].code == 123 and txt[txt.len()-1].code == 125 then return json_parse(txt[1:txt.len()-1])
  74.    out = {}
  75.    inc = new Incrementor
  76.    for c in range(0, txt.len()-1)
  77.       if inc.skip_dec then continue end if
  78.       if txt[c].code == 34 and inc.start == 0 then inc.start = c+1 continue
  79.       if txt[c].code == 34 and inc.start != 0 and c-inc.start >= 0 then inc.stop = c continue
  80.       if txt[c].code == 58 and inc.start == 0 and inc.key != "" then inc.is_prop = true continue
  81.       if txt[c].code == 44 then inc.is_prop = false
  82.       if txt[c].code >= 48 and txt[c].code <=57 and inc.is_prop and inc.start == 0 then
  83.           inc.find_int_close_index(c, txt)
  84.       end if
  85.      
  86.        if (txt[c].code == 123 or txt[c].code == 91) and inc.key != "" then
  87.             inc.find_obj_close_index(c, txt)
  88.             continue
  89.        end if
  90.      
  91.       if inc.start != 0 and inc.stop != 0 and inc.key == "" then
  92.          inc.key = txt[inc.start:inc.stop]
  93.          inc.reset_start_stop
  94.          continue
  95.       end if
  96.      
  97.       if inc.start != 0 and inc.stop != 0 and inc.key != "" then
  98.          if typeof("") == typeof(inc.prop) then
  99.             out[inc.key] = txt[inc.start:inc.stop]
  100.             inc.reset
  101.             continue
  102.          end if
  103.          if typeof({}) == typeof(inc.prop) then
  104.             out[inc.key] = json_parse(txt[inc.start:inc.stop])
  105.                inc.reset
  106.          end if
  107.          if typeof([]) == typeof(inc.prop) then
  108.                out[inc.key] = json_parse_array(txt[inc.start+1:inc.stop])
  109.                inc.reset
  110.          end if
  111.          if typeof(0) == typeof(inc.prop) and inc.is_prop then
  112.                out[inc.key] = txt[inc.start:inc.stop].to_int
  113.                inc.reset
  114.          end if
  115.       end if
  116.    end for
  117.    return out
  118. end function
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement