Re: [PATCH 11/19] rust/qobject: add from/to JSON bindings for QObject

Paolo Bonzini <[email protected]>
Newsgroups org.nongnu.qemu-rust,org.nongnu.qemu-devel
Message-ID <CABgObfYJiBZC6jx9wQGCubC8J6Gu0Hn9q+Zo_Dm1J8PcRJDNNw@mail.gmail.com>
Il ven 5 dic 2025, 13:16 Markus Armbruster <[email protected]> ha scritto:

> > Note however that there is no support for push parsing, therefore this
> > would not replace the balanced-parentheses machinery in
> > qobject/json-streamer.c, and therefore QMP would still need a minimal
> lexer.
>
> That push parser...  I never liked it.  First, it's half-assed: it's a
> push lexer wed to a pull parser with parenthesis counting.  Second, why
> complicated & half-assed when you can do simple & quarter-assed instead?
> We could've required "exactly one complete JSON value per line", or some
> expression separator such as an empty line.
>

Hmm not sure I agree, actually I think I disagree. It seems simpler but it
is also different.

Push parsing is not rocket science. It would be easy to write a proper one,
it's just that there is no real reason other than cleanliness.

> Grr... I just remembered about interpolation :/ so no, we still need a
> > parser for libqmp.c.
>
> Right.
>
> Interpolation lets us build QObjects from literal templates with
> variable scalars or QObjects interpolated.  More concise and much easier
> to read than the equivalend nest of constructor calls.  Drawback: chains
> us to our own, bespoke JSON parser.
>

And also, for similar reasons of practicality, single quotes (which IIRC
also became valid QMP, and that's less excusable).

But while it's a pity, we still get a lot from serde, namely making the
automatic generation of visitor code for structs someone else's problem.

Out of curiosity: how would we do better than "nest of constructor
> calls" in Rust?
>

You'd do that with a quoting macro, i.e.

qobject!({"command": cmd})

where the macro compiles to the nest of constructor calls, like
QObject::from_iter([(c"command", to_qobject(cmd))]).

Paolo

>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.