[Libwebsockets] LEJP array of strings

andy at warmcat.com andy at warmcat.com
Mon Mar 1 19:54:37 CET 2021



On March 1, 2021 3:28:12 PM UTC, Brice Hamon <brice at ydotm.com> wrote:

>I didn't know lws had a Json parser build in :)


There's some more introductory info about it here

https://warmcat.com/2020/03/08/libwebsockets-lejp.html

also there exists much higher level support for C struct <-> JSON (based on top of this JSON parser, including huge strings) and C struct <-> Sqlite.

https://warmcat.com/2020/03/27/libwebsockets-lws_struct.html

Sai (https://warmcat.com/git/sai ) is written using these apis, eg, both the web ui and builders communicate over ws links to the server about build status, logs etc using sqlite -> C structs -> JSON objects with these apis.  While it's not exactly trivial, this hugely reduced the amount of code involved compared to doing it manually.

-Andy

>On Mon, Mar 1, 2021 at 9:28 AM Andy Green <andy at warmcat.com> wrote:
>
>>
>>
>> On 3/1/21 2:05 PM, Jean-Paul Saman wrote:
>> > Hi,
>> >
>> > How do I serialize/parse an array of strings with LEJP functions?
>> >
>> > Here is a JSON snippit I have trouble with to solve with LEJP:
>> > [
>> >   {
>> >     "groups": [ "item1", "item2", "item3" ]
>> >   }
>> > ]
>> >
>> > Any pointers/comment/solution are welcome.
>>
>> If I put your JSON into a file "my.json" verbatim, build current main
>> branch lws, and do
>>
>> $ cat my.json
>|/projects/libwebsockets/build/bin/libwebsockets-test-lejp
>> [2021/03/01 14:24:19:7398] N: libwebsockets-test-lejp  (C) 2017 -
>2018
>> andy at warmcat.com
>> [2021/03/01 14:24:19:7399] N:   usage: cat my.json |
>> libwebsockets-test-lejp
>>
>> [2021/03/01 14:24:19:7399] N: LEJPCB_CONSTRUCTED: path  match 0
>statckp 0
>> [2021/03/01 14:24:19:7400] N: LEJPCB_START: path  match 0 statckp 0
>> [2021/03/01 14:24:19:7400] N: LEJPCB_ARRAY_START: path [] match 0
>statckp 2
>> [2021/03/01 14:24:19:7400] N:  LEJPCB_OBJECT_START: path [] match 0
>> statckp 2
>> [2021/03/01 14:24:19:7400] N:   path: '[].groups' (LEJPCB_PAIR_NAME)
>> [2021/03/01 14:24:19:7400] N:   LEJPCB_PAIR_NAME: path [].groups
>match 0
>> statckp 9
>> [2021/03/01 14:24:19:7400] N:   LEJPCB_ARRAY_START: path [].groups[]
>> match 0 statckp 11
>> [2021/03/01 14:24:19:7400] N:    LEJPCB_VAL_STR_START: path
>[].groups[]
>> match 0 statckp 11
>> [2021/03/01 14:24:19:7400] N:        value 'item1' (array indexes: 0
>0 )
>>   (LEJPCB_VAL_STR_END)
>> [2021/03/01 14:24:19:7401] N:    LEJPCB_VAL_STR_START: path
>[].groups[]
>> match 0 statckp 11
>> [2021/03/01 14:24:19:7401] N:        value 'item2' (array indexes: 0
>1 )
>>   (LEJPCB_VAL_STR_END)
>> [2021/03/01 14:24:19:7401] N:    LEJPCB_VAL_STR_START: path
>[].groups[]
>> match 0 statckp 11
>> [2021/03/01 14:24:19:7401] N:        value 'item3' (array indexes: 0
>2 )
>>   (LEJPCB_VAL_STR_END)
>> [2021/03/01 14:24:19:7401] N:   LEJPCB_ARRAY_END: path [] match 0
>statckp 2
>> [2021/03/01 14:24:19:7401] N:  LEJPCB_OBJECT_END: path [] match 0
>statckp 2
>> [2021/03/01 14:24:19:7401] N: LEJPCB_ARRAY_END: path [] match 0
>statckp 2
>> [2021/03/01 14:24:19:7401] N: Parsing Completed (LEJPCB_COMPLETE)
>> [2021/03/01 14:24:19:7402] N: LEJPCB_COMPLETE: path [] match 0
>statckp 2
>> [2021/03/01 14:24:19:7402] N: okay (2)
>> [2021/03/01 14:24:19:7402] N: LEJPCB_DESTRUCTED: path [] match 0
>statckp 2
>>
>> it seems fine.  Just look at what the test app does, it is very small
>> and easy to understand.
>>
>>
>>
>https://libwebsockets.org/git/libwebsockets/tree/test-apps/test-lejp.c?h=main
>>
>> -Andy
>> _______________________________________________
>> Libwebsockets mailing list
>> Libwebsockets at ml.libwebsockets.org
>> https://libwebsockets.org/mailman/listinfo/libwebsockets
>>


More information about the Libwebsockets mailing list