Skip to content

RFC: Implementing multiple serialization methods #1197

Open

Description

@lukepighetti

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!)

Activity

  1. dave26199 commented on Nov 6, 2022

    @dave26199

    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.

  2. bivens-dev commented on May 28, 2025

    @bivens-dev

    @davidmorgan Circling back on this, I'm not sure if this is something you were still considering at all but CBOR was certainly something I was looking at as an alternative to JSON. Just wanted to offer it as a usecase.

  3. davidmorgan commented on May 30, 2025

    @davidmorgan
    Collaborator

    I expect built_value already works with CBOR, since both work with "the wildly successful JSON data model: numbers, strings, arrays, maps (called objects in JSON), and a few values such as false, true, and null". I'd be interested to hear about it if anyone tries it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions