capnproto / go-capnp

Cap'n Proto library and code generator for Go
https://capnproto.org
Other
1.22k stars 110 forks source link

JSON marshalling/unmarshalling #28

Open zombiezen opened 8 years ago

zombiezen commented 8 years ago

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.

zombiezen commented 8 years ago

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.

kant777 commented 3 years ago

@zombiezen any update on this?

zenhack commented 3 years ago

@kant777, I don't believe anyone is actively working on this. Patches welcome.