Tiny JSON Encoder

Post and discuss new components that you have created.
Post Reply
mnfisher
Valued Contributor
Posts: 1339
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 126 times
Been thanked: 667 times

Tiny JSON Encoder

Post by mnfisher »

I recently started playing with IOT and needed a JSON encoder - and having waded in (only to find that there is one supplied with Flowcode :-) )

I offer this up anyway - it offers support for arrays (given here is Long Int and float) - but this would be easily extended to other int types (8 or 16 bit)

It's also slightly smaller - good for PICs etc with minimal memory.

Usage is slightly different.

User has a string (say .json)

Call CreateJSON(.json)
Then the 'add' functions - for example AddInt(.json, "Name", 123) (json is now {"Name": 123})
AddString(,json, "AString", "Hello") (json is now {"Name": 123, "String": "Hello"} )

.data[3] = {1,2,3}

AddIntArray(.json, "IntArray", .data, 3) (json = {"Name": 123, "String": "Hello", "IntArray": [1, 2,3]}

etc

The user must ensure that then string has sufficient space to hold the json - but there is little overhead for devices with minimal RAM.

I did hit a snag - AddStrArray - the type of the string array must match the parameter definition which is less than convenient...

Any one any ideas - I wondered about a large string with a 'separator character (',' ?) which cannot appear in any string - so I added this - here it expects a ',' between strings there is some code in json main that can be run in simulation... It might be better to pass the delimiter as an parameter (this for example would allow nested JSON - perhaps using '\t' (tab) as a delimiter

It generates the same JSON as the FC component - except I don't add a final '\n' - is this needed?

Martin
Attachments
Tiny_JSON.fcpx
(2.44 KiB) Downloaded 4 times
tiny_json_test.fcfx
(15.47 KiB) Downloaded 6 times
json.fcfx
(27.67 KiB) Downloaded 6 times

Post Reply