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

JSON (proper) #32

Open
1 of 4 tasks
vil1 opened this issue Jan 3, 2019 · 2 comments
Open
1 of 4 tasks

JSON (proper) #32

vil1 opened this issue Jan 3, 2019 · 2 comments

Comments

@vil1
Copy link
Member

vil1 commented Jan 3, 2019

We currently have a toy example interpreting schemas into JSON encoders, that merely stands as an example/POC for interpreting schemas into contravariant functors.

We need to add modules for working real-world JSON libraries (prioritising the most widely used).

Candidates are:

  • circe
  • play-json
  • argonaut
  • scalaz-json (when it happens)
@GrafBlutwurst
Copy link
Collaborator

GrafBlutwurst commented Jan 4, 2019

One current issue is that it is quite trivial to derive an encoder which will generate invalid JSON.

val tplSchema: Fix[Schema, (Boolean, String)] = prim(JsonSchema.JsonBool) :*: prim(JsonSchema.JsonString)

        implicit val primToEncoderNT = new (Prim ~> Encoder) {
          def apply[A](fa: Prim[A]): Encoder[A] = { a =>
            fa match {
              case JsonNumber => a.toString
              case JsonBool   => a.toString
              case JsonString => s""""$a""""
              case JsonNull   => "null"
            }
          }
        }

        val serializer: Encoder[(Boolean, String)] = tplSchema.to[Encoder]
        val out = serializer((true, "asdf"))
        Fail.string(out)

will output true, "asdf"

the problem here is that there is no "sensible" encoder for an anonymous tuple. So far I see 2 possibilities to rectify this:

  1. introduce default values for such cases
  2. add errors to the derivation Error \/ Encoder[A] and return an error if the derivation is for a schema that has no sensible json encoder.

both of there would require a more powerful fold than cata probably histo so we can inspect the structure of the already folded part in the :*: case to determine whether the underlying terms are labelled or not.

This was referenced Jan 13, 2019
@sasiharan
Copy link

@vil1 I would like to work on schema module for Circe library if you have not started on it.

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

3 participants