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

Add support for JSON in H2 using circe #1599

Merged
merged 1 commit into from
Jan 9, 2022
Merged

Add support for JSON in H2 using circe #1599

merged 1 commit into from
Jan 9, 2022

Conversation

andrzejressel
Copy link
Contributor

@andrzejressel andrzejressel commented Nov 25, 2021

This PR introduces support for JSON type in H2.

Code and tests are based on Postgres implementation.

I've tried using native H2 type ValueJson, but H2 really insists on using VARCHAR/bytearray:

    implicit val jsonPut: Put[Json] =
      Put.Advanced.other[ValueJson](
        NonEmptyList.of("JSON", "json")
      ).tcontramap(a =>
        ValueJson.fromJson(a.noSpaces)
      )
	  
	implicit val jsonGet: Get[Json] =
      Get.Advanced.other[ValueJson](
        NonEmptyList.of("JSON", "json")
      ).temap(a =>
        parse(a.getString).leftMap(_.show)
      )

@@@ vars

```scala
import doobie.h2.circe.json.implicits
Copy link
Collaborator

@jatcwang jatcwang Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean

Suggested change
import doobie.h2.circe.json.implicits
import doobie.h2.circe.json.implicits._

?

Also wondering whether we should mention that instances need to be created explicitly with h2DecoderGetT h2EncoderGetT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I was thinking about adding ._, but I haven't saw that in PostgreSQL implementation that I based my code on.

Regarding h2DecoderGetT and h2EncoderGetT, from what I understand they're used mostly for testing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding h2DecoderGetT and h2EncoderGetT, from what I understand they're used mostly for testing.

They are used for creating instances of Put[A]/Get[A] provided that you have Encoder[A]/Decoder[A]. This isn't automatically derived (i.e. not implicit def) because that can often lead to the wrong behaviour. (Just because you have an Encoder[A] does not mean you want to write A as a JSON column in the database).

Anyway I can do the doc improvements in another PR. Thanks for your contribution :)

@jatcwang jatcwang merged commit 5a80379 into typelevel:main Jan 9, 2022
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

Successfully merging this pull request may close these issues.

2 participants