Possibility of generating struct for serializing json

since json has strict type
theoretically is should be possible to generate a generic struct right? like given {"a":[1,2]}
we can generate a struct.
so it can be re-created more easily and dont need to wasting typing boilerplate code right?
somearray=[1,2];
somejsonobject.add("a",somarray)
something like this would be possible to re-create the json right?

Is there a downside for doing this?Why no json library implemented this feature/function?
as like generatestruct(String inputjsonpath);

It is unclear what you are asking. If you mean whether it's possible to generate serialization and deserialization boilerplate for user-defined types, then this is exactly what Serde's derive macros do.

That is similar to I wanted,BTW is the dependencies also something like?
I dont really know how to properly build the cargo dependencies mapping.


serde_json = { version = "1.0.82", default-features = false, features = ["alloc"] }

how do we include the derive(Debug, Serialize, Deserialize)] thingy?

Ideally I want to let computer simply figuring out the WHOLE struct by itself.
Since honestly,it should be very obvious what type it is or can just use a type that they can fallback to.
for example in the link the json is>

JSON: {
  "field1": "this is a string",
  "field2": [
    1,
    2,
    3
  ]
}

and the struct is

struct SomeStruct {
    field1: String,
    field2: Vec<u64>,
}

Ideally The struct itself COULD BE generated if we just assume
any json key=field+counter;
any number =u64,
any array=Vec

Why do we need to type this and is there a existing tools to generate this?
Is what I am trying to ask.

In that case, you'll want GitHub - Marwes/schemafy: Crate for generating rust types from a json schema to generate the structs from a schema.

Ahhh yes,Thank you.
But oh boi.it is hard to use without a guide for dependencies.

Using a crate to get more functionality

thanks will refer to that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.