Commit graph

10 commits

Author SHA1 Message Date
Jo-Philipp Wich
efcd487978 luci-lib-jsonc: improve handling of integral numeric values
Properly deal with integral numeric values exceeding the int32_t range
by replacing the cast logic with more fine grained checks:

 - Lua numbers which are integers in the first place are directly converted
   to JSON integers

 - Finite double Lua numbers which are integral are converted to JSON
   integer values

 - All other numeric values are converted to JSON doubles

This should bring the handling of large integral value in line with the
documented behavior of turning non-fractional Lua numbers into JSON
integers.

Fixes: #6647
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-10-25 22:42:23 +02:00
Justin Klaassen
45f411b7a2 luci-lib-jsonc: stringify int use json_object_new_int64 instead
Fixes: #3293
Signed-off-by: Justin Klaassen <jck112@users.noreply.github.com>
2022-03-20 08:11:33 -07:00
Jo-Philipp Wich
8da8f38bf9 luci-lib-jsonc: store large JSON integer values as Lua doubles
Fixes: #3293
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2019-11-13 09:11:09 +01:00
Jo-Philipp Wich
21db93576a luci-lib-jsonc: prevent infinite recursion in stringify
Also increase the stack size as needed to prevent crashes when serializing
deeply nested tables.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2018-12-21 15:41:19 +01:00
Jan-Philipp Litza
5335f22537 luci-lib-jsonc: Add ltn12-compatible sink factory
To use the luci-lib-jsonc parser as sink for an ltn12 pump (for example
from stdin), the following code will now do:

    require 'luci.ltn12'
    require 'luci.jsonc'

    local parser = luci.jsonc.new()
    luci.ltn12.pump.all(luci.ltn12.source.file(io.input()), parser:sink())
    print(parser:get())

Signed-off-by: Jan-Philipp Litza <janphilipp@litza.de>
2015-09-04 12:21:30 +02:00
Jan-Philipp Litza
90c841d66e luci-lib-jsonc: Fix memory leak in stringify() 2015-08-31 19:53:51 +02:00
Jan-Philipp Litza
e7645d557c luci-lib-jsonc: allow encoding empty lists
To be consistent with the behavior of luci-lib-json, an empty Lua table
should be encoded to an empty JSON list, not an empty JSON object.

To still allow encoding empty JSON objects, the usage of anything other
than a number or a string as a key (for example an empty table or a
function) can be used to force encoding as an object:

    json.stringify({})                  -- "[]"
    json.stringify({[{}] = true})       -- "{}"

Signed-off-by: Jan-Philipp Litza <janphilipp@litza.de>
2015-08-30 15:52:33 +02:00
Jan-Philipp Litza
e32a877aa4 luci-lib-jsonc: Ignore non-string-or-number keys in tables
Previously, the following caused a segmentation fault:

    json.stringify({[{}] = true})

This was caused by lua_tostring() returning NULL for anything but
strings and numbers, letting json_object_object_add crash.

This patch makes jsonc ignore all keys which have no string
representation altogether.

Signed-off-by: Jan-Philipp Litza <janphilipp@litza.de>
2015-08-30 15:51:17 +02:00
Jo-Philipp Wich
cf7e2695cc luci-lib-jsonc: add api documentation
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2015-01-28 22:31:28 +01:00
Jo-Philipp Wich
73b19f6382 libs: add luci-lib-jsonc, a Lua binding for JSON-C
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2015-01-26 16:49:23 +01:00