JSON pretty encode

1,598 views
Skip to first unread message

Bogdan Irimia

unread,
Sep 28, 2015, 8:37:28 AM9/28/15
to openre...@googlegroups.com
Hello, everybody

We need to encode a LUA data structure using JSON encoding, but in a human readable format, like this:

{
  "key1": "data",
  "key2": 27,
  "key3": {
    "key3_1": "something",
    "key3_2": "something else"
  },
  "key4": [
    "item1",
    "item2"
  ]
}

Please note the indentation for each item in the data structure.

I found for this two libraries written in pure Lua:
The first one seems to be faster for encoding (according to this: http://lua-users.org/wiki/JsonModules), but anyhow much slower than other C-based libraries (like "cjson" already included in OpenResty).

This type of "human readable encoding" (or "pretty encoding") is used only in a few places in our application, so performance is not critical, but the output has to be compatible with the "cjson" library.

What would you recommend?

Thank you

Bogdan

Aapo Talvensaari

unread,
Sep 28, 2015, 8:46:16 AM9/28/15
to openre...@googlegroups.com
On 28 September 2015 at 15:37, Bogdan Irimia <bog...@digitair.ro> wrote:
> We need to encode a LUA data structure using JSON encoding, but in a human readable format

Bogdan Irimia

unread,
Sep 28, 2015, 9:04:42 AM9/28/15
to openre...@googlegroups.com
Wasn't clear for me if it formats the text as we need it.

Aapo Talvensaari wrote:

On 28 September 2015 at 15:37, Bogdan Irimia <bog...@digitair.ro
<mailto:bog...@digitair.ro>> wrote:

    > We need to encode a LUA data structureusing JSON encoding, but

    in a human readable format


How about this:
https://github.com/bungle/lua-resty-libcjson

--
You received this message because you are subscribed to the Google
Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to openresty-en...@googlegroups.com
<mailto:openresty-en...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

Aapo Talvensaari

unread,
Sep 28, 2015, 9:37:28 AM9/28/15
to openresty-en
On Monday, 28 September 2015 16:04:42 UTC+3, bogdan wrote:
Wasn't clear for me if it formats the text as we need it.



It is easy to test:

print(json.encode({
  key1
= "data",
  key2
= 27,
  key3
= {
    key3_1
= "something",
    key3_2
= "something else"
 
},
  key4
= {
   
"item1",
   
"item2"
 
}
}, true))


This will output:

{
 
"key1": "data",
 
"key3": {
   
"key3_1": "something",
   
"key3_2": "something else"
 
},
 
"key2": 27,
 
"key4": ["item1", "item2"]
}


Not sure though if this is what you wanted as ut seems you like to have each array item on their own line.



Regards

Aapo

Bogdan Irimia

unread,
Sep 28, 2015, 9:39:05 AM9/28/15
to openre...@googlegroups.com
It'll do with arrays on one line. I tested it, works pretty ok for what we need.

Thank you

Monday, September 28, 2015 4:37 PM via Postbox
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Monday, September 28, 2015 4:04 PM via Postbox
Wasn't clear for me if it formats the text as we need it.

Aapo Talvensaari wrote:
Monday, September 28, 2015 3:45 PM via Postbox
> We need to encode a LUA data structure using JSON encoding, but in a human readable format
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Monday, September 28, 2015 3:37 PM via Postbox
Hello, everybody

We need to encode a LUA data structure using JSON encoding, but in a human readable format, like this:

{
  "key1": "data",
  "key2": 27,
  "key3": {
    "key3_1": "something",
    "key3_2": "something else"
  },
  "key4": [
    "item1",
    "item2"
  ]
}

Please note the indentation for each item in the data structure.

I found for this two libraries written in pure Lua:
The first one seems to be faster for encoding (according to this: http://lua-users.org/wiki/JsonModules), but anyhow much slower than other C-based libraries (like "cjson" already included in OpenResty).

This type of "human readable encoding" (or "pretty encoding") is used only in a few places in our application, so performance is not critical, but the output has to be compatible with the "cjson" library.

What would you recommend?

Thank you

Bogdan

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.

Aapo Talvensaari

unread,
Sep 28, 2015, 1:02:55 PM9/28/15
to openresty-en
On Monday, 28 September 2015 16:39:05 UTC+3, bogdan wrote:
It'll do with arrays on one line. I tested it, works pretty ok for what we need

You may also check this:

I just wrote a simple pretty formatter for Lua cJSON in Lua (no extra dependencies). This does excatly what you wanted. 

Stefan Parvu

unread,
Sep 28, 2015, 1:46:40 PM9/28/15
to openre...@googlegroups.com

> You may also check this:
> https://github.com/bungle/lua-resty-prettycjson
>

10 x thanks. We will use that, less dependencies and no need for extra
binaries, shared libs.

--
Stefan Parvu <spa...@kronometrix.org>
Reply all
Reply to author
Forward
0 new messages