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

Value isn't encodable/decodable #43

Closed
euclio opened this issue Sep 7, 2015 · 8 comments
Closed

Value isn't encodable/decodable #43

euclio opened this issue Sep 7, 2015 · 8 comments

Comments

@euclio
Copy link
Contributor

euclio commented Sep 7, 2015

I'm (slowly) working on #40, and I'm running into a problem with representing heterogenous lists (for arguments). I've resorting to using Vec<Value> as a struct field. However, since Value isn't encodable, it's difficult to serialize it as part of a larger struct.

@3Hren
Copy link
Owner

3Hren commented Sep 11, 2015

It's a bit tricky to implement. I'll try to implement for serde.

@Mange
Copy link

Mange commented Oct 2, 2015

Would this make it possible to handle something similar to this example from serde_json?

//#![feature(custom_derive, plugin)]
//#![plugin(serde_macros)]

extern crate serde_json;

use serde_json::Value;

fn main() {
    let data: Value = serde_json::from_str("{\"foo\": 13, \"bar\": \"baz\"}").unwrap();
    println!("data: {:?}", data);
    // data: {"bar":"baz","foo":13}
    println!("object? {}", data.is_object());
    // object? true

    let obj = data.as_object().unwrap();
    let foo = obj.get("foo").unwrap();

    println!("array? {:?}", foo.as_array());
    // array? None
    println!("u64? {:?}", foo.as_u64());
    // u64? Some(13u64)

    for (key, value) in obj.iter() {
        println!("{}: {}", key, match *value {
            Value::U64(v) => format!("{} (u64)", v),
            Value::String(ref v) => format!("{} (string)", v),
            _ => format!("other")
        });
    }
    // bar: baz (string)
    // foo: 13 (u64)
}

E.g. deserializing some generic MessagePack data, or have I misunderstood this issue?
There does not seem to exist any messagepack library for rust that handles this case yet, AFAIK.

@3Hren
Copy link
Owner

3Hren commented Oct 2, 2015

You can deserialize the bytearray into a msgpack::Value and then work with it as usual ADT.

This issue is about implementing serde::Serialize/Deserialize for rmp::Value.

@SuperFluffy
Copy link

It would be fantastic if we could get this. serde_json does something similar, giving in particular a structure like Object(BTreeMap<String, Value>), which makes it very convenient to construct non-heterogeneous structures (by which I mean something like {"foo": T, "bar": U}.

@3Hren
Copy link
Owner

3Hren commented Jul 24, 2016

Please check this as there is the first look on serialization support for Value. If everything is ok, I'm going to implement deserization too.

Unfortunately, it's required to wrap Value to be able to impl external trait.

@euclio
Copy link
Contributor Author

euclio commented Sep 6, 2016

@3Hren Have you experimented with implementing Deserialize for Value?

@3Hren
Copy link
Owner

3Hren commented Sep 7, 2016

@euclio have no time for this issue, sorry :( Still, the code should be almost the same as in #73.

@3Hren
Copy link
Owner

3Hren commented Jan 5, 2017

Now after I've moved Value from rmp into rmpv crate it is possible to natively serialize/deserialize values using serde (cargo test features=with-serde).

https://github.com/3Hren/msgpack-rust/blob/master/rmpv/src/ext.rs

@3Hren 3Hren closed this as completed Jan 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants