google/built_value.dart

RFC: Implementing multiple serialization methods

lukepighetti opened this issue · 1 comments

Sometimes you want to serialize a data class into multiple different data formats. The most common is JSON, but others include MessagePack, protobuf, XML, custom binary payloads, and more.

Serde (for Rust) has a list of data formats that are supported through external plugins. https://serde.rs/#data-formats

It would be nice to have some examples of how to handle serialization with built_value to multiple data formats.

I noticed we have StandardJsonPlugin, but it's not clear to me how we could use that and something like a MessagePackPlugin simultaneously, especially since @BuiltValue only supports String wire names, and it's not clear how we would input more complex information (especially in the case of binary formats). I suspect we'd have to duplicate the serializer stack and have different plugins on each one?

(I would have opened this as a discussion but I noticed it's not enabled on this repo!)

Hi Luke,

The simplest thing would be if the existing serialized format can be mapped to the additional format(s). Note that it does not actually serialize to JSON strings, but to a List (the default format) or Map (with StandardJsonPlugin). These structures may be easy to convert to other serialization formats.

I guess the most important consideration is whether there is enough information present to convert to each serialization format. Obviously if something is missing that could only be added by the developer or deduced during codegen then that will need special support.

Broadly speaking I would be open to adding more general support / hooks to support new ways of serialization--as long as they do not turn out to be too far off the path of what built_value is already doing. There is a case for limiting codegen complexity and a case for limiting any unused output of the codegen. On the other hand, I've thought for a while that the existing mechanism is probably not general enough and it would be nice to refactor on top of a more general core.

Did you have any specific format(s) in mind that you'd like to see supported? That might make the discussion more concrete :)

Thanks.