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

Consider using rustc_serialize::json::encode to obtain json #19

Closed
hugoduncan opened this issue Apr 12, 2015 · 12 comments
Closed

Consider using rustc_serialize::json::encode to obtain json #19

hugoduncan opened this issue Apr 12, 2015 · 12 comments

Comments

@hugoduncan
Copy link
Contributor

Rather than force every structure to implement the ToJson trait, it might be preferable to allow users to just add a #[derive(RustcEncodable)] on their structs, and handlebars can obtain a JSON value via the rustc_serialize::json::encode function.

@vayn
Copy link

vayn commented Apr 13, 2015

+1

And according to this post: What's the difference between the “serialize” and “rustc-serialize” crates?, I recommend to use Serde and its macros to serialize/deserialize a struct to/from JSON instead of rustc-serialize.

@hugoduncan
Copy link
Contributor Author

I can second the recommendation for Serde, although it does require rust nightly to use it.

@sunng87
Copy link
Owner

sunng87 commented Apr 13, 2015

Note that we don't actually serialize data to JSON. We just use the ToJson trait as a type system. Because handlebars is designed against JavaScript, whose type system isn't fully compatible with Rust. Currently, ToJson is a good way to convert Rust types into JavaScript types.

Yes, we need a macro for users to generate their ToJson impl easily.

@vayn
Copy link

vayn commented Apr 13, 2015

Thanks. I understand why you use Json enum and ToJson trait for transforming Rust type and JavaScript type now. It is little twisted for the purpose though... Macro is a better and clearer way.

@sunng87
Copy link
Owner

sunng87 commented Apr 13, 2015

@vayn patches are always welcomed :-)

@hugoduncan
Copy link
Contributor Author

Another possibility, when using serde at least, would be to write your own Serializer implementation that returned a BTreeMap of values. This would remove the need to implement ToJson and the need for a custom macro.

@sunng87
Copy link
Owner

sunng87 commented Apr 15, 2015

@hugoduncan I didn't look into Serde, is it easier to implement Serializer of Serde than ToJson of rustc-serialize?

Because most ToJson implementation will be: create a map, put every field into it. Perhaps we can find some meta-programming techniques to deal with this. Not sure if Rust has something for that.

@hugoduncan
Copy link
Contributor Author

@sunng87 The Serializer would be something that rust-handlebars would implement. Users would then just mark structs with #[derive(Serialize)].

@sunng87
Copy link
Owner

sunng87 commented Apr 16, 2015

@hugoduncan Iooked at Serde, I would like to implement a custom derive for handlebars that generates ToJson implementation. It would be compatible with current type system, but also reduce repeated ToJson impl.

@sunng87
Copy link
Owner

sunng87 commented Apr 19, 2015

@hugoduncan @vayn check out tojson_macros I just made for generating default ToJson imp.

@vayn
Copy link

vayn commented Apr 19, 2015

I'm still learning how to make a procedural macro for custom struct.

@sunng87 You're amazing! 👍

@sunng87
Copy link
Owner

sunng87 commented Apr 21, 2015

@vayn :)

I'll close this for now. Feel free to reopen if there's any question.

@sunng87 sunng87 closed this as completed Apr 21, 2015
JoNil pushed a commit to JoNil/handlebars-rust that referenced this issue Mar 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants