Path: ...!weretis.net!feeder9.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Mikko Newsgroups: comp.lang.c Subject: Re: Which newsgroup for json parsing? Date: Sat, 18 May 2024 16:21:52 +0300 Organization: - Lines: 53 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 18 May 2024 15:21:52 +0200 (CEST) Injection-Info: dont-email.me; posting-host="48864972f4821b7340d511423b46b671"; logging-data="2952295"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18OZZBXySDlTlYum/C35sCq" User-Agent: Unison/2.2 Cancel-Lock: sha1:H/DXsqjG4Pc7jaKXrFumirK7WTM= Bytes: 3095 On 2024-05-16 18:01:15 +0000, Josef Möllers said: > On 16.05.24 18:27, Mikko wrote: >> On 2024-05-08 13:04:56 +0000, Josef Möllers said: >> >>> 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. >>> >>> Thanks in advance, >> >> Sometimes it is easier to make a parser than to use an existing one. >> In this case I might try LEX. >> > > Sorry to be nitpicking, but lex is a "lexical analyzer" aka a > "scanner", it can only recognize tokens, eg the brackets, quotes, > identifiers etc, but not structures. > "yacc" (or its "bison" equivalent" would be a parser but definitely too heavy. > > What one could do would be to use LEX to recognize the tokens and the > write a recursive descent parser in plain C. As YACC is too heavy I would use only LEX and do with C what LEX alone cannot do. I havn't often done so but when I did I got what I wanted. For JSON parsing a stack of partly parsed objets and arrays is sufficient in addition to what LEX gives, so the C code can be kept simple. LEX can put C fragment to the right place in the final code. If a generic parser is not needed but only a parser with specific structures known in advance the result of parsing can be delivered in a form that is easier to use than a result from a generic JSON parser. In addition, the LEXä+C code can simpler in some sense although more complicated in another sense. > But, as I wrote in another posting, I have switched to libjson-c5 and > the code works fine. That the code works fine is the most important aspect. -- Mikko