Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Go? Here's Rust enum for working with JSON

enum Value { Null, Bool(bool), Number(Number), String(String), Array(Vec<Value>), Object(Map<String, Value>), }

How would this look in Go?



throwing annotations on structs and letting the library generate suitable serializers and deserializers works reasonably well in Go, as long as the JSON is not too unstructured. The same applies for Java and C# (here the JSON libs might be even better). And obviously for Rust too via serde.


Thats a fairly big restriction though and a sign of type system that is not flexible enough.


Maybe. However your representation isn't without drawbacks either: All fields are typed by strings, and are thereby not strongly typed. And each additional level (in an array or object) requires mandatory boxing and allocations. The codegen variants on pure structs don't have this drawbacks.

I would say your representation is favorable when someone wants to work with arbitrary JSON in a flexible fashion. However if schemas and code generators are available, I would prefer to use those.


Thing is in Rust you can do both


Not proficient in Go but I see an interface{} somewhere in the answer.


At which point type safety goes out the window




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: