Code Monkey home page Code Monkey logo

Comments (11)

HeapUnderfl0w avatar HeapUnderfl0w commented on May 27, 2024 2

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)

from serde-yaml.

sinesc avatar sinesc commented on May 27, 2024

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.

from serde-yaml.

mardiros avatar mardiros commented on May 27, 2024

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.

from serde-yaml.

ExpHP avatar ExpHP commented on May 27, 2024

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)
}

from serde-yaml.

amitu avatar amitu commented on May 27, 2024

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)?

from serde-yaml.

mardiros avatar mardiros commented on May 27, 2024

@amitu

Thanks I didn't know that works!

https://github.com/mardiros/rustaman/blob/features/multi-requests/src/helpers/http.rs#L68

from serde-yaml.

pickfire avatar pickfire commented on May 27, 2024

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

from serde-yaml.

dtolnay avatar dtolnay commented on May 27, 2024

Sounds good @pickfire, go for it.

from serde-yaml.

pickfire avatar pickfire commented on May 27, 2024

@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>?

from serde-yaml.

pickfire avatar pickfire commented on May 27, 2024

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

Should we write ourselves?

from serde-yaml.

dtolnay avatar dtolnay commented on May 27, 2024

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

from serde-yaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.