neo4j-labs / neo4rs

Rust driver for Neo4j

Home Page:https://docs.rs/neo4rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use (Serde) `Serialize` instead of or additionally to `Into<BoltType>`

User65k opened this issue · comments

https://serde.rs/

This would solve #7 and add a lot of flexibility, marcos, ...
I guess Deserialize would also be handy.

If you like, I would create a PR for it
I just realised that BoltType is deeply rooted into your crate and also conflicts with an into implementation from any type that is Serialize so this is probably a lot...

What I actually try to archive is converting a JSON Map (with different Value Types) to BoltType.
But currently there seems to be no way of getting to a BoltMap so adding a
impl<T> From<Box<T>> for BoltType where T: Into<BoltType>
for the dynamic types would not by itself help me

However converting from JSON would be easy:

impl From<serde_json::Value> for BoltType {
    fn from(json: serde_json::Value) -> BoltType {
        match json {
            serde_json::Value::Null => BoltType::Null(BoltNull),
            serde_json::Value::Bool(bool) => BoltType::Boolean(BoltBoolean::new(bool)),
            serde_json::Value::Number(nr) => {
                if nr.is_f64() {
                    BoltType::Float(BoltFloat::new(nr.as_f64().unwrap()))
                }else{
                    BoltType::Integer(BoltInteger::new(nr.as_i64().unwrap()))
                }
            },
            serde_json::Value::String(str) => BoltType::String(BoltString::new(&str)),
            serde_json::Value::Array(arr) => {
                let mut l = BoltList::with_capacity(arr.len());
                for v in arr {
                    l.push(v.into());
                }
                BoltType::List(l)
            },
            serde_json::Value::Object(map) => {
                let mut m = BoltMap::with_capacity(map.len());
                for (k,v) in map {
                    m.put(k.into(),v.into());
                }
                BoltType::Map(m)
            },
        }
    }
}

Would you be interested in a conversion from JSON? Probably behind a Feature flag, as I guess that serde_json adds a bit of weight

Wow 😍. that would easy things big time, any performance implications?

We now have a serde dataformat implementation for BoltType, where we can support Deserialize without going through JSON. This is available in 0.7.0-rc.2 and will continue to be improved.