Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support various encoding styles of Aeson #36

Open
chiroptical opened this issue Nov 15, 2021 · 1 comment
Open

Support various encoding styles of Aeson #36

chiroptical opened this issue Nov 15, 2021 · 1 comment

Comments

@chiroptical
Copy link
Collaborator

Introduced in #35, aeson allows for different encoding styles. We should support then for the languages where it makes sense. The styles are documented here

@tadfisher
Copy link
Collaborator

A good one for the Kotlin backend is ObjectWithSingleTag, because newtypes are collapsed by Kotlin Serialization. For example

value class Foo(val field: String)

data class Bar(val foo: Foo)

Json.encodeToString(Bar(Foo("hello")))
// -> { "foo": "hello" }
data class Foo(val field: String)

data class Bar(val foo: Foo)

Json.encodeToString(Bar(Foo("world")))
// -> { "foo": { "field": "hello" } }

This currently forces a workaround to either manually implement ToMoatData using MoatStruct, or to write a function like

-- | Given a newtype, generate a struct instead.
newtypeToStruct :: MoatData -> MoatData
newtypeToStruct MoatNewtype {..} =
  MoatStruct
    { structName = newtypeName
    , structTyVars = newtypeTyVars
    , structInterfaces = newtypeInterfaces
    , structProtocols = newtypeProtocols
    , structAnnotations = newtypeAnnotations
    , structFields = [newtypeField]
    , structPrivateTypes = []
    , structTags = []
    }
newtypeToStruct _ = error "newtypeToStruct: only works for newtypes"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants