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.

Post History

77%
+5 −0
Q&A Handling JSON files in Rust without manually creating mapping classes

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 str...

1 answer  ·  posted 4y ago by dustytrash‭  ·  last activity 4y ago by r~~‭

Question rust json
#6: Post edited by user avatar dustytrash‭ · 2020-10-16T15:15:30Z (about 4 years ago)
Fixed example to be actual example. I realized it could be relavent, I don't actually need to access fields from the classes, just reference the classes
  • 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` will be used like any other class:
  • // example 1
  • let character_info : CharacterInfo = { ... };
  • // example 2
  • let character_info : CharacterInfo = serde_json::from_str(&json_str).unwrap();
  • 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?
  • 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?
#5: Post edited by user avatar dustytrash‭ · 2020-10-16T15:10:23Z (about 4 years ago)
  • 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 URL, an outside source. 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` will be used like any other class:
  • // example 1
  • let character_info : CharacterInfo = { ... };
  • // example 2
  • let character_info : CharacterInfo = serde_json::from_str(&json_str).unwrap();
  • 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?
  • 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` will be used like any other class:
  • // example 1
  • let character_info : CharacterInfo = { ... };
  • // example 2
  • let character_info : CharacterInfo = serde_json::from_str(&json_str).unwrap();
  • 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?
#4: Post edited by user avatar dustytrash‭ · 2020-10-16T14:56:54Z (about 4 years ago)
clarified where JSON data is coming from
  • I have JSON that looks something like this:
  • {"id":"n-fsdf-6b6",
  • "name":"JohnSmith",
  • "revisionDate":1591072274000}
  • The JSON data is named `CharacterInfo`. However 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` will be used like any other class:
  • // example 1
  • let character_info : CharacterInfo = { ... };
  • // example 2
  • let character_info : CharacterInfo = serde_json::from_str(&json_str).unwrap();
  • 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?
  • 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 URL, an outside source. 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` will be used like any other class:
  • // example 1
  • let character_info : CharacterInfo = { ... };
  • // example 2
  • let character_info : CharacterInfo = serde_json::from_str(&json_str).unwrap();
  • 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?
#3: Post edited by user avatar dustytrash‭ · 2020-10-16T14:55:32Z (about 4 years ago)
added an example showing the classes need to be used like any other class
  • I have JSON that looks something like this:
  • {"id":"n-fsdf-6b6",
  • "name":"JohnSmith",
  • "revisionDate":1591072274000}
  • The JSON data is named `CharacterInfo`. However 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 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?
  • I have JSON that looks something like this:
  • {"id":"n-fsdf-6b6",
  • "name":"JohnSmith",
  • "revisionDate":1591072274000}
  • The JSON data is named `CharacterInfo`. However 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` will be used like any other class:
  • // example 1
  • let character_info : CharacterInfo = { ... };
  • // example 2
  • let character_info : CharacterInfo = serde_json::from_str(&json_str).unwrap();
  • 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?
#2: Post edited by user avatar dustytrash‭ · 2020-10-16T14:53:23Z (about 4 years ago)
  • I have JSON that looks something like this:
  • {"id":"n-fsdf-6b6",
  • "name":"JohnSmith",
  • "revisionDate":1591072274000}
  • The JSON data is named `CharacterInfo`. However 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 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? In JS mapping classes are not required, but in Rust it's required at runtime.
  • I have JSON that looks something like this:
  • {"id":"n-fsdf-6b6",
  • "name":"JohnSmith",
  • "revisionDate":1591072274000}
  • The JSON data is named `CharacterInfo`. However 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 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?
#1: Initial revision by user avatar dustytrash‭ · 2020-10-16T14:52:16Z (about 4 years ago)
Handling JSON files in Rust without manually creating mapping classes
I have JSON that looks something like this:

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

The JSON data is named `CharacterInfo`. However 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 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? In JS mapping classes are not required, but in Rust it's required at runtime.