lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


dkjson is a module for encoding and decoding JSON data. It supports UTF-8.

JSON (JavaScript Object Notation) is a format for serializing data based
on the syntax for JavaScript data structures.

dkjson is written in Lua without any dependencies, but
when LPeg is available dkjson uses it to speed up decoding.

<http://dkolf.de/src/dkjson-lua.fsl/>

Changes since version 2.4:

  *  Added customizable exception handling for encoding.
  *  Decode input that contains JavaScript comments.


The customizable exception handling lets you specify a different
behaviour for values where the encoder would otherwise raise an error.
Provided with the library is a default exception handler that encodes
the error message as a string (to simplify debugging):

  print(
    json.encode(
      { fn = math.log },
      { exception = json.encodeexception }
    )
  )

--> {"fn":"<type 'function' is not supported by JSON.>"}

Or you could encode functions using a lookup table:

  fntbl = {
    [math.log] = '"#fn:log"',
    [math.sin] = '"#fn:sin"'
  }
  print(
    json.encode(
      { fn = math.log },
      {
         exception = function (reason, value, state, defaultmessage)
           return fntbl[value]
         end
      }
    )
  )

--> {"fn":"#fn:log"}


Decoding input with JavaScript comments is a convenience feature for
users of your application when they want to disable lines in
configuration files. I discourage using comments in most other
situations; a file with comments is not a valid JSON file anymore.

Best regards,

David Kolf