Re: Which newsgroup for json parsing?

Liste des GroupesRevenir à l c 
Sujet : Re: Which newsgroup for json parsing?
De : malcolm.arthur.mclean (at) *nospam* gmail.com (Malcolm McLean)
Groupes : comp.lang.c
Date : 08. May 2024, 16:33:48
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v1g2ge$qv9$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 08/05/2024 14:04, Josef Möllers wrote:
Hi all,
 I am trying to parse a json string received through MQTT from a "Shelly Plug S", e.g.
{"id":0, "source":"button", "output":true, "apower":0.0, "voltage":237.9, "current":0.000, "aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1715172119},"temperature":{"tC":41.1, "tF":106.0}}
 I am trying to use libjson-glib but I can't figure out what to use as the first argument to json_gobject_from_data()!
I am also looking at libjson-c but cannot find any examples that could guide me.
 
JSON parsing is the sort of thing you can easily do in C, but you really don't want to do yourself, becuase it is a lot of fiddly code to write, and everyone wants to solve exactly the same problem, which is to query text formatted as JSON, usually for fields whose names and types are known, but with facilities to list fields onthe fly.
But of course I wrote my own JSON parser in C++, As programmers often do. But it was at work, so I can't post a copy here, since it was work for hire and not mine to give away.
Now libjson-glib strikes me as rather a poorly designed library with too many dependencies, and over complicated.
But here's the prototype for the function
  GObject*
json_gobject_from_data (
   GType gtype,
   const gchar* data,
   gssize length,
   GError** error
)
Obviously, this creates a Gobject* that you query, from text that you pass in (he data argument). For some reason the string is not nul-terminated and so you pass in the length, and you also get a parse error object, and to be fair this can be extremely important.
But what is the gtype parameter?
WEll a bit of digging reveals this palaver
GType
my_struct_get_type (void)
{
   static GType boxed_type = 0;
   if (boxed_type == 0)
     {
       boxed_type =
         g_boxed_type_register_static (g_intern_static_string ("MyStruct"),
                                       (GBoxedCopyFunc) my_struct_copy,
                                       (GBoxedFreeFunc) my_struct_free);
       json_boxed_register_serialize_func (boxed_type, JSON_NODE_OBJECT,
                                           my_struct_serialize);
       json_boxed_register_deserialize_func (boxed_type, JSON_NODE_OBJECT,
                                             my_struct_deserialize);
     }
   return boxed_type;
}
So you set it up. Ir's a highly flexible way of registering a data structure at run time. But not what you want to parse a bit of JSON. And if there's a simple default type to pass in, they are not documenting this clearly. So if I had a choice, I would simply avoid the library and choose another.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Date Sujet#  Auteur
8 May 24 * Which newsgroup for json parsing?29Josef Möllers
8 May 24 +* Re: Which newsgroup for json parsing?2Anton Shepelev
23 May 24 i`- Re: Which newsgroup for json parsing?1Lawrence D'Oliveiro
8 May 24 +* Re: Which newsgroup for json parsing?2Malcolm McLean
8 May 24 i`- Re: Which newsgroup for json parsing?1Anton Shepelev
9 May 24 +- Re: Which newsgroup for json parsing?1jak
13 May 24 +- Re: Which newsgroup for json parsing?1Josef Möllers
16 May 24 `* Re: Which newsgroup for json parsing?22Mikko
16 May 24  +* Re: Which newsgroup for json parsing?19Josef Möllers
16 May 24  i+* Re: Which newsgroup for json parsing?6bart
17 May 24  ii`* Re: Which newsgroup for json parsing?5Malcolm McLean
17 May 24  ii +* Re: Which newsgroup for json parsing?2bart
25 May 24  ii i`- Re: XML (was Re: Which newsgroup for json parsing?)1Lawrence D'Oliveiro
17 May 24  ii `* Re: Which newsgroup for json parsing?2jak
17 May 24  ii  `- Re: Which newsgroup for json parsing?1Malcolm McLean
18 May 24  i`* Re: Which newsgroup for json parsing?12Mikko
27 May 24  i `* Re: Which newsgroup for json parsing?11Josef Möllers
27 May 24  i  `* Re: Which newsgroup for json parsing?10Vir Campestris
28 May 24  i   `* Re: Which newsgroup for json parsing?9Josef Möllers
28 May 24  i    +* Re: Which newsgroup for json parsing?7Michael S
28 May 24  i    i+- Re: Which newsgroup for json parsing?1Josef Möllers
29 May 24  i    i+* Re: Which newsgroup for json parsing?3Malcolm McLean
29 May 24  i    ii`* Re: Which newsgroup for json parsing?2David Brown
30 May 24  i    ii `- Re: Which newsgroup for json parsing?1Malcolm McLean
31 May 24  i    i+- Re: Which newsgroup for json parsing?1Tim Rentsch
5 Aug 24  i    i`- Re: Which newsgroup for json parsing?1Lawrence D'Oliveiro
30 May 24  i    `- Re: Which newsgroup for json parsing?1Vir Campestris
23 May 24  `* Re: Which newsgroup for json parsing?2Lawrence D'Oliveiro
23 May 24   `- Re: Which newsgroup for json parsing?1Malcolm McLean

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal