Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Handling JSON files in Rust without manually creating mapping classes

Post

Handling JSON files in Rust without manually creating mapping classes

+5
−0

I have JSON that looks something like this:

{"id":"n-fsdf-6b6",
"name":"JohnSmith",
"revisionDate":1591072274000}

The JSON data is named CharacterInfo. It comes from a static external URL. The structure of this information could be changed, as it comes from an outside source and updates occur.

In rust the mapping class using Serede looks something like this:

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct CharacterInfo {
    pub id: String,
    pub name: String,
    #[serde(rename = "revisionDate")]
    pub revision_date i64: 
}

The CharacterInfo class needs to be referenced in code. It's an API wrapper library, so the method looks something like this:

pub fn get_character_info() -> Result<CharacterInfo, HttpError> {
    let http_result = HttpClient::get(Self::CHARACTER_INFO_URL);

    match http_result {
        Ok(result) => {
            let character_info: CharacterInfo = serde_json::from_str(&result).unwrap();

            Ok(character_info)
        }
        Err(error) => Err(error),
    }
}

The problem is the JSON files could change and there are a lot of them. Generating them manually would be tedius.

Generating the .rs mapping classes Is not that difficult, in fact websites already exist to do this (https://transform.tools/json-to-rust-serde).

But I'm not sure how I could have my Rust program compile or use the .rs files after creating. Also would this be considered hacky / bad practice? Is there a better way?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (2 comments)
General comments
r~~‭ wrote about 4 years ago

How much work are you interested in doing when the JSON format changes? Are you trying to avoid recompilation altogether? Or do you just want the recompilation not to involve too much manual effort?

dustytrash‭ wrote about 4 years ago

I'd like no manual work if the JSON changes. It does not matter if recompilation is needed on updates