Parse base::Value into Mojo struct

28 views
Skip to first unread message

Tibor Goldschwendt

unread,
Jul 16, 2021, 5:37:44 PM7/16/21
to chromium-mojo, Alex Gough
Hi y'all,

Do we have autogenerated Mojo bindings code to convert a base::Value to a Mojo struct?

E.g. given the Mojo struct

struct Foo {
  string bar;
  int32 baz;
};

Do we have autogenerated code that does the following?

auto foo = Foo::New();
foo->bar = value->FindStringPath("bar");
foo->baz = value->FindIntPath("baz");

We have several applications where we parse JSONs into base::Value and then populate a Mojo struct, resulting in lots of boiler plate. I'm curious if some of this boilerplate could be replaced by autogenerated code. If such autogenerated code does not yet exist, what would it take to add it?

Cheers,
Tibor

K. Moon

unread,
Jul 16, 2021, 5:43:58 PM7/16/21
to Tibor Goldschwendt, chromium-mojo, Alex Gough
Have you looked at the Mojo bindings for JavaScript, rather than trying to convert JSON back and forth?

--
You received this message because you are subscribed to the Google Groups "chromium-mojo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-moj...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/6eb7d9ce-11eb-452e-a057-ddd28563e301n%40chromium.org.

K. Moon

unread,
Jul 16, 2021, 5:44:41 PM7/16/21
to Tibor Goldschwendt, chromium-mojo, Alex Gough

Tibor Goldschwendt

unread,
Jul 16, 2021, 6:46:24 PM7/16/21
to K. Moon, chromium-mojo, Alex Gough
Thanks for the pointer kmoon@! Using JS bindings would be neat but it might not quite work for us because:
- We likely still want to perform download and parsing in the browser process without spinning up a renderer.
- The JSON has to be parsed in a sandboxed process as it is not trusted. The present infra gives us a base::Value after the parsing. I guess we could write the base::Value back to a JSON to make it trusted in case converting a JSON to Mojo is easier than converting a base::Value to Mojo.

K. Moon

unread,
Jul 19, 2021, 10:14:09 PM7/19/21
to Tibor Goldschwendt, chromium-mojo, Alex Gough
(Since nobody chimed in with a better idea... :-))

If you're doing all the manipulation in native code, off the top of my head, I don't think there's anything generated by Mojo, as it doesn't seem like it would be very general-purpose; presumably you'd need a custom generator for this application.

Tibor Goldschwendt

unread,
Jul 21, 2021, 3:03:34 PM7/21/21
to K. Moon, chromium-mojo, Alex Gough
Makes sense, thanks Kahmy!
Reply all
Reply to author
Forward
0 new messages