Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Neutrinos have bad breadth.


devel / comp.lang.c / JSON to C converter

SubjectAuthor
* JSON to C converterMalcolm McLean
+- Re: JSON to C converterKenny McCormack
+* Re: JSON to C converterKaz Kylheku
|`* Re: JSON to C converterMalcolm McLean
| +* Re: JSON to C converterManfred
| |`* Re: JSON to C converterMalcolm McLean
| | `- Re: JSON to C converterManfred
| +- Re: JSON to C converterKaz Kylheku
| `* Re: JSON to C converterBen Bacarisse
|  `* Re: JSON to C converterMalcolm McLean
|   +* Re: JSON to C converterBen Bacarisse
|   |`* Re: JSON to C converterMalcolm McLean
|   | `- Re: JSON to C converterBen Bacarisse
|   `- Re: JSON to C converterÖö Tiib
`* Re: JSON to C converterBonita Montero
 `* Re: JSON to C converterMalcolm McLean
  `* Re: JSON to C converterBonita Montero
   `* Re: JSON to C converterMalcolm McLean
    +* Re: JSON to C converterBonita Montero
    |`* Re: JSON to C converterMalcolm McLean
    | `- Re: JSON to C converterBonita Montero
    +* Re: JSON to C converterBart
    |`- Re: JSON to C converterMalcolm McLean
    `- Re: JSON to C converterKaz Kylheku

1
JSON to C converter

<8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21051&group=comp.lang.c#21051

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:21cf:b0:42d:cc:4121 with SMTP id d15-20020a05621421cf00b0042d00cc4121mr7692034qvh.70.1648814109706;
Fri, 01 Apr 2022 04:55:09 -0700 (PDT)
X-Received: by 2002:ad4:5949:0:b0:441:8296:a12d with SMTP id
eo9-20020ad45949000000b004418296a12dmr7515145qvb.4.1648814109536; Fri, 01 Apr
2022 04:55:09 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Apr 2022 04:55:09 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:41c1:5be1:529f:c686;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:41c1:5be1:529f:c686
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
Subject: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 01 Apr 2022 11:55:09 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 10
 by: Malcolm McLean - Fri, 1 Apr 2022 11:55 UTC

I'm thinking of writing a routine to convert JSON to C structures in C source
code. (It would go in the Baby X resource compiler as the first use case).

So it takes JSON and spits out a C source file with a structure declaration
and initial elements in a following definition.

I can see some obvious difficulties, for instance JSON allows for arrays
of mixed types. Then a JSON object has an identier, but not a type name,
whilst C structures require type name.

Anyone any ideas about how to proceed?

Re: JSON to C converter

<t26sup$2co01$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21052&group=comp.lang.c#21052

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Fri, 1 Apr 2022 12:59:05 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <t26sup$2co01$1@news.xmission.com>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
Injection-Date: Fri, 1 Apr 2022 12:59:05 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2514945"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Fri, 1 Apr 2022 12:59 UTC

In article <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>,
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>I'm thinking of writing a routine to convert JSON to C structures in C source
>code. (It would go in the Baby X resource compiler as the first use case).
>
>So it takes JSON and spits out a C source file with a structure declaration
>and initial elements in a following definition.
>
>I can see some obvious difficulties, for instance JSON allows for arrays
>of mixed types. Then a JSON object has an identier, but not a type name,
>whilst C structures require type name.
>
>Anyone any ideas about how to proceed?

An obvious suggestion at this point would be to ask if you've looked at the
"jq" program. I'm guessing that "jq" does something pretty similar to what
you're suggesting. Sounds like a good starting point to me.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/TedCruz

Re: JSON to C converter

<20220401075645.437@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21053&group=comp.lang.c#21053

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Fri, 1 Apr 2022 15:06:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <20220401075645.437@kylheku.com>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
Injection-Date: Fri, 1 Apr 2022 15:06:42 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5b189a79a19927f2a80b70a11ec93a75";
logging-data="28652"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19JkG+/h5Vjm809VbR7Xm3XgJyIyaeVTGY="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:CB0vAl73CN0NoVoDRGUL/ZZiFJ4=
 by: Kaz Kylheku - Fri, 1 Apr 2022 15:06 UTC

On 2022-04-01, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> I'm thinking of writing a routine to convert JSON to C structures in C source
> code. (It would go in the Baby X resource compiler as the first use case).
>
> So it takes JSON and spits out a C source file with a structure declaration
> and initial elements in a following definition.
>
> I can see some obvious difficulties, for instance JSON allows for arrays
> of mixed types. Then a JSON object has an identier, but not a type name,
> whilst C structures require type name.
>
> Anyone any ideas about how to proceed?

C99 designated initializers handle unions

jnode j0023 = { .type = jnode_num, .val = { .num = 3.14 ] };
jnode j0024 = { .type = jnode_str, .val = { .str = "abc" ] };
jnode j0025 = { .type = jnode_true };
jnode j0026 = { .type = jnode_false };

jnode *ja0027[4] = { &j0023, &j0024, &j0025, &j0026 };

jnode j0027 = { .type = jnode_vec,
.val = { .vec = { .data = ja0027, .len = 4 } } };

where jnode is something like

enum jnode_type { jnode_false, jnode_true, ..., jnode_num, ...}

typedef struct jnode {
enum jnode_type type;
union {
double num;
char *str;
struct {
struct jnode *data;
size_t len;
} vec;
} val;
} jnode;

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: JSON to C converter

<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21054&group=comp.lang.c#21054

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:508:b0:2e1:deae:22bd with SMTP id l8-20020a05622a050800b002e1deae22bdmr8754957qtx.597.1648826348932;
Fri, 01 Apr 2022 08:19:08 -0700 (PDT)
X-Received: by 2002:a37:acc:0:b0:67d:320e:7eb with SMTP id 195-20020a370acc000000b0067d320e07ebmr6602789qkk.513.1648826348682;
Fri, 01 Apr 2022 08:19:08 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Apr 2022 08:19:08 -0700 (PDT)
In-Reply-To: <20220401075645.437@kylheku.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:fdda:d7a6:ad0b:a26;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:fdda:d7a6:ad0b:a26
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com> <20220401075645.437@kylheku.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 01 Apr 2022 15:19:08 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 68
 by: Malcolm McLean - Fri, 1 Apr 2022 15:19 UTC

On Friday, 1 April 2022 at 16:06:56 UTC+1, Kaz Kylheku wrote:
> On 2022-04-01, Malcolm McLean <malcolm.ar...@gmail.com> wrote:
> > I'm thinking of writing a routine to convert JSON to C structures in C source
> > code. (It would go in the Baby X resource compiler as the first use case).
> >
> > So it takes JSON and spits out a C source file with a structure declaration
> > and initial elements in a following definition.
> >
> > I can see some obvious difficulties, for instance JSON allows for arrays
> > of mixed types. Then a JSON object has an identier, but not a type name,
> > whilst C structures require type name.
> >
> > Anyone any ideas about how to proceed?
> C99 designated initializers handle unions
>
> jnode j0023 = { .type = jnode_num, .val = { .num = 3.14 ] };
> jnode j0024 = { .type = jnode_str, .val = { .str = "abc" ] };
> jnode j0025 = { .type = jnode_true };
> jnode j0026 = { .type = jnode_false };
>
> jnode *ja0027[4] = { &j0023, &j0024, &j0025, &j0026 };
>
> jnode j0027 = { .type = jnode_vec,
> .val = { .vec = { .data = ja0027, .len = 4 } } };
>
>
> where jnode is something like
>
> enum jnode_type { jnode_false, jnode_true, ..., jnode_num, ...}
>
> typedef struct jnode {
> enum jnode_type type;
> union {
> double num;
> char *str;
> struct {
> struct jnode *data;
> size_t len;
> } vec;
> } val;
> } jnode;
>
That's to build a JSON parser in C. I already have one of those.

What I was thinking of was a program along the lines of

input
{ "name" : "Fred Bloggs",
"salary": 32000.0,
"id": 1234,
"hours_worked" : [8, 8, 7, 8, 10]
}

output

typedef struct
{ char *name;
double salary;
int id;
int hours_worked[5];
} EMPLOYEE;

EMPLOYEE fred {"Fred Blogs", 32000.0, 1234, 8, 8,7, 8, 10]};

Obviously the first issue is that there is no way a computer can get from that JSON to "EMPLOYEE".
The other issue is that whilst the scalar fields are simple, the array fields start opening up more and
more difficulties.

Re: JSON to C converter

<t278b8$1jmr$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21055&group=comp.lang.c#21055

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Fri, 1 Apr 2022 18:13:27 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t278b8$1jmr$1@gioia.aioe.org>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com>
<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="52955"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Fri, 1 Apr 2022 16:13 UTC

On 4/1/2022 5:19 PM, Malcolm McLean wrote:
> On Friday, 1 April 2022 at 16:06:56 UTC+1, Kaz Kylheku wrote:
>> On 2022-04-01, Malcolm McLean <malcolm.ar...@gmail.com> wrote:
>>> I'm thinking of writing a routine to convert JSON to C structures in C source
>>> code. (It would go in the Baby X resource compiler as the first use case).
>>>
>>> So it takes JSON and spits out a C source file with a structure declaration
>>> and initial elements in a following definition.
>>>
>>> I can see some obvious difficulties, for instance JSON allows for arrays
>>> of mixed types. Then a JSON object has an identier, but not a type name,
>>> whilst C structures require type name.
>>>
>>> Anyone any ideas about how to proceed?
>> C99 designated initializers handle unions
>>
>> jnode j0023 = { .type = jnode_num, .val = { .num = 3.14 ] };
>> jnode j0024 = { .type = jnode_str, .val = { .str = "abc" ] };
>> jnode j0025 = { .type = jnode_true };
>> jnode j0026 = { .type = jnode_false };
>>
>> jnode *ja0027[4] = { &j0023, &j0024, &j0025, &j0026 };
>>
>> jnode j0027 = { .type = jnode_vec,
>> .val = { .vec = { .data = ja0027, .len = 4 } } };
>>
>>
>> where jnode is something like
>>
>> enum jnode_type { jnode_false, jnode_true, ..., jnode_num, ...}
>>
>> typedef struct jnode {
>> enum jnode_type type;
>> union {
>> double num;
>> char *str;
>> struct {
>> struct jnode *data;
>> size_t len;
>> } vec;
>> } val;
>> } jnode;
>>
> That's to build a JSON parser in C. I already have one of those.
>
> What I was thinking of was a program along the lines of
>
> input
> {
> "name" : "Fred Bloggs",
> "salary": 32000.0,
> "id": 1234,
> "hours_worked" : [8, 8, 7, 8, 10]
> }
>
> output
>
> typedef struct
> {
> char *name;
> double salary;
> int id;
> int hours_worked[5];
> } EMPLOYEE;
>
> EMPLOYEE fred {"Fred Blogs", 32000.0, 1234, 8, 8,7, 8, 10]};
>
> Obviously the first issue is that there is no way a computer can get from that JSON to "EMPLOYEE".

Well, in fact there is, it's about dynamic types and introspection,
which is easy for interpreted languages, but not as much in C.

I believe one thing to consider is /how/ "EMPLOYEE" is going to be used
in the rest of the code, and how the rest of the code is supposed to
know about this type.
Answers to these questions will probably shed light on how this type
should be generated as well.

> The other issue is that whilst the scalar fields are simple, the array fields start opening up more and
> more difficulties.

I believe this part is a different perspective on the same problem as
the first one.

Re: JSON to C converter

<af4d3020-c7e0-4383-85f3-f7b6a845a1d2n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21056&group=comp.lang.c#21056

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5f49:0:b0:2e1:b989:7aab with SMTP id y9-20020ac85f49000000b002e1b9897aabmr9163729qta.465.1648830980851;
Fri, 01 Apr 2022 09:36:20 -0700 (PDT)
X-Received: by 2002:a05:6214:20e6:b0:440:f6d0:fe55 with SMTP id
6-20020a05621420e600b00440f6d0fe55mr8633436qvk.57.1648830980697; Fri, 01 Apr
2022 09:36:20 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Apr 2022 09:36:20 -0700 (PDT)
In-Reply-To: <t278b8$1jmr$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:f9ec:664b:1a2b:30d0;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:f9ec:664b:1a2b:30d0
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com> <05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<t278b8$1jmr$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <af4d3020-c7e0-4383-85f3-f7b6a845a1d2n@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 01 Apr 2022 16:36:20 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 91
 by: Malcolm McLean - Fri, 1 Apr 2022 16:36 UTC

On Friday, 1 April 2022 at 17:13:44 UTC+1, Manfred wrote:
> On 4/1/2022 5:19 PM, Malcolm McLean wrote:
> > On Friday, 1 April 2022 at 16:06:56 UTC+1, Kaz Kylheku wrote:
> >> On 2022-04-01, Malcolm McLean <malcolm.ar...@gmail.com> wrote:
> >>> I'm thinking of writing a routine to convert JSON to C structures in C source
> >>> code. (It would go in the Baby X resource compiler as the first use case).
> >>>
> >>> So it takes JSON and spits out a C source file with a structure declaration
> >>> and initial elements in a following definition.
> >>>
> >>> I can see some obvious difficulties, for instance JSON allows for arrays
> >>> of mixed types. Then a JSON object has an identier, but not a type name,
> >>> whilst C structures require type name.
> >>>
> >>> Anyone any ideas about how to proceed?
> >> C99 designated initializers handle unions
> >>
> >> jnode j0023 = { .type = jnode_num, .val = { .num = 3.14 ] };
> >> jnode j0024 = { .type = jnode_str, .val = { .str = "abc" ] };
> >> jnode j0025 = { .type = jnode_true };
> >> jnode j0026 = { .type = jnode_false };
> >>
> >> jnode *ja0027[4] = { &j0023, &j0024, &j0025, &j0026 };
> >>
> >> jnode j0027 = { .type = jnode_vec,
> >> .val = { .vec = { .data = ja0027, .len = 4 } } };
> >>
> >>
> >> where jnode is something like
> >>
> >> enum jnode_type { jnode_false, jnode_true, ..., jnode_num, ...}
> >>
> >> typedef struct jnode {
> >> enum jnode_type type;
> >> union {
> >> double num;
> >> char *str;
> >> struct {
> >> struct jnode *data;
> >> size_t len;
> >> } vec;
> >> } val;
> >> } jnode;
> >>
> > That's to build a JSON parser in C. I already have one of those.
> >
> > What I was thinking of was a program along the lines of
> >
> > input
> > {
> > "name" : "Fred Bloggs",
> > "salary": 32000.0,
> > "id": 1234,
> > "hours_worked" : [8, 8, 7, 8, 10]
> > }
> >
> > output
> >
> > typedef struct
> > {
> > char *name;
> > double salary;
> > int id;
> > int hours_worked[5];
> > } EMPLOYEE;
> >
> > EMPLOYEE fred {"Fred Blogs", 32000.0, 1234, 8, 8,7, 8, 10]};
> >
> > Obviously the first issue is that there is no way a computer can get from that JSON to "EMPLOYEE".
> Well, in fact there is, it's about dynamic types and introspection,
> which is easy for interpreted languages, but not as much in C.
>
> I believe one thing to consider is /how/ "EMPLOYEE" is going to be used
> in the rest of the code, and how the rest of the code is supposed to
> know about this type.
> Answers to these questions will probably shed light on how this type
> should be generated as well.
> > The other issue is that whilst the scalar fields are simple, the array fields start opening up more and
> > more difficulties.
> I believe this part is a different perspective on the same problem as
> the first one.
>
The Baby X resource compiler is basically a way of getting images into C programs, by
dumping them as RGBA quads in C source. However it does other things as well, for
example there's an audio resampler in there, and it can escape strings to create
text resources.
One thing someone might want to do is import non-image data. So the question is,
what format would that data come in, and in what format should it be output to
make it easy for someone writing a program to integrate the data into their
source code?
One format for data is JSON. A generic JSON structure with nodes would work, but it
wouldn't be easy to use. I want a flat array of C structures with easy to access fields.

Re: JSON to C converter

<20220401112323.169@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21057&group=comp.lang.c#21057

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Fri, 1 Apr 2022 18:40:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 182
Message-ID: <20220401112323.169@kylheku.com>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com>
<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
Injection-Date: Fri, 1 Apr 2022 18:40:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5b189a79a19927f2a80b70a11ec93a75";
logging-data="31376"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pFPqS0MgOcYm76IUSsFdjVJsKdBIe+XI="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:kgTVWZ5IwrJPc1tWv3CCqbMJdXo=
 by: Kaz Kylheku - Fri, 1 Apr 2022 18:40 UTC

On 2022-04-01, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> On Friday, 1 April 2022 at 16:06:56 UTC+1, Kaz Kylheku wrote:
>> On 2022-04-01, Malcolm McLean <malcolm.ar...@gmail.com> wrote:
>> > I'm thinking of writing a routine to convert JSON to C structures in C source
>> > code. (It would go in the Baby X resource compiler as the first use case).
>> >
>> > So it takes JSON and spits out a C source file with a structure declaration
>> > and initial elements in a following definition.
>> >
>> > I can see some obvious difficulties, for instance JSON allows for arrays
>> > of mixed types. Then a JSON object has an identier, but not a type name,
>> > whilst C structures require type name.
>> >
>> > Anyone any ideas about how to proceed?
>> C99 designated initializers handle unions
>>
>> jnode j0023 = { .type = jnode_num, .val = { .num = 3.14 ] };
>> jnode j0024 = { .type = jnode_str, .val = { .str = "abc" ] };
>> jnode j0025 = { .type = jnode_true };
>> jnode j0026 = { .type = jnode_false };
>>
>> jnode *ja0027[4] = { &j0023, &j0024, &j0025, &j0026 };
>>
>> jnode j0027 = { .type = jnode_vec,
>> .val = { .vec = { .data = ja0027, .len = 4 } } };
>>
>>
>> where jnode is something like
>>
>> enum jnode_type { jnode_false, jnode_true, ..., jnode_num, ...}
>>
>> typedef struct jnode {
>> enum jnode_type type;
>> union {
>> double num;
>> char *str;
>> struct {
>> struct jnode *data;
>> size_t len;
>> } vec;
>> } val;
>> } jnode;
>>
> That's to build a JSON parser in C. I already have one of those.

Well, no; the idea was to just convert a JSON data structure
to something statically declared in C, without there necessarily
being any parser in the program.

> What I was thinking of was a program along the lines of
>
> input
> {
> "name" : "Fred Bloggs",
> "salary": 32000.0,
> "id": 1234,
> "hours_worked" : [8, 8, 7, 8, 10]
> }

Right, so that just means JSON objects are becoming structs.

> output
>
> typedef struct
> {
> char *name;
> double salary;
> int id;
> int hours_worked[5];
> } EMPLOYEE;

I see, so a string or number can directly go to char * or double.

This EMPLOYEE identifier has to come from somewhere; the Json
has no such thing because objects are anonymous. You need a
convention like:

input
{
"class" : "EMPLOYEE"
...

}

and if that property is missing, then a machine-generated
name is used.

> EMPLOYEE fred {"Fred Blogs", 32000.0, 1234, 8, 8,7, 8, 10]};
>
> Obviously the first issue is that there is no way a computer can get
> from that JSON to "EMPLOYEE".

> The other issue is that whilst the
> scalar fields are simple, the array fields start opening up more and
> more difficulties.

JSON is dynamically typed; an array can have a mixtur eof any kind
of object. An array can contain a number, string, EMPLOYEE,
CUSTOMER, ... all at the same time.

That is why in my proposal, I have objects with type fields and
discriminated unions. The example shows an array being made of
heterogeneous objects.

One approach would be that the compiler for this adds all of the
structure names into the type enumeration.

Say that an EMPLOYEE and CUSTOMER types occur in the input.

Then:

enum jnode_type {
// the basic types
jnode_false, jnode_true, ..., jnode_num, jnode_vec,
// custom types ferreted out from data
jnode_EMPLOYEE,
jnode_CUSTOMER,
...
}

Then:

typedef struct jnode {
enum jnode_type type;
union {
double num;
char *str;

struct {
struct jnode *data;
size_t len;
} vec;

struct EMPLOYEE { // type == jnode_EMPLOYEE
char *name;
double salary;
int id;
int hours_worked[5];
} EMPLOYEE;

struct CUSTOMER { // type == jnode_CUSTOMER
...
} CUSTOMER;
} val;
} jnode;

Or these could be pointers, to keep the union small:

struct EMPLOYEE *EMPLOYEE; type == jnode_EMPLOYEE
struct CUSTOMER *CUSTOMER; type == jnode_CUSTOMER;

S then if you have a pointer to a node P, and you know that N->type is
jnode_EMPLOYEE, you can access P->val->EMPLOYEE.salary.

The hours_worked array could be int hours_worked[5] because the compiler
has identified that all the numbers which occur in the data for every
instance of employee are integers.

(The size of the array is the maximum across all the instances of
EMPLOYEE, and there should be a machine-generated size field for the
actual number present in the given instance, like n_hours_worked.)

If the compiler detects that the array is heterogeneous, then it
emits something else;

struct EMPLOYEE {
char *name;
double salary;
int id;
jnode hours_worked; // vector
} EMPLOYEE;

hours_worked becomes a jnode of type jnode_vec, and the code
has to use more complicated access to get into it.

(Since writing the code which accesses this structure is left
as an exercise to the application, this could be knocked off
in an afternoon.)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: JSON to C converter

<87y20oh6ud.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21058&group=comp.lang.c#21058

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Fri, 01 Apr 2022 21:44:10 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <87y20oh6ud.fsf@bsb.me.uk>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com>
<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="77aef27c919b9e1eca883ad10389548a";
logging-data="3986"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196ff70/xfnv4CVpkHY3+jiA8jWux2cC3c="
Cancel-Lock: sha1:kGm3fAbKkbIg9f+C+i4sSXoh+Kk=
sha1:ArVqL2SpUCJFTyhOzDZUQn4Emlo=
X-BSB-Auth: 1.69571537da8b7faa3b33.20220401214410BST.87y20oh6ud.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 1 Apr 2022 20:44 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:

> What I was thinking of was a program along the lines of
>
> input
> {
> "name" : "Fred Bloggs",
> "salary": 32000.0,
> "id": 1234,
> "hours_worked" : [8, 8, 7, 8, 10]
> }
>
> output
>
> typedef struct
> {
> char *name;
> double salary;
> int id;
> int hours_worked[5];
> } EMPLOYEE;

There a small wrinkle here. JSON has only number values, not int and
double, so unless the JSON is coming from somewhere you can influence
(for example a user who is given a convention to use) you won't be able
to tell that salary is a double and id is an int.

--
Ben.

Re: JSON to C converter

<97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21059&group=comp.lang.c#21059

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:57d5:0:b0:2e0:70a8:4419 with SMTP id w21-20020ac857d5000000b002e070a84419mr10206739qta.257.1648851983733;
Fri, 01 Apr 2022 15:26:23 -0700 (PDT)
X-Received: by 2002:a05:622a:303:b0:2e0:6833:24f3 with SMTP id
q3-20020a05622a030300b002e0683324f3mr10239842qtw.404.1648851983606; Fri, 01
Apr 2022 15:26:23 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Apr 2022 15:26:23 -0700 (PDT)
In-Reply-To: <87y20oh6ud.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:141c:ffc2:70ac:e5f2;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:141c:ffc2:70ac:e5f2
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com> <05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<87y20oh6ud.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Fri, 01 Apr 2022 22:26:23 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 33
 by: Malcolm McLean - Fri, 1 Apr 2022 22:26 UTC

On Friday, 1 April 2022 at 21:44:24 UTC+1, Ben Bacarisse wrote:
> Malcolm McLean <malcolm.ar...@gmail.com> writes:
>
> > What I was thinking of was a program along the lines of
> >
> > input
> > {
> > "name" : "Fred Bloggs",
> > "salary": 32000.0,
> > "id": 1234,
> > "hours_worked" : [8, 8, 7, 8, 10]
> > }
> >
> > output
> >
> > typedef struct
> > {
> > char *name;
> > double salary;
> > int id;
> > int hours_worked[5];
> > } EMPLOYEE;
> There a small wrinkle here. JSON has only number values, not int and
> double, so unless the JSON is coming from somewhere you can influence
> (for example a user who is given a convention to use) you won't be able
> to tell that salary is a double and id is an int.
>
If you've only got a few values, it's impossible to tell. But if you've got a large
number of values, and they are all written without decimal points, then you
can be fairly sure that the field is inherently integral.
The Baby X resource compiler takes an XML file which lists the input files.
It can be used to supply data that can't be inferred automatically.
However the feature needs to be lightly used. If the XML becomes too
complicated, it starts to defeat the point.

Re: JSON to C converter

<87sfqwgx50.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21060&group=comp.lang.c#21060

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Sat, 02 Apr 2022 01:13:47 +0100
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <87sfqwgx50.fsf@bsb.me.uk>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com>
<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<87y20oh6ud.fsf@bsb.me.uk>
<97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="2e1f6aa93b0d51b1bbafc7b427b9200a";
logging-data="7075"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+iG8mxBEZL1ez6CZ6GNcIQ+U+9/OSgK5I="
Cancel-Lock: sha1:IQBlbgM4bDQ5wqfWogFu9ucqwDg=
sha1:GnEY7nc77AnrlCQ+gmHFuft2yL8=
X-BSB-Auth: 1.06722b6f6608a1bb8f71.20220402011347BST.87sfqwgx50.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 2 Apr 2022 00:13 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:

> On Friday, 1 April 2022 at 21:44:24 UTC+1, Ben Bacarisse wrote:
>> Malcolm McLean <malcolm.ar...@gmail.com> writes:
>>
>> > What I was thinking of was a program along the lines of
>> >
>> > input
>> > {
>> > "name" : "Fred Bloggs",
>> > "salary": 32000.0,
>> > "id": 1234,
>> > "hours_worked" : [8, 8, 7, 8, 10]
>> > }
>> >
>> > output
>> >
>> > typedef struct
>> > {
>> > char *name;
>> > double salary;
>> > int id;
>> > int hours_worked[5];
>> > } EMPLOYEE;
>> There a small wrinkle here. JSON has only number values, not int and
>> double, so unless the JSON is coming from somewhere you can influence
>> (for example a user who is given a convention to use) you won't be able
>> to tell that salary is a double and id is an int.
>>
> If you've only got a few values, it's impossible to tell. But if
> you've got a large number of values, and they are all written without
> decimal points, then you can be fairly sure that the field is
> inherently integral.

That'll often work, but your example wanted double for salary. My
annual salary never had fractional pounds.

> The Baby X resource compiler takes an XML file
> which lists the input files. It can be used to supply data that can't
> be inferred automatically. However the feature needs to be lightly
> used. If the XML becomes too complicated, it starts to defeat the
> point.

--
Ben.

Re: JSON to C converter

<5506af78-3e53-465f-a4a3-8c9598b2dcb5n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21062&group=comp.lang.c#21062

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:5cc3:0:b0:441:1959:cb45 with SMTP id iu3-20020ad45cc3000000b004411959cb45mr10374652qvb.93.1648868294989;
Fri, 01 Apr 2022 19:58:14 -0700 (PDT)
X-Received: by 2002:a05:622a:60f:b0:2e2:750:ce24 with SMTP id
z15-20020a05622a060f00b002e20750ce24mr10983643qta.315.1648868294869; Fri, 01
Apr 2022 19:58:14 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Apr 2022 19:58:14 -0700 (PDT)
In-Reply-To: <97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=145.14.29.114; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 145.14.29.114
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com> <05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<87y20oh6ud.fsf@bsb.me.uk> <97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5506af78-3e53-465f-a4a3-8c9598b2dcb5n@googlegroups.com>
Subject: Re: JSON to C converter
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Sat, 02 Apr 2022 02:58:14 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 46
 by: Öö Tiib - Sat, 2 Apr 2022 02:58 UTC

On Saturday, 2 April 2022 at 01:26:31 UTC+3, Malcolm McLean wrote:
> On Friday, 1 April 2022 at 21:44:24 UTC+1, Ben Bacarisse wrote:
> > Malcolm McLean <malcolm.ar...@gmail.com> writes:
> >
> > > What I was thinking of was a program along the lines of
> > >
> > > input
> > > {
> > > "name" : "Fred Bloggs",
> > > "salary": 32000.0,
> > > "id": 1234,
> > > "hours_worked" : [8, 8, 7, 8, 10]
> > > }
> > >
> > > output
> > >
> > > typedef struct
> > > {
> > > char *name;
> > > double salary;
> > > int id;
> > > int hours_worked[5];
> > > } EMPLOYEE;
> > There a small wrinkle here. JSON has only number values, not int and
> > double, so unless the JSON is coming from somewhere you can influence
> > (for example a user who is given a convention to use) you won't be able
> > to tell that salary is a double and id is an int.
> >
> If you've only got a few values, it's impossible to tell. But if you've got a large
> number of values, and they are all written without decimal points, then you
> can be fairly sure that the field is inherently integral.
> The Baby X resource compiler takes an XML file which lists the input files.
> It can be used to supply data that can't be inferred automatically.
> However the feature needs to be lightly used. If the XML becomes too
> complicated, it starts to defeat the point.

Real data always comes from somewhere and so conforms to some
kind of schema of layout that is specified by that source of data.
That specification is usually simple to code as C, and then data
file contents are also usually simple to transform into values of
that C program.

JSON has loose rules for to keep it terse. That does not bother us
when we know the schema but makes it impossible to guess the
schema automatically on general case. For example in specification
it is easy to say that when member "unit" is missing from JSON
object then it is "meter". How you guess that from data?

Re: JSON to C converter

<t29vtt$1l4h$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21066&group=comp.lang.c#21066

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Sat, 2 Apr 2022 19:08:13 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t29vtt$1l4h$1@gioia.aioe.org>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com>
<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<t278b8$1jmr$1@gioia.aioe.org>
<af4d3020-c7e0-4383-85f3-f7b6a845a1d2n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="54417"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Sat, 2 Apr 2022 17:08 UTC

On 4/1/2022 6:36 PM, Malcolm McLean wrote:
> On Friday, 1 April 2022 at 17:13:44 UTC+1, Manfred wrote:
>> On 4/1/2022 5:19 PM, Malcolm McLean wrote:
>>> On Friday, 1 April 2022 at 16:06:56 UTC+1, Kaz Kylheku wrote:
>>>> On 2022-04-01, Malcolm McLean <malcolm.ar...@gmail.com> wrote:
>>>>> I'm thinking of writing a routine to convert JSON to C structures in C source
>>>>> code. (It would go in the Baby X resource compiler as the first use case).
>>>>>
>>>>> So it takes JSON and spits out a C source file with a structure declaration
>>>>> and initial elements in a following definition.
>>>>>
>>>>> I can see some obvious difficulties, for instance JSON allows for arrays
>>>>> of mixed types. Then a JSON object has an identier, but not a type name,
>>>>> whilst C structures require type name.
>>>>>
>>>>> Anyone any ideas about how to proceed?
>>>> C99 designated initializers handle unions
>>>>
>>>> jnode j0023 = { .type = jnode_num, .val = { .num = 3.14 ] };
>>>> jnode j0024 = { .type = jnode_str, .val = { .str = "abc" ] };
>>>> jnode j0025 = { .type = jnode_true };
>>>> jnode j0026 = { .type = jnode_false };
>>>>
>>>> jnode *ja0027[4] = { &j0023, &j0024, &j0025, &j0026 };
>>>>
>>>> jnode j0027 = { .type = jnode_vec,
>>>> .val = { .vec = { .data = ja0027, .len = 4 } } };
>>>>
>>>>
>>>> where jnode is something like
>>>>
>>>> enum jnode_type { jnode_false, jnode_true, ..., jnode_num, ...}
>>>>
>>>> typedef struct jnode {
>>>> enum jnode_type type;
>>>> union {
>>>> double num;
>>>> char *str;
>>>> struct {
>>>> struct jnode *data;
>>>> size_t len;
>>>> } vec;
>>>> } val;
>>>> } jnode;
>>>>
>>> That's to build a JSON parser in C. I already have one of those.
>>>
>>> What I was thinking of was a program along the lines of
>>>
>>> input
>>> {
>>> "name" : "Fred Bloggs",
>>> "salary": 32000.0,
>>> "id": 1234,
>>> "hours_worked" : [8, 8, 7, 8, 10]
>>> }
>>>
>>> output
>>>
>>> typedef struct
>>> {
>>> char *name;
>>> double salary;
>>> int id;
>>> int hours_worked[5];
>>> } EMPLOYEE;
>>>
>>> EMPLOYEE fred {"Fred Blogs", 32000.0, 1234, 8, 8,7, 8, 10]};
>>>
>>> Obviously the first issue is that there is no way a computer can get from that JSON to "EMPLOYEE".
>> Well, in fact there is, it's about dynamic types and introspection,
>> which is easy for interpreted languages, but not as much in C.
>>
>> I believe one thing to consider is /how/ "EMPLOYEE" is going to be used
>> in the rest of the code, and how the rest of the code is supposed to
>> know about this type.
>> Answers to these questions will probably shed light on how this type
>> should be generated as well.
>>> The other issue is that whilst the scalar fields are simple, the array fields start opening up more and
>>> more difficulties.
>> I believe this part is a different perspective on the same problem as
>> the first one.
>>
> The Baby X resource compiler is basically a way of getting images into C programs, by
> dumping them as RGBA quads in C source. However it does other things as well, for
> example there's an audio resampler in there, and it can escape strings to create
> text resources.
> One thing someone might want to do is import non-image data. So the question is,
> what format would that data come in, and in what format should it be output to
> make it easy for someone writing a program to integrate the data into their
> source code?
> One format for data is JSON. A generic JSON structure with nodes would work, but it
> wouldn't be easy to use. I want a flat array of C structures with easy to access fields.

So, resource compiler. Which means out of line processing of data. Also,
the user of such compiled types is the application developer, not some
code framework. In this context, probably Kaz's suggestion elsethread
would do (or at least I can't find a better solution at this time)

Re: JSON to C converter

<60bd3582-c7ec-43e2-b7d7-132474ef0632n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21069&group=comp.lang.c#21069

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:3184:b0:67d:cce9:bab4 with SMTP id bi4-20020a05620a318400b0067dcce9bab4mr11095651qkb.685.1648980375835;
Sun, 03 Apr 2022 03:06:15 -0700 (PDT)
X-Received: by 2002:a37:9b84:0:b0:699:9200:fc94 with SMTP id
d126-20020a379b84000000b006999200fc94mr9132198qke.94.1648980375695; Sun, 03
Apr 2022 03:06:15 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 3 Apr 2022 03:06:15 -0700 (PDT)
In-Reply-To: <87sfqwgx50.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:6965:dfa3:7a76:e0e1;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:6965:dfa3:7a76:e0e1
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com> <05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<87y20oh6ud.fsf@bsb.me.uk> <97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>
<87sfqwgx50.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <60bd3582-c7ec-43e2-b7d7-132474ef0632n@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sun, 03 Apr 2022 10:06:15 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 41
 by: Malcolm McLean - Sun, 3 Apr 2022 10:06 UTC

On Saturday, 2 April 2022 at 01:14:00 UTC+1, Ben Bacarisse wrote:
> Malcolm McLean <malcolm.ar...@gmail.com> writes:
>
> > On Friday, 1 April 2022 at 21:44:24 UTC+1, Ben Bacarisse wrote:
> >> Malcolm McLean <malcolm.ar...@gmail.com> writes:
> >>
> >> > What I was thinking of was a program along the lines of
> >> >
> >> > input
> >> > {
> >> > "name" : "Fred Bloggs",
> >> > "salary": 32000.0,
> >> > "id": 1234,
> >> > "hours_worked" : [8, 8, 7, 8, 10]
> >> > }
> >> >
> >> > output
> >> >
> >> > typedef struct
> >> > {
> >> > char *name;
> >> > double salary;
> >> > int id;
> >> > int hours_worked[5];
> >> > } EMPLOYEE;
> >> There a small wrinkle here. JSON has only number values, not int and
> >> double, so unless the JSON is coming from somewhere you can influence
> >> (for example a user who is given a convention to use) you won't be able
> >> to tell that salary is a double and id is an int.
> >>
> > If you've only got a few values, it's impossible to tell. But if
> > you've got a large number of values, and they are all written without
> > decimal points, then you can be fairly sure that the field is
> > inherently integral.
> That'll often work, but your example wanted double for salary. My
> annual salary never had fractional pounds.
>
My CSV file parser solves this problem by returning all numerical fields as
doubles. But C programmers expect values that are constrained to be integers
to be an integer type. And C won't allow you to use a floating point type as
an array index.
So there isn't a good answer.

Re: JSON to C converter

<87y20me5k2.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21071&group=comp.lang.c#21071

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Sun, 03 Apr 2022 13:04:45 +0100
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <87y20me5k2.fsf@bsb.me.uk>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<20220401075645.437@kylheku.com>
<05926cb4-1371-4f58-a9a9-83a6f6ad31f9n@googlegroups.com>
<87y20oh6ud.fsf@bsb.me.uk>
<97ed523f-a6f9-4c82-92ed-417004808a6en@googlegroups.com>
<87sfqwgx50.fsf@bsb.me.uk>
<60bd3582-c7ec-43e2-b7d7-132474ef0632n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="dd353e8412b478f41f47bc1049a7123e";
logging-data="30958"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CYN9GDPryROmY4smi17Bij/4N/9Rq5jw="
Cancel-Lock: sha1:E+DoZYKpBnKyBneYe9LmZJ9m66s=
sha1:sZQLFkaetKqCrP1mq8KBTCeubM8=
X-BSB-Auth: 1.89cc8374396edac73b9c.20220403130445BST.87y20me5k2.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 3 Apr 2022 12:04 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:

> On Saturday, 2 April 2022 at 01:14:00 UTC+1, Ben Bacarisse wrote:
>> Malcolm McLean <malcolm.ar...@gmail.com> writes:
>>
>> > On Friday, 1 April 2022 at 21:44:24 UTC+1, Ben Bacarisse wrote:
>> >> Malcolm McLean <malcolm.ar...@gmail.com> writes:
>> >>
>> >> > What I was thinking of was a program along the lines of
>> >> >
>> >> > input
>> >> > {
>> >> > "name" : "Fred Bloggs",
>> >> > "salary": 32000.0,
>> >> > "id": 1234,
>> >> > "hours_worked" : [8, 8, 7, 8, 10]
>> >> > }
>> >> >
>> >> > output
>> >> >
>> >> > typedef struct
>> >> > {
>> >> > char *name;
>> >> > double salary;
>> >> > int id;
>> >> > int hours_worked[5];
>> >> > } EMPLOYEE;
>> >> There a small wrinkle here. JSON has only number values, not int and
>> >> double, so unless the JSON is coming from somewhere you can influence
>> >> (for example a user who is given a convention to use) you won't be able
>> >> to tell that salary is a double and id is an int.
>> >>
>> > If you've only got a few values, it's impossible to tell. But if
>> > you've got a large number of values, and they are all written without
>> > decimal points, then you can be fairly sure that the field is
>> > inherently integral.
>>
>> That'll often work, but your example wanted double for salary. My
>> annual salary never had fractional pounds.
>>
> My CSV file parser solves this problem by returning all numerical fields as
> doubles. But C programmers expect values that are constrained to be integers
> to be an integer type. And C won't allow you to use a floating point type as
> an array index.
> So there isn't a good answer.

I'd use a richer syntax than JSON. The last time I did something
similar (more than 30 years go mind) there was no JSON so I had to
invent my own. With just a bit more info you can distinguish numeric
types (the syntax forced doubles to have either a . or an exponent) and
also distinguish between char * and char [] fields. Of course, if your
data comes from somewhere else that uses JSON, you are stuck with JSON.

--
Ben.

Re: JSON to C converter

<t2ckhp$f3r$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21072&group=comp.lang.c#21072

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Sun, 3 Apr 2022 19:12:25 +0200
Organization: A noiseless patient Spider
Lines: 3
Message-ID: <t2ckhp$f3r$1@dont-email.me>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 3 Apr 2022 17:12:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="29084fe68ed57ad676244da3ecc45ffc";
logging-data="15483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+DXQnQENG7mQ/BWq/zTkVBb1osVlAUXE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:WAMSHo3gDYwjA/XdPxlTIupR59A=
In-Reply-To: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sun, 3 Apr 2022 17:12 UTC

No one needs a static JSON to C data structure converter.
People need dyanmic converters that convert the data
structures at runtime.

Re: JSON to C converter

<eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21073&group=comp.lang.c#21073

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ae9:ed8a:0:b0:67d:6a0d:2a82 with SMTP id c132-20020ae9ed8a000000b0067d6a0d2a82mr12535714qkg.561.1649019694421;
Sun, 03 Apr 2022 14:01:34 -0700 (PDT)
X-Received: by 2002:a05:6214:cc3:b0:440:f5fc:f1ab with SMTP id
3-20020a0562140cc300b00440f5fcf1abmr14623325qvx.59.1649019694227; Sun, 03 Apr
2022 14:01:34 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 3 Apr 2022 14:01:34 -0700 (PDT)
In-Reply-To: <t2ckhp$f3r$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:6965:dfa3:7a76:e0e1;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:6965:dfa3:7a76:e0e1
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com> <t2ckhp$f3r$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sun, 03 Apr 2022 21:01:34 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 11
 by: Malcolm McLean - Sun, 3 Apr 2022 21:01 UTC

On Sunday, 3 April 2022 at 18:12:39 UTC+1, Bonita Montero wrote:
> No one needs a static JSON to C data structure converter.
> People need dyanmic converters that convert the data
> structures at runtime.
>
I already have a JSON parser to accept JSON at runtime and extract
the data as C native variables.
The Baby X resource compiler takes in data and converts it to compileable
C source. But there might not really be a use case for JSON. Or it might
be too difficult to specify all the additional information needed to convert the
JSON to acceptable C.
Scratching the idea is a definite possibility.

Re: JSON to C converter

<t2hf6n$67p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21084&group=comp.lang.c#21084

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Tue, 5 Apr 2022 15:11:54 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <t2hf6n$67p$1@dont-email.me>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me>
<eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Apr 2022 13:11:51 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d194222113956307730aabfa96558b54";
logging-data="6393"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186OIP5sauIEGNC9SoJ78rKL8oBEFz1oA4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:mIY8PzQATjcCyLvBeVQXL5x4Sr8=
In-Reply-To: <eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Tue, 5 Apr 2022 13:11 UTC

Am 03.04.2022 um 23:01 schrieb Malcolm McLean:

> But there might not really be a use case for JSON.

There are many usecases for JSON.
JSON often replaces XML because it's simpler
(look at this XML Schema bloat).

Re: JSON to C converter

<2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21085&group=comp.lang.c#21085

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:3184:b0:67d:cce9:bab4 with SMTP id bi4-20020a05620a318400b0067dcce9bab4mr2122471qkb.685.1649164954711;
Tue, 05 Apr 2022 06:22:34 -0700 (PDT)
X-Received: by 2002:a37:ad0f:0:b0:5f1:70cc:362 with SMTP id
f15-20020a37ad0f000000b005f170cc0362mr2186241qkm.187.1649164954495; Tue, 05
Apr 2022 06:22:34 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 5 Apr 2022 06:22:34 -0700 (PDT)
In-Reply-To: <t2hf6n$67p$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:48e0:56e6:ae6a:5091;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:48e0:56e6:ae6a:5091
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me> <eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Tue, 05 Apr 2022 13:22:34 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 12
 by: Malcolm McLean - Tue, 5 Apr 2022 13:22 UTC

On Tuesday, 5 April 2022 at 14:12:05 UTC+1, Bonita Montero wrote:
> Am 03.04.2022 um 23:01 schrieb Malcolm McLean:
>
> > But there might not really be a use case for JSON.
> There are many usecases for JSON.
> JSON often replaces XML because it's simpler
> (look at this XML Schema bloat).
>
JSON has been accepted as a format for data exchange.

The question is whether there is a use case for it in a C source generator.
Would people take data in in JSON and want it converted to compileable
C structures?

Re: JSON to C converter

<t2hkl8$9nv$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21086&group=comp.lang.c#21086

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Tue, 5 Apr 2022 16:44:59 +0200
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <t2hkl8$9nv$1@dont-email.me>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me>
<eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me>
<2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Apr 2022 14:44:56 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d194222113956307730aabfa96558b54";
logging-data="9983"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/S2jofU1MC2kJ2J4dw9oedWNJFFB19tzg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:COkIc829rNN8t10cVJXfh0Ev23s=
In-Reply-To: <2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Tue, 5 Apr 2022 14:44 UTC

Am 05.04.2022 um 15:22 schrieb Malcolm McLean:
> On Tuesday, 5 April 2022 at 14:12:05 UTC+1, Bonita Montero wrote:
>> Am 03.04.2022 um 23:01 schrieb Malcolm McLean:
>>
>>> But there might not really be a use case for JSON.
>> There are many usecases for JSON.
>> JSON often replaces XML because it's simpler
>> (look at this XML Schema bloat).
>>
> JSON has been accepted as a format for data exchange.
>
> The question is whether there is a use case for it in a C source generator.
> Would people take data in in JSON and want it converted to compileable
> C structures?

No, and that's while I'm asking who needs such a converter.

Re: JSON to C converter

<82a21f6f-af69-43a7-91da-092ad9ddb6c3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21087&group=comp.lang.c#21087

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:8e8f:0:b0:443:d25b:4e21 with SMTP id x15-20020a0c8e8f000000b00443d25b4e21mr3075178qvb.128.1649170989348;
Tue, 05 Apr 2022 08:03:09 -0700 (PDT)
X-Received: by 2002:a05:6214:769:b0:443:e16b:52aa with SMTP id
f9-20020a056214076900b00443e16b52aamr3376637qvz.3.1649170988725; Tue, 05 Apr
2022 08:03:08 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 5 Apr 2022 08:03:08 -0700 (PDT)
In-Reply-To: <t2hkl8$9nv$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:8c64:15f2:559e:a52;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:8c64:15f2:559e:a52
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me> <eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me> <2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
<t2hkl8$9nv$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <82a21f6f-af69-43a7-91da-092ad9ddb6c3n@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Tue, 05 Apr 2022 15:03:09 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 14
 by: Malcolm McLean - Tue, 5 Apr 2022 15:03 UTC

On Tuesday, 5 April 2022 at 15:45:14 UTC+1, Bonita Montero wrote:
> Am 05.04.2022 um 15:22 schrieb Malcolm McLean:
>
> > The question is whether there is a use case for it in a C source generator.
> > Would people take data in in JSON and want it converted to compileable
> > C structures?
> No, and that's while I'm asking who needs such a converter.
>
You often want to hardcode data rather than reading it in from a file. In
the embedded world, there might be no filesystem from which you can
read in the data. In the hosted world, it often makes the executable
easier to distribute if it doesn't depend on external data files for support,
and it's more robust.
Theres definitely a case for facilities to package image files. The question
uis whether anyone would actually use a similar facility for JSON.

Re: JSON to C converter

<t2hn19$4ju$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21088&group=comp.lang.c#21088

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Tue, 5 Apr 2022 16:25:29 +0100
Organization: A noiseless patient Spider
Lines: 132
Message-ID: <t2hn19$4ju$1@dont-email.me>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me>
<eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me>
<2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Apr 2022 15:25:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d19fadca357526e47ec216a3a67eefd2";
logging-data="4734"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Zzzn5fv58NiyoV1mPD6YEuCA90MR0hQo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:cWyc9ryOuO6aSg8o+48V3vnhHNw=
In-Reply-To: <2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
 by: Bart - Tue, 5 Apr 2022 15:25 UTC

On 05/04/2022 14:22, Malcolm McLean wrote:
> On Tuesday, 5 April 2022 at 14:12:05 UTC+1, Bonita Montero wrote:
>> Am 03.04.2022 um 23:01 schrieb Malcolm McLean:
>>
>>> But there might not really be a use case for JSON.
>> There are many usecases for JSON.
>> JSON often replaces XML because it's simpler
>> (look at this XML Schema bloat).
>>
> JSON has been accepted as a format for data exchange.
>
> The question is whether there is a use case for it in a C source generator.
> Would people take data in in JSON and want it converted to compileable
> C structures?

To take an arbitrary JSON and produce a C source file with hard-coded
struct types is problematic.

AIUI, JSON contains two structuring mechanisms:

[...] arrays of mixed/arbitrary types
{...} objects of string:value pairs

So the first problem with turning this into C, is that each element of
the array may be a number, bool, string, object or nested array.

The next, if intending to turn {...} into a struct, is that each {...}
object could represent a different struct. Maybe they represent the same
record, but it is up to the translator to decide if two objects are the
same struct or not:

{ "age":10, "name":"bart", "colour":0xFFFF00}
{ "name":lisa, "age":8}

?

It would be very easy to end up with 1000s of different struct types.

You need to step back and look at a selection of actual use-cases to see
whether such a tool as you propose could do anything useful, and what
extra data or meta-data would be needed.

For example, you can take a C program containing an initialised array of
structs, and write some code to serialise that data into a JSON file.

Then you know that that data /could/ be turned back into C (which
defines a populated array of structs) using a suitable tool, but it
might need some extra hints.

Once that's done, you can have an application capable of writing out
data via JSON, and reading it back in. But it will not cope with any
random JSON file downloaded from somewhere.

If I take this example from Wikipedia (where I got my recently acquired
knowledge):

{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}

This is data that can be represented within any scripting language (I
believe JSON format is supposed to be a subset of JavaScript).

I can even paste that into my own scripting language, and run it after
making these substitutions:

{...} => [...] A dict constructor when key:value pairs present
[...] => (...) A list constructor
null => nil

data:=
[
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": [
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
],
"phoneNumbers": (
[
"type": "home",
"number": "212 555-1234"
],
[
"type": "office",
"number": "646 555-4567"
]
),
"children": (),
"spouse": nil
]

println data

This outputs (unformatted):

[age:27,children:(),phoneNumbers:([type:home,number:212
555-1234],[type:office,number:646 555-4567])
,firstName:John,spouse:nil,address:[state:NY,postalCode:10021-3100,city:New
York,streetAddress:21 2n
d Street],isAlive:1,lastName:Smith]

The point is, JSON format suits dynamic, arbitrarily structured data. It
doesn't suit the fixed data of a C program, unless you specifically
write a C program that represents arbitrary data. Which means not
hard-coding structs for that data.

Re: JSON to C converter

<892145f9-1fb1-4b48-aaf2-aa2f5ba6666fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21089&group=comp.lang.c#21089

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:24f:b0:2e1:d658:a595 with SMTP id c15-20020a05622a024f00b002e1d658a595mr3533961qtx.657.1649173369655;
Tue, 05 Apr 2022 08:42:49 -0700 (PDT)
X-Received: by 2002:a05:620a:c4b:b0:67e:11a2:7cfb with SMTP id
u11-20020a05620a0c4b00b0067e11a27cfbmr2585546qki.9.1649173369409; Tue, 05 Apr
2022 08:42:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 5 Apr 2022 08:42:49 -0700 (PDT)
In-Reply-To: <t2hn19$4ju$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:8c64:15f2:559e:a52;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:8c64:15f2:559e:a52
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me> <eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me> <2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
<t2hn19$4ju$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <892145f9-1fb1-4b48-aaf2-aa2f5ba6666fn@googlegroups.com>
Subject: Re: JSON to C converter
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Tue, 05 Apr 2022 15:42:49 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 148
 by: Malcolm McLean - Tue, 5 Apr 2022 15:42 UTC

On Tuesday, 5 April 2022 at 16:25:43 UTC+1, Bart wrote:
> On 05/04/2022 14:22, Malcolm McLean wrote:
> > On Tuesday, 5 April 2022 at 14:12:05 UTC+1, Bonita Montero wrote:
> >> Am 03.04.2022 um 23:01 schrieb Malcolm McLean:
> >>
> >>> But there might not really be a use case for JSON.
> >> There are many usecases for JSON.
> >> JSON often replaces XML because it's simpler
> >> (look at this XML Schema bloat).
> >>
> > JSON has been accepted as a format for data exchange.
> >
> > The question is whether there is a use case for it in a C source generator.
> > Would people take data in in JSON and want it converted to compileable
> > C structures?
> To take an arbitrary JSON and produce a C source file with hard-coded
> struct types is problematic.
>
> AIUI, JSON contains two structuring mechanisms:
>
> [...] arrays of mixed/arbitrary types
> {...} objects of string:value pairs
>
> So the first problem with turning this into C, is that each element of
> the array may be a number, bool, string, object or nested array.
>
> The next, if intending to turn {...} into a struct, is that each {...}
> object could represent a different struct. Maybe they represent the same
> record, but it is up to the translator to decide if two objects are the
> same struct or not:
>
> { "age":10, "name":"bart", "colour":0xFFFF00}
> { "name":lisa, "age":8}
>
> ?
>
> It would be very easy to end up with 1000s of different struct types.
>
> You need to step back and look at a selection of actual use-cases to see
> whether such a tool as you propose could do anything useful, and what
> extra data or meta-data would be needed.
>
> For example, you can take a C program containing an initialised array of
> structs, and write some code to serialise that data into a JSON file.
>
> Then you know that that data /could/ be turned back into C (which
> defines a populated array of structs) using a suitable tool, but it
> might need some extra hints.
>
> Once that's done, you can have an application capable of writing out
> data via JSON, and reading it back in. But it will not cope with any
> random JSON file downloaded from somewhere.
>
>
> If I take this example from Wikipedia (where I got my recently acquired
> knowledge):
>
> {
> "firstName": "John",
> "lastName": "Smith",
> "isAlive": true,
> "age": 27,
> "address": {
> "streetAddress": "21 2nd Street",
> "city": "New York",
> "state": "NY",
> "postalCode": "10021-3100"
> },
> "phoneNumbers": [
> {
> "type": "home",
> "number": "212 555-1234"
> },
> {
> "type": "office",
> "number": "646 555-4567"
> }
> ],
> "children": [],
> "spouse": null
> }
>
> This is data that can be represented within any scripting language (I
> believe JSON format is supposed to be a subset of JavaScript).
>
> I can even paste that into my own scripting language, and run it after
> making these substitutions:
>
> {...} => [...] A dict constructor when key:value pairs present
> [...] => (...) A list constructor
> null => nil
>
> data:=
> [
> "firstName": "John",
> "lastName": "Smith",
> "isAlive": true,
> "age": 27,
> "address": [
> "streetAddress": "21 2nd Street",
> "city": "New York",
> "state": "NY",
> "postalCode": "10021-3100"
> ],
> "phoneNumbers": (
> [
> "type": "home",
> "number": "212 555-1234"
> ],
> [
> "type": "office",
> "number": "646 555-4567"
> ]
> ),
> "children": (),
> "spouse": nil
> ]
>
> println data
>
> This outputs (unformatted):
>
> [age:27,children:(),phoneNumbers:([type:home,number:212
> 555-1234],[type:office,number:646 555-4567])
> ,firstName:John,spouse:nil,address:[state:NY,postalCode:10021-3100,city:New
> York,streetAddress:21 2n
> d Street],isAlive:1,lastName:Smith]
>
> The point is, JSON format suits dynamic, arbitrarily structured data. It
> doesn't suit the fixed data of a C program, unless you specifically
> write a C program that represents arbitrary data. Which means not
> hard-coding structs for that data.
>
I've already got a JSON parser. So I can take in a JSON string from an external
file, or hardcode it as a string, then query it for the keys and datatype, and
extract the data.
But it's rare to really need that flexibility. In my real life use of JSON, I use
JSON to store user settings. So I know the keys and the type of data they
hold beforehand. I just set a field to a default value if a key is missing or
holds invalid data.

An example use case might be a football management game. The data for the
players is held in some editing tool which stores the players' statistics and
history as a big array of JSON objects. You don't want the data to be visible
to the user in the final product, so you hardcode it to a C array. You know what
the keys are when you write the main program, because it has to access
fields of the struct.
The question is whether the JSON to C converter can be generic, or if it has
to be bespoke.

Re: JSON to C converter

<t2hrd0$6fe$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21090&group=comp.lang.c#21090

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Tue, 5 Apr 2022 18:40:02 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <t2hrd0$6fe$1@dont-email.me>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me>
<eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me>
<2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
<t2hkl8$9nv$1@dont-email.me>
<82a21f6f-af69-43a7-91da-092ad9ddb6c3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Apr 2022 16:40:00 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d194222113956307730aabfa96558b54";
logging-data="6638"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aJ8mClpULGE8tnWibNUBiOxjgne9HYL8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:fXvc4GWM4tvUwohDlUPi2mYVz5g=
In-Reply-To: <82a21f6f-af69-43a7-91da-092ad9ddb6c3n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Tue, 5 Apr 2022 16:40 UTC

Am 05.04.2022 um 17:03 schrieb Malcolm McLean:
> On Tuesday, 5 April 2022 at 15:45:14 UTC+1, Bonita Montero wrote:
>> Am 05.04.2022 um 15:22 schrieb Malcolm McLean:
>>
>>> The question is whether there is a use case for it in a C source generator.
>>> Would people take data in in JSON and want it converted to compileable
>>> C structures?
>> No, and that's while I'm asking who needs such a converter.
>>
> You often want to hardcode data rather than reading it in from a file.

And you never have JSON as a raw material for that.
Rest from you text unread.

Re: JSON to C converter

<20220405102653.611@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21091&group=comp.lang.c#21091

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: JSON to C converter
Date: Tue, 5 Apr 2022 17:27:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <20220405102653.611@kylheku.com>
References: <8d514df1-31eb-42ae-a1e1-d204b98d89b0n@googlegroups.com>
<t2ckhp$f3r$1@dont-email.me>
<eb21bdb6-6254-4c4d-a9a4-ad44cc1b103en@googlegroups.com>
<t2hf6n$67p$1@dont-email.me>
<2957a118-1c20-4eec-8e9a-f5950b74a331n@googlegroups.com>
Injection-Date: Tue, 5 Apr 2022 17:27:35 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="04264a35568fb1dc6a5e5084fa1ad61b";
logging-data="31484"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+f/VGoK0EqrtdWLDHpL1otzyJs0mavBlM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:ovTgu4XL3Muicv+X9MIADaKgwt0=
 by: Kaz Kylheku - Tue, 5 Apr 2022 17:27 UTC

On 2022-04-05, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> On Tuesday, 5 April 2022 at 14:12:05 UTC+1, Bonita Montero wrote:
>> Am 03.04.2022 um 23:01 schrieb Malcolm McLean:
>>
>> > But there might not really be a use case for JSON.
>> There are many usecases for JSON.
>> JSON often replaces XML because it's simpler
>> (look at this XML Schema bloat).
>>
> JSON has been accepted as a format for data exchange.
>
> The question is whether there is a use case for it in a C source generator.
> Would people take data in in JSON and want it converted to compileable
> C structures?

People would, but mainly those who don't know Lisp.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor