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.
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
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,
Josef
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 youYes, that was exactly my idea when writing the program: why come up with something new when there is existing code to build upon.
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.
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,
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.
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.
=?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid> writes:
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.
I've done full expression parsing and subsequent evaluation with
lex (or flex). No parser needed.
On 16/05/2024 19:53, Scott Lurndal wrote:
=?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid> writes:
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.
I've done full expression parsing and subsequent evaluation with
lex (or flex). No parser needed.
You've done 'full expression PARSING', then you say 'No PARSER needed'.
A bit contradictory?
Clearly parsing IS needed, so either you've used LEX from code that acts
as a parser, or it does more than just recognise tokens.
On 16/05/2024 20:21, bart wrote:
On 16/05/2024 19:53, Scott Lurndal wrote:Yes.
=?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid> writes:
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.
I've done full expression parsing and subsequent evaluation with
lex (or flex). No parser needed.
You've done 'full expression PARSING', then you say 'No PARSER needed'.
A bit contradictory?
Clearly parsing IS needed, so either you've used LEX from code that
acts as a parser, or it does more than just recognise tokens.
I took Ben's advice and completely rewrote my XML parser wirh a formal
lexer and recursive descent grammar. It was good idea, even though XML
is simple enough to get away with a ad hoc approach.
On 16/05/2024 20:21, bart wrote:
On 16/05/2024 19:53, Scott Lurndal wrote:Yes.
=?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid> writes:
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.
I've done full expression parsing and subsequent evaluation with
lex (or flex). No parser needed.
You've done 'full expression PARSING', then you say 'No PARSER needed'.
A bit contradictory?
Clearly parsing IS needed, so either you've used LEX from code that
acts as a parser, or it does more than just recognise tokens.
I took Ben's advice and completely rewrote my XML parser wirh a formal
lexer and recursive descent grammar. It was good idea, even though XML
is simple enough to get away with a ad hoc approach. Similarly, with
JSON. once you've lexed into identifiers, values, and curly and square btackets, the grammar is so simple that you almost don't notice that you
are writing a parser.
--
Check out Basic Algorithms and my other books: https://www.lulu.com/spotlight/bgy1mm
On 16.05.24 18:27, Mikko wrote:
On 2024-05-08 13:04:56 +0000, Josef Mllers 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.
But, as I wrote in another posting, I have switched to libjson-c5 and
the code works fine.
Observe that examples are optional in that they illustrate documentation rather then provide it.
Sometimes it is easier to make a parser than to use an existing one.
In this case I might try LEX.
On Thu, 16 May 2024 19:27:00 +0300, Mikko wrote:
Sometimes it is easier to make a parser than to use an existing one.
In this case I might try LEX.
Fun fact: hand-coded lexical analyzers tend to be faster than table-driven >ones like lex.
On 2024-05-16 18:01:15 +0000, Josef Möllers said:[...]
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.
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.
In my 40+ years of experience in IT/programming (I graduated 1981 from a Dutch polytechnic "HIO" in Computer Science and have retired in 2022) I
have learnt that "works fine" is only part of the work. Maintainability should be added as well. Even if it is code written for one's personal
use only, it may need some work later and then it's crucial to have it maintainable.
But maybe you think so too,
On 27/05/2024 12:51, Josef Möllers wrote:
In my 40+ years of experience in IT/programming (I graduated 1981 from
a Dutch polytechnic "HIO" in Computer Science and have retired in
2022) I have learnt that "works fine" is only part of the work.
Maintainability should be added as well. Even if it is code written
for one's personal use only, it may need some work later and then it's
crucial to have it maintainable.
But maybe you think so too,
I agree completely.
I learned my lesson a a student with a personal project which I left for
6 months. When I came back to it I had to comment it before I could
carry on.
(and I'm even older than you, although not by much)
On 27.05.24 22:18, Vir Campestris wrote:
On 27/05/2024 12:51, Josef Möllers wrote:
In my 40+ years of experience in IT/programming (I graduated 1981
from a Dutch polytechnic "HIO" in Computer Science and have
retired in 2022) I have learnt that "works fine" is only part of
the work. Maintainability should be added as well. Even if it is
code written for one's personal use only, it may need some work
later and then it's crucial to have it maintainable.
But maybe you think so too,
I agree completely.
I learned my lesson a a student with a personal project which I
left for 6 months. When I came back to it I had to comment it
before I could carry on.
You will never have enough comments, even when you consider this rule
;-)
On Tue, 28 May 2024 12:33:02 +0200
Josef Möllers <josef@invalid.invalid> wrote:
On 27.05.24 22:18, Vir Campestris wrote:
On 27/05/2024 12:51, Josef Möllers wrote:
In my 40+ years of experience in IT/programming (I graduated 1981
from a Dutch polytechnic "HIO" in Computer Science and have
retired in 2022) I have learnt that "works fine" is only part of
the work. Maintainability should be added as well. Even if it is
code written for one's personal use only, it may need some work
later and then it's crucial to have it maintainable.
But maybe you think so too,
I agree completely.
I learned my lesson a a student with a personal project which I
left for 6 months. When I came back to it I had to comment it
before I could carry on.
You will never have enough comments, even when you consider this rule
;-)
Probably true.
And despite that you can very easily have too much (or too many?)
comments.
On 28/05/2024 12:45, Michael S wrote:
On Tue, 28 May 2024 12:33:02 +0200People have done some research and found that comments tend to make code harder to understand and maintain. Unfortunately as always I can only
Josef Möllers <josef@invalid.invalid> wrote:
On 27.05.24 22:18, Vir Campestris wrote:
On 27/05/2024 12:51, Josef Möllers wrote:
In my 40+ years of experience in IT/programming (I graduated 1981
from a Dutch polytechnic "HIO" in Computer Science and have
retired in 2022) I have learnt that "works fine" is only part of
the work. Maintainability should be added as well. Even if it is
code written for one's personal use only, it may need some work
later and then it's crucial to have it maintainable.
But maybe you think so too,
I agree completely.
I learned my lesson a a student with a personal project which I
left for 6 months. When I came back to it I had to comment it
before I could carry on.
You will never have enough comments, even when you consider this rule
;-)
Probably true.
And despite that you can very easily have too much (or too many?)
comments.
vaguely remeember reading this somewhere and can't provide any sort of reference.
Let me challenge that: I'm 67 (born September 1956) 😉
On Tue, 28 May 2024 12:33:02 +0200
Josef Moellers <josef@invalid.invalid> wrote:
On 27.05.24 22:18, Vir Campestris wrote:
On 27/05/2024 12:51, Josef Moellers wrote:
In my 40+ years of experience in IT/programming (I graduated 1981
from a Dutch polytechnic "HIO" in Computer Science and have
retired in 2022) I have learnt that "works fine" is only part of
the work. Maintainability should be added as well. Even if it is
code written for one's personal use only, it may need some work
later and then it's crucial to have it maintainable.
But maybe you think so too,
I agree completely.
I learned my lesson a a student with a personal project which I
left for 6 months. When I came back to it I had to comment it
before I could carry on.
You will never have enough comments, even when you consider this rule
;-)
Probably true.
And despite that you can very easily have too much (or too many?)
comments.
And despite that you can very easily have too much (or too many?)
comments.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (1 / 15) |
Uptime: | 155:29:55 |
Calls: | 10,383 |
Files: | 14,054 |
Messages: | 6,417,848 |