Closed ppoliani closed 2 years ago
There should be TryFrom
conversions for Value
into all equivalent native types - here's some pseudocode for example:
let some_hashmap: HashMap<String, i64> = HashMap::from_iter( ... );
let bolt_value = Value::from(some_hashmap); // This is a Value::Map
let native_value: HashMap<String, Value> = HashMap::try_from(bolt_value).unwrap();
// Or if your hashmap value type implements TryFrom<Value>...
let native_value: HashMap<String, i64> = HashMap::try_from(bolt_value).unwrap();
Something like that anyway... see here for more conversions. I noticed that some of them aren't showing up on docs.rs, which is a bit strange. Run cargo doc --open
on bolt-proto
and you should be able to see all of them.
Looking back at this, the Value
API could use some simplification - it would be nice to be able to just match
on a Value
to get the native type:
match value {
Value::Boolean(b) => ... // where b: bool
Value::String(s) => ... // where s: String
_ => ...
}
I will consider doing this at some point so there is no need for the TryFrom
conversions - that was sort of an experiment that didn't work out very well.
I'll also see about implementing serde's Serialize
trait so people don't need to write custom serializers.
@lucis-fluxum thanks so much for the quick response. This is very helpful indeed.
I have actually found the TryFrom implementation by digging into the code and managed to write a simple serde-json serializer. But I totally agree with you; having that as part of the core crate would be super awesome.
I'm trying to turn the records I get from a query into json; however I haven't found any easy way to do that. I tried to use custom serialisation using serde_json but I found that I cannot access the underlying
value
of the different Value types that are supported.Is there any way we can get hold of the underlying value so custom serialization into different types could be possible?