Problems at turning json to struct

How do you properly convert a "huge" json to a struct?

In go there are converters on the net to do so. For rust I only found one and that one isn't doing his job right or what's more likely I do something wrong.

If it is any help, I can post a link to the json if anybody wants to take a look at it.

serde-json is mostly likely the right answer in this case: GitHub - serde-rs/json: Strongly typed JSON library for Rust

Already use it and fail miserably.

What error are you getting? Serde is definitely the best solution for this in the Rust language, and, as many would argue, even across different languages :slight_smile:

Error: invalid type: map, expected unit at line 3 column 10

That probably means that your rust struct and the actual JSON have different layouts: that is, names and/or types of fields do not match between JSON and Rust.

1 Like

You are right. I had some troubles with the naming.
My problem was that the structs alone are 412 lines to put my json into a struct.

I found a website to generate my struct. I still had to tinker a bit around but only because fields are missing from time to time.

The site is json_typegen

2 Likes

Looks like the web-based json_typegen is backed by the json_typegen crate, which includes a command-line tool and a proc macro. Might also check that out if you'd like to avoid fiddling with a web-ui.

Oops, that page is quite out of date with the current functionality of my tool, as it has been replaced with a version using Web Assembly, which is hosted at https://typegen.vestera.as/ . I guess a redirect would be in order (Edit: Fixed. Old page now redirects.). If you have any issues (e.g. with missing fields) or feedback in general, an issue on Issues · evestera/json_typegen · GitHub would be very welcome. :slight_smile:

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.