hjson/hjson-rust

How to use Serialize/Deserialize derives with this crate?

tuboythers opened this issue · 6 comments

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.

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

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

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

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.

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

Closing due to now using serde 1.0 after merging #24.