mpx / lua-cjson Goto Github PK
View Code? Open in Web Editor NEWLua CJSON is a fast JSON encoding/parsing module for Lua
Home Page: https://kyne.au/~mark/software/lua-cjson.php
License: MIT License
Lua CJSON is a fast JSON encoding/parsing module for Lua
Home Page: https://kyne.au/~mark/software/lua-cjson.php
License: MIT License
Since all numbers are represented as double, larger numbers (like longs) get truncated. For example, the output of this code...
cjson.encode(cjson.decode("9007199254740992"))```
...is not the same as the input. This is needed since I need to sometimes parse the JSON output of APIs that I do not control and that use numbers that do not fit into doubles.
A proposed fix is in this pull request:
https://github.com/mpx/lua-cjson/pull/36
exp:
local t = {"ban/ana"}
local res = cjson.encode(t)
print(res)
the result is {"ban\/ana"}
But others json lib will not do this, exp jsoncpp.
If i have to do this, is there any way to avoid it?
I have a config file whose content is organized in JSON, when I read the file from disk and assign its content to a Lua string and print shows exactly the same as the file.But when I try to decode it use cjson the console shows "Expected value but found invalid token at character 1".
Here is my file:
{"apkname":"lukaYingyongbao_215_jiagu_sign.apk","verName":"2.1.5","verCode":20,"verForced":"false","appname":"撸卡漫画","title":"撸卡漫画更新啦","verInfo":"《撸卡漫画》新版本上线啦!\n主要修复版本部分机型的登录问题以及性能优化"}
Don't believe it but finally, Visual Studio 14 defines snprintf()!
It restricts redefinition, this is added in stdio.h:
#ifdef snprintf
#error: Macro definition of snprintf conflicts with Standard Library function declaration”
#endif
so we get
...\ucrt\stdio.h(1927): fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration [...\lua-cjson\build\cjson.vcxproj
errors.
My solution to this was skipping of add_definitions(-Dstrncasecmp=_strnicmp)
for MSVC14. Patch follows:
CMakeLists.txt | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ca26381..ff74d6f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,8 +71,10 @@ endif()
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-Dinline=__inline)
- add_definitions(-Dsnprintf=_snprintf)
- add_definitions(-Dstrncasecmp=_strnicmp)
+ if(NOT MSVC14)
+ add_definitions(-Dsnprintf=_snprintf)
+ endif(NOT MSVC14)
+ add_definitions(-Dstrncasecmp=_strnicmp)
endif()
add_library(cjson MODULE lua_cjson.c strbuf.c ${FPCONV_SOURCES})
Keep on coding, Jedzia;)
I write a test:
emptyValue = {}
emptyValueText = cjson.encode(emptyValue)
print("empty value text is: " .. emptyValueText)
emptyValueNew = cjson.decode(emptyValueText)
print("type(emptyValueNew) is " .. type(emptyValueNew))
emptyValueNew.newMember = "emptyValueNew string"
print("emptyValueNew.newMember = " .. emptyValueNew.newMember)
Program crashed after print: "type(emptyValueNew) is table"
Doing:
cjson.encode({url = "https://google.com})
prints {"url":"https:\/\/google.com"}
. I'm trying to stringify a JSON object here, and don't expect to get the /
escaped. Is there any solution?
for the latest code, I found the maximum value of encode_number_precision is 16, which is merged in commit.
openresty@f79aa68
For our scenes, we use int64 value in json strings, which means 16 precission numbers is not enough for us.
So any suggestions?
Can we change max value of encode_number_precision to 19, 20 or add new configuration like cjson.max_encode_number_precision(X) for different scenes?
Thanks.
I am trying to package lua-cjson for openSUSE and it is not easy.
Provided ./runtest.sh
is very inflexible, so in order to make it work, I had to patch it, and even then I was not able to make cmake-type build working, because it has hardcoded Lua 5.1 version in it (we build for multiple Lua versions, and it would be awesome, if we could test with them; it would be good even for you, so that you may be sure about the extent of your Lua coverage).
Hi there!
First of all thank you for a library!
Here is some code I can't explain to myself:
00:59 ar@alexroman-XPS8700[~]$ cat cjson.lua
#!/usr/bin/lua
local cjson = require "cjson"
local badjson = "Just a string"
print(cjson.decode(badjson))
00:59 ar@alexroman-XPS8700[~]$ ./cjson.lua
/usr/bin/lua: ./cjson.lua:6: Expected value but found invalid token at character 1
stack traceback:
[C]: in function 'decode'
./lua-cjson-bug.lua:6: in main chunk
[C]: ?
00:59 ar@alexroman-XPS8700[~]$
Some my clients send wrong JSON and it cause crash of script.
If it's a correct behavior I'll be happy to know how can I handle this error?
lua-cjson 2.1.0-1
using termux pkg luarocks on armv7a android
u0_a185@localhost ~/luatest> lua -e "require 'cjson'"
lua: error loading module 'cjson' from file
'/data/data/com.termux/files/usr/lib/lua/5.3/cjson.so':
dlopen failed: cannot locate symbol "floor" referenced by "/data/data/com.termux/files/usr/lib/lua/5.3/cjson.so"... stack traceback:
[C]: in ?
[C]: in function 'require'
(command line):1: in main chunk
[C]: in ?
https://travis-ci.org/hughperkins/clnn/builds/107115627#L497-L503
In file included from lua_cjson.c:47:
./fpconv.h:15:20: warning: inline function 'fpconv_init' is not defined [-Wundefined-inline]
extern inline void fpconv_init();
^
lua_cjson.c:1367:5: note: used here
fpconv_init();
^
1 warning generated.
Currently cjson uses global library settings:
cjson.decode_invalid_numbers([setting])
cjson.encode_invalid_numbers([setting])
cjson.encode_keep_buffer([keep])
cjson.encode_max_depth([depth])
cjson.decode_max_depth([depth])
cjson.encode_sparse_array([convert[, ratio[, safe]]])
This interface presents the problem of storing some state on the library. This is not ideal, since the output of cjson.encode
and cjson.decode
, when called with the same parameters, will change depending on those global settings. That is undesirable in lots of scenarios.
I propose an interface that, while allowing the same functionality, does not store any state on the library itself, and makes cjson.encode
and cjson.decode
deterministic: Add a (non-mandatory) options
parameter to both.
cjson.encode(value, options)
cjson.decode(str, options)
For example:
cjson.decode("{'a' = 1}", {invalid_numbers = true})
cjson.encode({a = 1}, {depth = 1})
I would provide a pull request for this myself, but again I lack the necessary C skills to do so.
Please consider this as an enhancement request. Since this would be an interface-breaking change, I would suggest including it on a 3.x version.
I'm currently using lua5.3.But this is what I get from the logs.
lua entry thread aborted: runtime error: error loading module 'cjson' from file '/usr/local/lib/lua/5.3/cjson.so':
/usr/local/lib/lua/5.3/cjson.so: undefined symbol: luaL_setfuncs
Any idea on how to fix this
I am running:
local cjson = require("cjson")
and in response I am getting this:
Error: error loading module 'cjson' from file 'C:\Program Files (x86)\Lua\5.1\systree\lib\lua\5.1\cjson.dll':
%1 is not a valid Win32 application.
stack traceback:
[string "boot.lua"]:637: in function <[string "boot.lua"]:633>
[C]: at 0x7fffaa6d6130
[C]: in function 'require'
main.lua:2: in main chunk
[C]: in function 'require'
[string "boot.lua"]:475: in function <[string "boot.lua"]:311>
[C]: in function 'xpcall'
[string "boot.lua"]:645: in function <[string "boot.lua"]:639>
[C]: in function 'xpcall
any ideas how to fix?
UPDATE:
I just found out that cjson runs fine with just lua. I had been running it with love2d. Still looking into why I'm getting this error
The problem occurs when I required the 'cjson'.
Environment: Mac OS X 10.11.6, lua 5.3
error loading module 'cjson' from file '/usr/local/lib/lua/5.3/cjson.so':
dlopen(/usr/local/lib/lua/5.3/cjson.so, 6): Symbol not found: _lua_insert
Referenced from: /usr/local/lib/lua/5.3/cjson.so
Expected in: flat namespace
in /usr/local/lib/lua/5.3/cjson.so
What's wrong with it?
i m trying to import the requests library but i got this error
error loading module 'cjson.safe' from file '/usr/local/lib/lua/5.2/cjson.so':
/usr/local/lib/lua/5.2/cjson.so: undefined symbol: lua_objlen
any help ?
I installed tarantool 1.8 on Alpine Linux 3.7 and now I am trying a few extra rocks.
When issuing "tarantoolctl rocks install luajwt" at the OS prompt, I see the errors below. Any idea why this might be?
Missing dependencies for luajwt 1.3-4:
luacrypto >= 0.3.2-1 (not installed)
lua-cjson >= 2.1.0 (not installed)
lbase64 >= 20120807-3 (not installed)
luajwt 1.3-4 depends on luacrypto >= 0.3.2-1 (not installed)
Installing http://rocks.moonscript.org/luacrypto-0.3.2-2.src.rock
gcc -O2 -fPIC -I/usr/local/include/tarantool -c src/lcrypto.c -o src/lcrypto.o -I/usr/include
gcc -shared -o crypto.so -L/usr/local/lib src/lcrypto.o -L/usr/lib -Wl,-rpath,/usr/lib: -lcrypto
luacrypto 0.3.2-2 is now installed in /root/.rocks (license: MIT)
luajwt 1.3-4 depends on lua-cjson >= 2.1.0 (not installed)
Installing http://rocks.moonscript.org/lua-cjson-2.1.0-1.src.rock
gcc -O2 -fPIC -I/usr/local/include/tarantool -c lua_cjson.c -o lua_cjson.o
lua_cjson.c:1298:13: error: static declaration of 'luaL_setfuncs' follows non-static declaration
static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
^~~~~~~~~~~~~
In file included from lua_cjson.c:44:0:
/usr/local/include/tarantool/lauxlib.h:88:18: note: previous declaration of 'luaL_setfuncs' was here
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
^~~~~~~~~~~~~
In file included from lua_cjson.c:47:0:
fpconv.h:15:20: warning: inline function 'fpconv_init' declared but never defined
extern inline void fpconv_init();
^~~~~~~~~~~
Error: Failed installing dependency: http://rocks.moonscript.org/lua-cjson-2.1.0-1.src.rock - Build error: Failed compiling object lua_cjson.o
Hi,
Since this is not addressed in documentation I thought I would ask here - what's the "right" way of handling invalid JSONs without blowing up?
Hi, today i spent 6 hours to compile cjson in windows 11, so is any compiled package anywhere?
because "luarocks make" not working right out of box
lua_cjson.obj : error LNK2019.
cjson.dll : fatal error LNK1120
i understand its all can be solved but, but it cant take to much time.
So if author just wants non C/C++ people suffered so its good, i understand author, and i will pass this lib.
And if author want help to users, so good windows compile manual or precompiled file will be veyr fine aid, thanks fo feading.
PS; I undertand that linux is cool and i prefer linux for development but sometime windows platform cant be avoided....
local cjson = require "cjson"
while true do
local t = {"a", "b", "c"}
local s = cjson.encode(t)
local value = cjson.decode(s) --Crash every time
end
This issue is related to #6, in which a global switch is suggested in order to force empty objects to "always be arrays" or "always be objects".
I would argue that while that will solve some specific issues, it will not resolve the more general one, which is differentiating between empty arrays and empty objects in general. Consider the following string:
local json_str = '{"items":[],"properties":{}}'
Ideally cjson should be able to decode that string to a lua object, that when encoded back to json returns the same string.
assert(cjson.encode(cjson.decode(json_str))
This means that cjson needs a way to "mark" tables that are arrays. This could be used by the decoder itself, but would be useful if exposed, so people could give cjson "hints" about what must be an array, even when empty.
I'm writing a possible Lua implementation here. In my case I'm using metatables, but maybe someone with more C knowledge can think of other ideas.
function cjson.mark_as_array = function(t)
local mt = getmetatable(t) or {}
mt.__is_cjson_array = true
return setmetatable(t, mt)
end
function cjson.is_marked_as_array(t)
local mt = getmetatable(t)
return mt and mt.__is_cjson_array
end
cjson.encode
should mark all arrays it finds using the cjson.mark_as_array
.
On the other hand, when cjson.decode
encountered an empty table, it should use cjson.is_marked_as_array
to decide whether to return "{}"
or "[]"
. Notice that this would only apply to empty tables. A lua table like {1,2, a = "foo"}
should be encoded as an object, even when marked as an array.
I would gladly provide a pull request for this, but I don't do C. Please consider this a feature request.
I tried using both x86 and x64 for compilation
Using https://rocks.moonscript.org/lua-cjson-2.1.0-1.src.rock... switching to 'build' mode
cl /MD /O2 -c -Folua_cjson.obj -IC:/lua/5.1/include/ lua_cjson.c -DDISABLE_INVALID_NUMBERS
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
lua_cjson.c
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(78) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(79) : error C2085: 'strbuf_reset' : not in formal parameter list
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(79) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(83) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(84) : error C2085: 'strbuf_allocated' : not in formal parameter list
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(84) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(90) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(91) : error C2082: redefinition of formal parameter 'strbuf_empty_length'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(91) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(95) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(96) : error C2082: redefinition of formal parameter 'strbuf_ensure_empty_length'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(96) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(101) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(102) : error C2082: redefinition of formal parameter 'strbuf_empty_ptr'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(102) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(106) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(107) : error C2082: redefinition of formal parameter 'strbuf_extend_length'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(107) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(111) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(112) : error C2082: redefinition of formal parameter 'strbuf_length'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(112) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(116) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(117) : error C2082: redefinition of formal parameter 'strbuf_append_char'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(117) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(122) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(123) : error C2085: 'strbuf_append_char_unsafe' : not in formal parameter list
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(123) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(127) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(128) : error C2082: redefinition of formal parameter 'strbuf_append_mem'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(128) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(134) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(135) : error C2085: 'strbuf_append_mem_unsafe' : not in formal parameter list
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(135) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(140) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(141) : error C2082: redefinition of formal parameter 'strbuf_ensure_null'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(141) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(145) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(146) : error C2082: redefinition of formal parameter 'strbuf_string'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\strbuf.h(146) : error C2143: syntax error : missing ';' before '{'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\fpconv.h(15) : error C2054: expected '(' to follow 'inline'
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\fpconv.h(15) : error C2085: 'fpconv_init' : not in formal parameter list
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\fpconv.h(18) : error C2085: 'fpconv_g_fmt' : not in formal parameter list
c:\users\greg waxman\appdata\local\temp\luarocks_lua-cjson-2.1.0-1-4204\lua-cjson-2.1.0\fpconv.h(19) : error C2085: 'fpconv_strtod' : not in formal parameter list
lua_cjson.c(92) : error C2085: 'json_token_type_t' : not in formal parameter list
lua_cjson.c(94) : error C2085: 'json_token_type_name' : not in formal parameter list
lua_cjson.c(94) : error C2143: syntax error : missing ';' before '='
lua_cjson.c(113) : error C2016: C requires that a struct or union has at least one member
lua_cjson.c(113) : error C2061: syntax error : identifier 'json_token_type_t'
lua_cjson.c(130) : error C2059: syntax error : '}'
lua_cjson.c(136) : error C2061: syntax error : identifier 'json_config_t'
lua_cjson.c(138) : error C2059: syntax error : '}'
lua_cjson.c(141) : error C2016: C requires that a struct or union has at least one member
lua_cjson.c(141) : error C2061: syntax error : identifier 'json_token_type_t'
lua_cjson.c(149) : error C2059: syntax error : '}'
lua_cjson.c(192) : error C2143: syntax error : missing '{' before '*'
lua_cjson.c(194) : error C2065: 'json_config_t' : undeclared identifier
lua_cjson.c(194) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(194) : warning C4552: '*' : operator has no effect; expected operator with side-effect
lua_cjson.c(196) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(196) : warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
lua_cjson.c(197) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(200) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(200) : warning C4047: 'return' : 'int *' differs in levels of indirection from 'int'
lua_cjson.c(206) : error C2143: syntax error : missing '{' before '*'
lua_cjson.c(268) : error C2065: 'json_config_t' : undeclared identifier
lua_cjson.c(268) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(268) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
lua_cjson.c(268) : error C2106: '=' : left operand must be l-value
lua_cjson.c(270) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(270) : error C2223: left of '->encode_sparse_convert' must point to struct/union
lua_cjson.c(270) : warning C4047: 'function' : 'const char **' differs in levels of indirection from 'int'
lua_cjson.c(270) : warning C4024: 'json_enum_option' : different types for formal and actual parameter 4
lua_cjson.c(270) : error C2198: 'json_enum_option' : too few arguments for call
lua_cjson.c(271) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(271) : error C2223: left of '->encode_sparse_ratio' must point to struct/union
lua_cjson.c(271) : error C2198: 'json_integer_option' : too few arguments for call
lua_cjson.c(272) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(272) : error C2223: left of '->encode_sparse_safe' must point to struct/union
lua_cjson.c(272) : error C2198: 'json_integer_option' : too few arguments for call
lua_cjson.c(281) : error C2065: 'json_config_t' : undeclared identifier
lua_cjson.c(281) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(281) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
lua_cjson.c(281) : error C2106: '=' : left operand must be l-value
lua_cjson.c(283) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(283) : error C2223: left of '->encode_max_depth' must point to struct/union
lua_cjson.c(283) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
lua_cjson.c(283) : warning C4024: 'json_integer_option' : different types for formal and actual parameter 3
lua_cjson.c(283) : error C2198: 'json_integer_option' : too few arguments for call
lua_cjson.c(290) : error C2065: 'json_config_t' : undeclared identifier
lua_cjson.c(290) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(290) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
lua_cjson.c(290) : error C2106: '=' : left operand must be l-value
lua_cjson.c(292) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(292) : error C2223: left of '->decode_max_depth' must point to struct/union
lua_cjson.c(292) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
lua_cjson.c(292) : warning C4024: 'json_integer_option' : different types for formal and actual parameter 3
lua_cjson.c(292) : error C2198: 'json_integer_option' : too few arguments for call
lua_cjson.c(298) : error C2065: 'json_config_t' : undeclared identifier
lua_cjson.c(298) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(298) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
lua_cjson.c(298) : error C2106: '=' : left operand must be l-value
lua_cjson.c(300) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(300) : error C2223: left of '->encode_number_precision' must point to struct/union
lua_cjson.c(300) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'
lua_cjson.c(300) : warning C4024: 'json_integer_option' : different types for formal and actual parameter 3
lua_cjson.c(300) : error C2198: 'json_integer_option' : too few arguments for call
lua_cjson.c(306) : error C2065: 'json_config_t' : undeclared identifier
lua_cjson.c(306) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(306) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
lua_cjson.c(306) : error C2106: '=' : left operand must be l-value
lua_cjson.c(309) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(309) : error C2223: left of '->encode_keep_buffer' must point to struct/union
lua_cjson.c(311) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(311) : error C2223: left of '->encode_keep_buffer' must point to struct/union
lua_cjson.c(311) : warning C4047: 'function' : 'const char **' differs in levels of indirection from 'int'
lua_cjson.c(311) : warning C4024: 'json_enum_option' : different types for formal and actual parameter 4
lua_cjson.c(311) : error C2198: 'json_enum_option' : too few arguments for call
lua_cjson.c(314) : error C2065: 'cfg' : undeclared identifier
lua_cjson.c(314) : error C2223: left of '->encode_keep_buffer' must point to struct/union
lua_cjson.c(314) : fatal error C1003: error count exceeds 100; stopping compilation
Error: Failed installing dependency: https://rocks.moonscript.org/lua-cjson-2.1.0-1.src.rock - Build error: Failed compiling object lua_cjson.obj
local cjson = require "cjson"
local tab = {}
tab[10] = 199
tab.pp = "fan"
local estr = cjson.encode(tab)
print("-------------", estr)
local totab = cjson.decode(estr)
for k,v in pairs(totab) do
print("---fff-------", k,v)
end
the question : 199 change to 199.0, so bad when i use to json protocol to communicate with client!
how to solve it, please.
thanks!
Hi!
Why does lua-cjson escape slashes:
> cjson = require 'cjson'
> print(cjson.encode('/'))
"\/"
Is that a bug?
Hi Mark,
Only minimified output is possible?
An option for indented output would be nice.
Thanks,
Cosmin.
Hi,
I use luajit 2.1.0-beta3 and lua-cjson 2.1.0-1 from http://luarocks.org,I print a error "cjson.so: undefined symbol: lua_rotate" when running. I search that "lua_rotate" is a new add in lua 5.3. But there has the note in lua-cjson 2.1.0-1 require lua > 5.1 not 5.3. So, which lua version is need by lua-cjson 2.1.0-1?luajit 2.1.0-beta3 and lua-cjson 2.1.0-1 not match?
Thank you.
有如下代码:
location /echo1 {
content_by_lua '
local tb = {y=123,b=234,c=456}
local cjson = require "cjson"
local out = cjson.encode(tb)
ngx.say(out)
';
}
访问http://xxx/echo1会输出;
{"y":123,"c":456,"b":234}
如何按原来的{"y":123,"b":234,"c":456} 输出?我知道hash table都是无序或者说是顺序不确定的,如何实现我上述需求?
root@tt:~# lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio (double int32)
> j = require 'cjson'
>
> s = {"",""}
>
> print(j.encode(s))
["",""]
> s = {nil,nil}
>
> print(j.encode(s))
{}
>
How to get empty json array,use cjson.
I'm not expert and, or seasoned Lua programmer or user, but I find that you defined cjson2 two times in separate examples or statements in your manual.adoc as:
local cjson2 = cjson.new()
which has not been used anywhere or any further in this document and, or examples :-(
The poor and, or overly expressive documentation and bad examples are major reasons for the good code and, or nice open source software like this and Lua going ashtray day by day.
Hello, I have a question. When I use decode to handle a string like "expirePay":{"key":"","value":null,"descr":""}, the value is converted to cjson.null instead of Lua's nil. cjson.null is treated as userdata in my Lua script and I can't process it. How should I handle this? Thank you.
Can we add header guards to strbuf.h?
#pragma once
#ifndef _STRBUF_H_
#define _STRBUF_H_
#endif
Using Lua 5.1.5
On Rocky Linux 8.7
Linux uhn5klbjhmgmt-p.lab.rmn.local 4.18.0-425.10.1.el8_7.x86_64 #1 SMP Thu Jan 12 16:32:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-16)
Luarocks compiles and installs it with no problem :
$ luarocks install lua-cjson
Warning: falling back to wget - install luasec to get native HTTPS support
Installing https://luarocks.org/lua-cjson-2.1.0.10-1.src.rock
lua-cjson 2.1.0.10-1 depends on lua >= 5.1 (5.1-1 provided by VM)
gcc -O2 -fPIC -I/home/user1/.luaver/lua/5.1.5/include -c lua_cjson.c -o lua_cjson.o
gcc -O2 -fPIC -I/home/user1/.luaver/lua/5.1.5/include -c strbuf.c -o strbuf.o
gcc -O2 -fPIC -I/home/user1/.luaver/lua/5.1.5/include -c fpconv.c -o fpconv.o
gcc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
lua-cjson 2.1.0.10-1 is now installed in /home/user1/.luaver/luarocks/3.9.2_5.1 (license: MIT)
However when issuing a require
on it it fail:
/usr/bin/lua: error loading module 'cjson.safe' from file '/home/user1/lib/lua/5.1/cjson/safe.so':
/home/user1/lib/lua/5.1/cjson/safe.so: undefined symbol: lua_pcall
stack traceback:
[C]: in ?
[C]: in function 'require'
./sgce_check.lua:4: in main chunk
[C]: in ?
Hi,
Using lua v5.1 and lua-cjson v2.1.0.6-1.
Why print(cjson.decode(cjson.encode({success = true})))
results in ""
(an empty line) and not {success = true}
?
What am I missing?!
Thanks!
How can allow '' back slash in lua cjson.decode
{"status_code":503,"body":"{"code": 1, "message": "Service unavailable"}"}
I want to decode the statement above, but got Expected value but found invalid token at character 27
error. I tried another statement like below:
{"status_code":503,"body":{"code": 1, "message": "Service unavailable"}}
However there is same error. It looks like cjson does't support decode nested? Is there another solution? Thx.
Hi,
CJSON is not locale independent. Specifically, if the current locale wants us to encode the decimal point as a comma, as in Germany, CJSON will happily do so.
This makes the other side rather unhappy. Worse, if the problem occurs in a list it's not even recognized by the receiving parser.
Please fix!
Hi. I've got problems installing cjson with luajit2.1:
lua_cjson.c:1298:13: error: static declaration of ‘luaL_setfuncs’ follows non-static declaration
static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
^
In file included from lua_cjson.c:44:0:
/home/travis/build/OystParis/lua-nats/install/lua/include/luajit-2.1/lauxlib.h:88:18: note: previous declaration of ‘luaL_setfuncs’ was here
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
^
Error: Failed installing dependency: https://luarocks.org/lua-cjson-2.1.0-1.src.rock - Build error: Failed compiling object lua_cjson.o
do you have any idea?
This repo should be transferred to a new maintainer since the current one is highly inactive. Anyone willing to do that?
lua 5.3.1 in my mac install
cc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
Undefined symbols for architecture x86_64:
"_luaL_argerror", referenced from:
_json_encode in lua_cjson.o
_json_decode in lua_cjson.o
_json_cfg_encode_sparse_array in lua_cjson.o
_json_cfg_encode_max_depth in lua_cjson.o
_json_cfg_decode_max_depth in lua_cjson.o
_json_cfg_encode_number_precision in lua_cjson.o
_json_arg_init in lua_cjson.o
...
"_luaL_checkinteger", referenced from:
_json_cfg_encode_sparse_array in lua_cjson.o
_json_cfg_encode_max_depth in lua_cjson.o
_json_cfg_decode_max_depth in lua_cjson.o
_json_cfg_encode_number_precision in lua_cjson.o
"_luaL_checklstring", referenced from:
_json_decode in lua_cjson.o
"_luaL_checkoption", referenced from:
_json_enum_option in lua_cjson.o
"_luaL_error", referenced from:
_json_encode in lua_cjson.o
_json_decode in lua_cjson.o
_json_append_data in lua_cjson.o
_json_append_number in lua_cjson.o
_json_process_value in lua_cjson.o
_json_arg_init in lua_cjson.o
_json_protect_conversion in lua_cjson.o
...
"_luaL_setfuncs", referenced from:
_lua_cjson_new in lua_cjson.o
"_lua_checkstack", referenced from:
_json_append_data in lua_cjson.o
_json_process_value in lua_cjson.o
"_lua_createtable", referenced from:
_lua_cjson_new in lua_cjson.o
_json_process_value in lua_cjson.o
"_lua_getfield", referenced from:
_luaopen_cjson_safe in lua_cjson.o
_lua_cjson_safe_new in lua_cjson.o
"_lua_gettop", referenced from:
_json_encode in lua_cjson.o
_json_decode in lua_cjson.o
_json_arg_init in lua_cjson.o
_json_protect_conversion in lua_cjson.o
"_lua_newuserdata", referenced from:
_lua_cjson_new in lua_cjson.o
"_lua_next", referenced from:
_json_append_data in lua_cjson.o
"_lua_pcallk", referenced from:
_json_protect_conversion in lua_cjson.o
"_lua_pushboolean", referenced from:
_json_process_value in lua_cjson.o
_json_enum_option in lua_cjson.o
"_lua_pushcclosure", referenced from:
_lua_cjson_new in lua_cjson.o
_luaopen_cjson_safe in lua_cjson.o
_lua_cjson_safe_new in lua_cjson.o
"_lua_pushinteger", referenced from:
_json_cfg_encode_sparse_array in lua_cjson.o
_json_cfg_encode_max_depth in lua_cjson.o
_json_cfg_decode_max_depth in lua_cjson.o
_json_cfg_encode_number_precision in lua_cjson.o
"_lua_pushlightuserdata", referenced from:
_lua_cjson_new in lua_cjson.o
_json_process_value in lua_cjson.o
"_lua_pushlstring", referenced from:
_json_encode in lua_cjson.o
_json_process_value in lua_cjson.o
"_lua_pushnil", referenced from:
_json_append_data in lua_cjson.o
_json_arg_init in lua_cjson.o
_json_protect_conversion in lua_cjson.o
"_lua_pushnumber", referenced from:
_json_process_value in lua_cjson.o
"_lua_pushstring", referenced from:
_lua_cjson_new in lua_cjson.o
_json_enum_option in lua_cjson.o
"_lua_pushvalue", referenced from:
_json_protect_conversion in lua_cjson.o
"_lua_rawgeti", referenced from:
_json_append_data in lua_cjson.o
"_lua_rawset", referenced from:
_json_process_value in lua_cjson.o
"_lua_rawseti", referenced from:
_json_process_value in lua_cjson.o
"_lua_rotate", referenced from:
_json_protect_conversion in lua_cjson.o
"_lua_setfield", referenced from:
_lua_cjson_new in lua_cjson.o
_luaopen_cjson_safe in lua_cjson.o
_lua_cjson_safe_new in lua_cjson.o
"_lua_setmetatable", referenced from:
_lua_cjson_new in lua_cjson.o
"_lua_settop", referenced from:
_json_append_data in lua_cjson.o
"_lua_toboolean", referenced from:
_json_append_data in lua_cjson.o
_json_enum_option in lua_cjson.o
"_lua_tolstring", referenced from:
_json_append_string in lua_cjson.o
"_lua_tonumberx", referenced from:
_json_append_data in lua_cjson.o
_json_append_number in lua_cjson.o
"_lua_touserdata", referenced from:
_json_encode in lua_cjson.o
_json_decode in lua_cjson.o
_json_append_data in lua_cjson.o
_json_arg_init in lua_cjson.o
_json_destroy_config in lua_cjson.o
"_lua_type", referenced from:
_json_cfg_encode_sparse_array in lua_cjson.o
_json_cfg_encode_max_depth in lua_cjson.o
_json_cfg_decode_max_depth in lua_cjson.o
_json_cfg_encode_number_precision in lua_cjson.o
_json_append_data in lua_cjson.o
_json_append_number in lua_cjson.o
_json_enum_option in lua_cjson.o
...
"_lua_typename", referenced from:
_json_append_data in lua_cjson.o
_json_append_number in lua_cjson.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
lua_cjson.c: In function ‘json_append_data’:
lua_cjson.c:743:19: warning: implicit declaration of function ‘lua_objlen’ [-Wimplicit-function-declaration]
len = lua_objlen(l, -1);
^~~~~~~~~~
I'm using lua 5.3, and according to the manual lua_objlen is a thing, so I don't see why this would error, but it does
I'm requiring the module using
package.path = package.path .. ";../?.lua"
local json = require('cjson')
And I'm getting:
lua: error loading module 'cjson' from file '/usr/local/lib/lua/5.3/cjson.so':
dlopen(/usr/local/lib/lua/5.3/cjson.so, 6): Symbol not found: _lua_objlen
Referenced from: /usr/local/lib/lua/5.3/cjson.so
Expected in: flat namespace
in /usr/local/lib/lua/5.3/cjson.so
stack traceback:
[C]: in ?
[C]: in function 'require'
log.lua:6: in main chunk
[C]: in ?
Got error requiring cjson
:
/apps/nginx/lua-cjson/cjson.so:1: unexpected symbol near 'char(127)'
cjson was built with command:
make LUA_INCLUDE_DIR=/usr/include/lua5.2
Even though in the makefile i changed lua_version from 5.3 to 5.1 it doesnt load in 5.1 and luajit and instead spits out this error:
error loading module 'cjson' from file './cjson.so':
./cjson.so: undefined symbol: luaL_setfuncs
I am using "make"
EDIT: Short: I cant compile it for luajit. I'm a noob at c compiling and stuff, and it doesnt show anywhere in the manual.
Hi,
I'm trying to compile lua-json with the USE_INTERNAL_FPCONV file and get the following compliation error:
fpconv.c:205:6: error: redefinition of ‘fpconv_init’
void fpconv_init()
^~~~~~~~~~~
In file included from fpconv.c:36:0:
fpconv.h:10:20: note: previous definition of ‘fpconv_init’ was here
static inline void fpconv_init()
^~~~~~~~~~~
Makefile:90: recipe for target 'fpconv.o' failed
make[1]: *** [fpconv.o] Error 1
Indeed, fpconv_init is defined in both fpconv.c and fpconv.h.
A fix could be undefining fpconv_init() in fpconv.c under the same flag.
What do you think?
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include -fpic -o lua_cjson.o lua_cjson.c
In file included from lua_cjson.c:48:
fpconv.h:15:20: warning: inline function ‘fpconv_init’ declared but never defined
15 | extern inline void fpconv_init();
| ^~~~~~~~~~~
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include -fpic -o strbuf.o strbuf.c
cc -c -O3 -Wall -pedantic -DNDEBUG -I/usr/local/include -fpic -o fpconv.o fpconv.c
cc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
I cannot imagine what good the inline without body is. I've compiled with the following patch and everything seems to work fine.
From c9551b68ec78ecea8d31af41bbffc2f4a39d1965 Mon Sep 17 00:00:00 2001
From: Tomas Volf <[email protected]>
Date: Tue, 15 Mar 2022 18:30:24 +0100
Subject: [PATCH] Fix compilation warning about fpconv_init
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
In file included from lua_cjson.c:48:
fpconv.h:15:20: warning: inline function ‘fpconv_init’ declared but never defined
15 | extern inline void fpconv_init();
| ^~~~~~~~~~~
inline does not seem to do anything useful here, since there is no body
to inline. And extern is default. So let's just drop both, we get no
warning and everything still works.
---
fpconv.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fpconv.h b/fpconv.h
index 0124908..22cab98 100644
--- a/fpconv.h
+++ b/fpconv.h
@@ -12,7 +12,7 @@ static inline void fpconv_init()
/* Do nothing - not required */
}
#else
-extern inline void fpconv_init();
+void fpconv_init();
#endif
extern int fpconv_g_fmt(char*, double, int);
--
2.35.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.