Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Expose the Serializer and Deserializer types #44

Closed
dtolnay opened this issue Feb 11, 2017 · 11 comments · Fixed by #185 or #186
Closed

Expose the Serializer and Deserializer types #44

dtolnay opened this issue Feb 11, 2017 · 11 comments · Fixed by #185 or #186
Milestone

Comments

@dtolnay
Copy link
Owner

dtolnay commented Feb 11, 2017

This is required for composition with other libraries like serde_ignored.

This is harder than just slapping pub on the thing because the Deserializer currently borrows some data owned by the serde_yaml::from_str method:

struct Deserializer<'a> {
    events: &'a [(Event, Marker)],
    /// Map from alias id to index in events.
    aliases: &'a BTreeMap<usize, usize>,
    pos: &'a mut usize,
    path: Path<'a>,
}

May need to refactor into a public owned Deserializer and an internal borrowed Deserializer.

The Serializer is also hard because it doesn't write to an underlying io::Write like JSON's serializer does.

@sinesc
Copy link

sinesc commented Feb 16, 2018

I've just discovered Serde a few days ago and have since been trying to use it in a toy project/game, so please excuse the newbie question, but is a fix for this required for stateful deserialization with serde-yaml?

I'd need to deserialize names of items into items (e.g. Arc<Sprite>) by looking them up in a library of items (that I'd like to pass as state). It's not often that I don't have to look up anything at all during deserialization, so I'm hopeful that I'm missing something.

@mardiros
Copy link

From what I understand,

this is also a requirements to user serde_transcode with yaml too.

The documentation is casting json to json.

I'd like to cast yaml to json, and I suppose it is not possible right now.

@ExpHP
Copy link

ExpHP commented May 25, 2018

Workarounds: (v0.7.4)

For now one can deserialize from a Value, but if you do this the naive way you end up with terrible error messages for anything other than syntax errors. (e.g. Message("invalid value: map, expected map with a single key", None))

However, there is a workaround for that, too, for those who can afford it:

use ::serde_yaml::Value;
use ::std::io::Read;
use ::serde::de::DeserializeOwned;

fn read_yaml<T: DeserializeOwned>(mut r: impl Read) -> Result<T, Error> {
    // First, read into a form that we can read from multiple times.
    let mut s = String::new();
    r.read_to_string(&mut s)?;

    // Get a Value, which implements Deserializer.
    let value = ::serde_yaml::from_str(&s)?;

    match _read_using_serde_ignored(value) {
        Ok(out) => Ok(out),
        Err(_) => {
            // That error message was garbage. Let's parse without serde_ignored
            // directly from the string, to get an error message with position info.
            ::serde_yaml::from_str(&s)?;
            unreachable!();
        }
    }
}

fn _read_using_serde_ignored<T: DeserializeOwned>(value: Value) -> Result<T, Error> {
    ::serde_ignored::deserialize(
        value,
        |path| warn!("Unused config item (possible typo?): {}", path),
    ).map_err(Into::into)
}

@amitu
Copy link

amitu commented Nov 1, 2018

I'd like to cast yaml to json, and I suppose it is not possible right now.

@mardiros: This is not true:

let v: serde_json::Value = serde_yaml::from_str(yml_string)?

@mardiros
Copy link

@pickfire
Copy link

pickfire commented Mar 7, 2019

@dtolnay I would like to work on this, I need it for babelfish as well, I just realized that I wanted to convert between json and yaml.

@dtolnay
Copy link
Owner Author

dtolnay commented Mar 7, 2019

Sounds good @pickfire, go for it.

@pickfire
Copy link

@dtolnay I think I might need some guidance on how to get this done. I thought it is just as easy as slapping pub against Deserializer and Serializer.

I noticed that there are src/read.rs for both json and cbor, do I need to have that as well? Do I need to change the signature to something like impl <'de, R> Deserializer<R>?

@pickfire
Copy link

pickfire commented May 12, 2019

I am not sure how this could be implemented using yaml-rust after trying out an implementation on #129.

Should we write ourselves?

@dtolnay
Copy link
Owner Author

dtolnay commented May 12, 2019

I believe this would be easier to implement after solving dtolnay/request-for-implementation#9 and switching backends.

@HeapUnderfl0w
Copy link

Im just leaving this as a note here:

A exposed (De)Serializer type is also required to drive something like serde_transcode (as seen here)

Repository owner locked and limited conversation to collaborators Jun 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
7 participants