go-capnp
go-capnp copied to clipboard
JSON marshalling/unmarshalling
go-capnproto2 doesn't generate MarshalJSON/UnmarshalJSON methods, which is a regression from v1. v1 solved this by doing codegen guarded by a compile-time constant, which means you had to recompile capnpc-go to generate JSON. This is quite hard to test, and if it were to be always enabled, bloats codegen for those that don't need JSON marshalling.
I think the ideal way to handle it is to store the parsed schema as a byte slice in the generated code, then write a generic utility function (in a separate package) that creates JSON based on a capnp.Struct, the type ID, and the parsed schema. This would also be applicable to #20, since the String representation would work very similarly.
Note that the support for doing Cap'n Proto reflection is now fully supported: you can get runtime type information for any Cap'n Proto type available at link time. Marshaling would operate similarly to the String representation, and unmarshaling would probably involve unmarshaling a map[string]interface{} and then writing the fields into the live object, similar to how the pogs package works.
Contributions are welcome here. I'd be happy to review the code to make this happen, but don't have the cycles myself.
@zombiezen any update on this?
@kant777, I don't believe anyone is actively working on this. Patches welcome.