Skip to content

TaggedObject encoding for Nullary constructor includes "contents" #300

@devwout

Description

@devwout

Suppose I want to encode the following type as JSON using TaggedObject SumEncoding.

data MyType = Zero | One String
     deriving (Generic)
instance Aeson.ToJSON MyType

The output of converting One "bla" to JSON is { "tag": "One", "contents": ["bla"] } , which is sensible.

However, when converting Zero to JSON, you get { "tag": "Zero", "contents": [] }. While technically correct, the "contents" key is completely unnecessary. Worse even, the "contents" key is required for the type to be parsed again.

This is awkward if one wants to use the JSON format as an external interface. Of course, it is possible to manually implement the FromJSON and ToJSON instances for that type, but that is tedious work.

I cannot imagine a case where one would like the empty "contents" key to be there. Is this intended behavior? Is there something I am missing?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions