Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Serialize/Deserialize derives with this crate? #14

Open
tuboythers opened this issue Dec 27, 2019 · 5 comments
Open

How to use Serialize/Deserialize derives with this crate? #14

tuboythers opened this issue Dec 27, 2019 · 5 comments

Comments

@tuboythers
Copy link

tuboythers commented Dec 27, 2019

Hello,

I am a bit new to Rust (I have to start with that), but I am also very confused how to use the custom derives of serde with this crate?

From your docs it would seem like it should be:

extern crate serde;
extern crate serde_hjson;

use serde_hjson::{Map,Value};

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
let sample_text=r#"[{ x: 1, y: 2 },{ x: 3, y: 4 }]"#;
    let sample: Value = serde_hjson::from_str(&sample_text).unwrap();
    {
        let sample2 = serde_hjson::to_string(sample.as_array().unwrap().get(0).unwrap().as_object().unwrap()).unwrap();

        let testData: Point = serde_hjson::from_str(&sample2).unwrap();
    }
}

However this returns the following error:

error[E0277]: the trait bound 'Point: serde::de::Deserialize' is not satisfied
   --> src/lib.rs:36:30
    |
36  |         let testData: Point = serde_hjson::from_str(&sample2).unwrap();
    |                              ^^^^^^^^^^^^^^^^^^^^^ the trait 'serde::de::Deserialize' is not implemented for 'Point'
    | 
    |
851 |     where T: de::Deserialize
    |              --------------- required by this bound in 'serde_hjson::from_str'

Edition is 2018 and my Cargo.toml deps are:

[dependencies]
serde = { version = "0.9", features = ["derive"] }
serde-hjson = "*"

If you could point me towards the right direction of what I am missing here I would be most grateful.

@dqsully
Copy link
Member

dqsully commented Jan 14, 2020

This may be due to the fact that we are not updated to serde 1.0 yet. There is an active issue for it here: #6

@LittleHuba
Copy link

You might want to refrain from advertising this feature in the docs if it is not supported yet.

@dqsully
Copy link
Member

dqsully commented Jun 1, 2020

It should work, just not with the latest version of Serde. Before the derive feature was a thing on the main Serde crate, you had to use this crate instead: https://crates.io/crates/serde_derive

@LittleHuba
Copy link

I tried it with both Serde 0.9 and 1.0, with the derive feature activated but get always the error that the serde::de::Deserialize Trait is not implemented for my structs (which makes sense as serde-hjson depends on 0.8).
I can get it to run when I explicitly link to the 0.8 version of serde_derive but then it is nearly unusable as most of the stuff required to work with this feature is still missing.

In other words for you to advertise this feature you would need to update to a newer version of serde - preferably 1.0.

@MillieSilva
Copy link

@dqsully Any update on the situation? Unfortunately i cannot lower serde versions in my case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants