Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to encode empty table as array #23

Closed
stuta opened this issue May 12, 2017 · 7 comments
Closed

Option to encode empty table as array #23

stuta opened this issue May 12, 2017 · 7 comments

Comments

@stuta
Copy link

stuta commented May 12, 2017

Hello.

Is it possible for empty Lua tables {} to return "[]" instead of "{}"?
Lua cjson has this option: cjson.encode_empty_table_as_object(false). I can use __jsontype, but then I would need to attach it to all possible deep elements.

I mean {a={}} => {"a": []}.

@xpol
Copy link
Owner

xpol commented May 16, 2017

Noted and work in progress. Hold on.

@stuta
Copy link
Author

stuta commented May 16, 2017

Thanks!

I think the detection code "if something is an array" is not correct.
{"asd", c=2} should give (like in cjson) => {"1": "asd", "c": 2}, not ["asd"]. It's ok for me if this is an option too. I'm sure the detection code will be found from cjson.

@xpol
Copy link
Owner

xpol commented May 17, 2017

It is just a choice. For any non empty table with length > 0, will treat as array.
That is lua-rapidjson is currently not support mixed hash and array tables.

@xpol
Copy link
Owner

xpol commented May 17, 2017

ccd02f2 Added empty_table_as_array option for rapidjson.encode(). You can have it a try.

@stuta
Copy link
Author

stuta commented May 17, 2017

Thanks, I'll try later.

If this does not not support mixed hash and array tables (separate option is ok), then I can't replace cjson with lua-rapidjson. This is not compatible with any other lua json library.

@stuta
Copy link
Author

stuta commented May 17, 2017

I tested this with nested arrays, it works. I think I can use this in most-used user case: sending json from my luajit server to browser. I know that I do not have mixed json there.

@stuta
Copy link
Author

stuta commented May 17, 2017

I changed in pretty json 4 spaces to one tab (saves space in big json):
if (pretty)
{
PrettyWriter writer(*s);
writer.SetIndent('\t', 1);
encodeValue(L, &writer, idx);
}

I had some trouble to change CMakeLists.txt to use sse2 or sse4.2 (osx, win and linux), if somebody needs info I can give it.

This library is really fast, first one to beat cjson (and msgpack). Thanks again!

@xpol xpol closed this as completed Mar 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants