Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #143876

Re: Static JSON parser for embedded

From pozz <pozzugno@gmail.com>
Newsgroups comp.lang.c
Subject Re: Static JSON parser for embedded
Date 2019-05-17 13:19 +0200
Organization A noiseless patient Spider
Message-ID <qbm5ch$jm7$1@dont-email.me> (permalink)
References <qblnbg$402$1@dont-email.me> <1cwDE.355087$ry6.215480@fx11.am4>

Show all headers | View raw


Il 17/05/2019 12:39, Bart ha scritto:
> On 17/05/2019 08:20, pozz wrote:
>> I'd like to use JSON documents in my embedded project. I don't need 
>> full parsing capabilities, because JSON could be very complex in general.
>>
>> However I will use simple C structs that are equivalent to JSON 
>> messages, for example:
>>
>>
>> struct myStruct {
>>    unsigned int id;
>>    char name[32];
>>    int array[8];
>> };
>>
>> {
>>    "id": 12345,
>>    "name": "John",
>>    "array": [ -3, 7, 8 ]
>> }
>>
>>
>> I'm searching a static converter from JSON to C struct, some sort of 
>> preprocessor that generates C code from C struct definition.
>>
>> The parse function prototype could be:
>>
>>    int json_parse(const char *s, struct myStruct *dst);
>>
>> if json_parse() returns 0, the result is in dst and I can access all 
>> the members of the struct in a simple way in C code.
> 
> ....
> 
>  > NXP LPC1768, 512kB Flash memory, 64kB RAM memory (that is almost full).
>  > I can't use dynamic memory.
> 
> It's not clear whether you are looking for one or two things, and 
> whether it/they run on this device, or on some development PC.

Yes, it was unclear in my request. I have a development machine with C 
cross-compiler for a target machine (NXP-based).

I was thinking at a "JSON compiler" that runs on the development machine 
and that generates C source code for cross-compiler.


> For example json_parse function sounds like it converts JSON data to 
> binary, arranged according to some C struct layout (but how does it know 
> that "12345" is unsigned int, or how big 'int' is?)

Bad example, I'm sorry.
"12345" *is* a string in JSON and will be a string in C struct.


> While a JSON to C converter generates source code that then needs a C 
> compiler to do anything with.

Yes, of course. I hope it's clear now.


> You say elsewhere that you generate the JSON data yourself; in that case 
> can't you create the matching struct definitions at the same time?

Yes, I start defining a JSON message and an equivalent C struct. What I 
need is a C function that parses the JSON message, check its correctness 
and matching of the reference struct and fill the members.

Of course, if I define N JSON messages, I will have N parsing functions 
with N structs.


> Note that a function such as:
> 
>      int json_parse(const char *s, struct myStruct *dst);
> 
> will presumably /only/ be able to parse data that matches that one 
> struct, and nothing else. And will only parse one struct, not a sequence 
> of them.

Maybe the name of the function would be json_parse_myStruct(). For 
another struct I will have a *different* function json_parse_myStruct2().


> Or are you really looking for something like this (which runs on the 
> device):
> 
>     int json_parse(char* s, J_Descr d, void* dst);
> 
> Where the extra parameter d /describes/ the shape and types of the data 
> expected? Here, while the function is general, at each call point you 
> must know what shape struct is expected, and provide a suitable d, and a 
> a pointer to a suitable struct.

Yes, this is another approach, a "dynamic" approach, because the parser 
must be ready to expect a generic JSON message, structured as d.


Even if my approach is "static", it should be future-proof. It should 
ignore unknown keys (maybe they will be added in future versions) 
without errors.

And it should be flexible, because JSON messages could be generated by 
different sources that don't guarantee the order of the keys. So the 
parser should detect known keys/values in whatever order they appear in 
the message.


My idea comes from Protocol Buffers[1] from Google. They have a 
"protobuf compiler" to convert the message descriptor file (.proto) in a 
suitable code.

[1] https://developers.google.com/protocol-buffers/

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 09:20 +0200
  Re: Static JSON parser for embedded Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-05-17 08:48 +0000
    Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 11:06 +0200
      Re: Static JSON parser for embedded Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-05-17 11:22 +0000
        Re: Static JSON parser for embedded Thiago Adams <thiago.adams@gmail.com> - 2019-05-17 04:40 -0700
          Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 14:10 +0200
            Re: Static JSON parser for embedded Thiago Adams <thiago.adams@gmail.com> - 2019-05-17 05:18 -0700
              Re: Static JSON parser for embedded Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-05-18 18:18 +0000
        Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 14:10 +0200
          Re: Static JSON parser for embedded Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-05-18 18:22 +0000
  Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 11:39 +0100
    Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 13:19 +0200
      Re: Static JSON parser for embedded Thiago Adams <thiago.adams@gmail.com> - 2019-05-17 05:09 -0700
        Re: Static JSON parser for embedded Thiago Adams <thiago.adams@gmail.com> - 2019-05-17 05:15 -0700
      Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 15:06 +0100
        Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 16:37 +0200
          Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 17:56 +0100
            Re: Static JSON parser for embedded "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-05-17 10:21 -0700
              Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 18:43 +0100
                Re: Static JSON parser for embedded Thiago Adams <thiago.adams@gmail.com> - 2019-05-17 10:58 -0700
                Re: Static JSON parser for embedded "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-05-17 11:16 -0700
                Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 19:43 +0100
                Re: Static JSON parser for embedded "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-05-17 11:57 -0700
                Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 20:11 +0100
                Re: Static JSON parser for embedded "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-05-17 12:22 -0700
                Re: Static JSON parser for embedded Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-05-17 21:33 +0100
                Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-18 00:11 +0200
            Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-17 22:33 +0100
              Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-18 00:17 +0200
                Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-18 01:25 +0100
            Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-17 23:43 +0200
  Re: Static JSON parser for embedded Thiago Adams <thiago.adams@gmail.com> - 2019-05-17 04:38 -0700
  Re: Static JSON parser for embedded "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-05-17 07:38 -0700
    Re: Static JSON parser for embedded Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-05-17 08:05 -0700
      Re: Static JSON parser for embedded "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-05-17 08:15 -0700
      Re: Static JSON parser for embedded Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2019-05-17 11:53 -0400
  Re: Static JSON parser for embedded Ian Collins <ian-news@hotmail.com> - 2019-05-18 15:33 +1200
    Re: Static JSON parser for embedded Bart <bc@freeuk.com> - 2019-05-18 11:57 +0100
  Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-05-18 17:14 +1000
    Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-05-18 16:03 +0200
      Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-05-19 09:12 +1000
        Re: Static JSON parser for embedded Niklas Holsti <niklas.holsti@tidorum.invalid> - 2019-05-19 17:13 +0300
          Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-05-20 09:25 +1000
        Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-06-06 11:12 +0200
          Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-06-07 09:11 +1000
            Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-06-07 08:59 +0200
              Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-06-12 09:41 +1000
    Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-06-05 16:44 +0200
      Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-06-06 09:27 +1000
        Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-06-06 09:50 +0200
          Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-06-07 09:08 +1000
      Re: Static JSON parser for embedded Clifford Heath <no.spam@please.net> - 2019-06-06 09:48 +1000
        Re: Static JSON parser for embedded pozz <pozzugno@gmail.com> - 2019-06-06 09:54 +0200

csiph-web