clarkmcc / cel-rust

Common Expression Language interpreter written in Rust
https://crates.io/crates/cel-interpreter
MIT License
375 stars 21 forks source link

Serialize Value to JSON String #39

Closed jplock closed 7 months ago

jplock commented 7 months ago

I’m fairly new to Rust and am using this project. I’d like to convert the executed Value into a String so I can serialize it to JSON.

Does Value need to implement From for this to work?

clarkmcc commented 7 months ago

I think we just need to implement Serialize for Value in order for this to work, which has the added advantage of not being strictly tied to JSON. I'll take a look at this this weekend. I apologize for the delay -- been a crazy week.

jplock commented 7 months ago

Would another approach be to implement Display for Value ?

clarkmcc commented 7 months ago

@jplock you certainly could, though as a library, it would require you to assume that your users always want to serialize to a specific format (whatever format we serialize to in the Display trait implementation). That is probably not a safe assumption. The better approach would be to implement serde::Serialize for Value, that way you can use any serde-compatible serializer. In your case, you would use serde_json to serialize Value to a JSON string.

I've gone ahead and done this in the serialize-value branch. Please experiment and provide feedback by pointing your cargo dependency to this repo and branch. Something like this should work

[dependencies]
cel-interpreter = { git = "https://github.com/clarkmcc/cel-rust", branch = "serialize-value", path = "interpreter" }
jplock commented 7 months ago

This is working, thank you! I'm using serde_json::to_value(value)

clarkmcc commented 7 months ago

Latest release 0.7.0 contains support for serializing values