A lightweight JSON library for Lua
 
 
Go to file
alexandro-rezakhani 83b301510b
handle encoding of number based key properties
i was unable to encode a table due to some number based key properties.

i modified the encode function to check for number based keys in addition to the already present string based keys.

an error will still be returned if the key is neither string nor number based.

it works in my cases


Please see this link:
https://github.com/rxi/json.lua/pull/43#issue-1446161604
2022-11-20 01:21:55 -05:00
.github Create FUNDING.yml 2020-06-18 17:03:53 +01:00
bench Fixed indentation in benchmark util script 2015-08-14 20:00:48 +01:00
test Additional string tests 2020-04-05 12:04:05 +01:00
LICENSE Updated copyright year: 2019 => 2020 2020-04-05 12:06:41 +01:00
README.md Update info about supporting Lua 5.4 version 2020-12-30 21:04:13 +01:00
json.lua handle encoding of number based key properties 2022-11-20 01:21:55 -05:00

README.md

json.lua

A lightweight JSON library for Lua

Features

  • Implemented in pure Lua: works with 5.1, 5.2, 5.3, 5.4 and JIT
  • Fast: generally outperforms other pure Lua JSON implementations (benchmark scripts)
  • Tiny: around 280sloc, 9kb
  • Proper error messages, eg: expected '}' or ',' at line 203 col 30

Usage

The json.lua file should be dropped into an existing project and required by it:

json = require "json"

The library provides the following functions:

json.encode(value)

Returns a string representing value encoded in JSON.

json.encode({ 1, 2, 3, { x = 10 } }) -- Returns '[1,2,3,{"x":10}]'

json.decode(str)

Returns a value representing the decoded JSON string.

json.decode('[1,2,3,{"x":10}]') -- Returns { 1, 2, 3, { x = 10 } }

Notes

  • Trying to encode values which are unrepresentable in JSON will never result in type conversion or other magic: sparse arrays, tables with mixed key types or invalid numbers (NaN, -inf, inf) will raise an error
  • null values contained within an array or object are converted to nil and are therefore lost upon decoding
  • Pretty encoding is not supported, json.encode() only encodes to a compact format

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.