On Friday, May 17, 2019 at 2:43:42 PM UTC-4, Bart wrote:
> On 17/05/2019 19:16, Rick C. Hodgin wrote:
> > On Friday, May 17, 2019 at 1:43:47 PM UTC-4, Bart wrote:
> >> On 17/05/2019 18:21, Rick C. Hodgin wrote:
> >>> Might make a good comp.lang.c coding challenge.
> >>
> >> Maybe, but the whole thing is rather open-ended.
> >
> > Let's define it.
> >
> > 1) The user provides a structure. Format must be per line:
> > field_name, type[, optional]
>
> This part is not clear. I think he prefers this structure to be defined
> as normal C code. If that's too hard, then an alternative can be used.
>
> But I don't think what you have is sufficient:
>
> What are the possibilities for 'type'? The OP's example included
> 'char[32]' and 'int[8]' and 'unsigned int'. Just how elaborate can these
> get? And what size is 'int' anyway?
In JSON, you're always dealing with text versions of the data,
so you translate into text. char[32] can contain "rick" or
"bart" only. It doesn't need to contain the full 32, but if
it is given "Bartholmew Montgomery Scott, Esquire" (36 chars),
then it truncates it to 32.
> How do you deal with nested structs?
type = JSON.
> > 2) Must be able to receive input data like this:
> > field_name = data
>
> I don't remember this was part of the requirement. Is this C code?
No. This would just be a way to receive input to encode into
the JSON string.
> > 3) Must generate output JSON.
>
> I don't remember this part either! (If needed, there are likely to be
> existing solutions.)
His goals were to be interoperative with other systems, so it
must be able to take local data, build JSON, and be able to
take remote JSON, and build local data.
> > 4) Must be able to parse JSON input into:
> > field_name = data
>
> That's not so simple either. You can't do this with a char[32] type or
> int[8].
char = "rick"
array = [1, 2, 3, 4, 5, 6, 7, 8]
> And that still requires 'data' to be captured in a suitable
> format, which may be unpacked (so char[3] taking 3 bytes may be
> represented by a list of 3 ints taking 12 bytes).
It's always in text form in JSON. sprintf(output, "%d", value)
for each and you convert int values to text.
> Additionally, you can't expect the date to be present in the same order
> as in the struct. This is what makes a hard-coded parser very much
> harder. You can't do:
>
> readinteger("id", S->id, 4);
> readstring("name", &S->name,32);
> readarray("array", &S->array,8,4);
>
> You have to tentatively read a field tag, and try and match it with one
> of the fields of the struct. It has to be more table-driven, but in that
> case, it is more the data that is hard-coded rather than the parsing code.
The requirements of JSON are all that's required on the inside.
Does JSON have a required order? I think it's more by protocol
or use cases that it would need to be in a particular order. At
the same level, each item should be made visible to the caller.
I would generically parse an input structure defining the needs
of the JSON output, but be able to receive input in any order.
Only when the output is required to be in that order would it
then make it be that way.
I would probably also set a "populated" flag to indicate if one
of the fields is populated or not, and if not do not even include
it in the output ... unless it's flagged required, or possibly if
it's not flagged optional.
I think it would get us close. But, we just need to look at the
needs of JSON, and then interpolate between C's needs, and JSON's
needs.
--
Rick C. Hodgin