question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filter Optional JSONB columns mapped to CustomType

See original GitHub issue

I have an optional(nullable) JSONB column which is mapped to Scala Case class.

def myColumn: Rep[MyClass] = column[MyClass]("my_column")

Implicit mapping looks like:

case class MyClass(name: String, number: Int, boolean: Boolean)

def implicitJsonMapper[T: ClassTag](implicit format: OFormat[T]): JdbcType[T] with BaseTypedType[T] =
    MappedJdbcType.base[T, JsValue](Json.toJson(_), _.as[T])

implicit val myClassMapper = implicitJsonMapper[MyClass]

How can I filter based on json keys inside “myColumn” column?

Obviously this didn’t work: TableQuery[MyTable].filter(_.myColumn +> "boolean" === true)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tmingleicommented, Aug 1, 2020

@lakshmankollipara here’s the codes of PlayJsonImplicits,

  trait PlayJsonImplicits extends PlayJsonCodeGenSupport {
    import utils.JsonUtils.clean
    implicit val playJsonTypeMapper: JdbcType[JsValue] =
      new GenericJdbcType[JsValue](
        pgjson,
        (v) => Json.parse(v),
        (v) => clean(Json.stringify(v)),
        hasLiteralForm = false
      )

    implicit def playJsonColumnExtensionMethods(c: Rep[JsValue]) = {
        new JsonColumnExtensionMethods[JsValue, JsValue](c)
      }
    implicit def playJsonOptionColumnExtensionMethods(c: Rep[Option[JsValue]]) = {
        new JsonColumnExtensionMethods[JsValue, Option[JsValue]](c)
      }
  }

Here, the points are implicit def playJsonColumnExtensionMethods(c: Rep[JsValue]) and implicit def playJsonOptionColumnExtensionMethods(c: Rep[Option[JsValue]]).

So to support TableQuery[MyTable].filter(_.myColumn +> "boolean" === true), you need add definitions like these,

    implicit def myClass JsonColumnExtensionMethods(c: Rep[MyClass]) = {
        new JsonColumnExtensionMethods[MyClass, MyClass](c)
      }
    implicit def myClass JsonOptionColumnExtensionMethods(c: Rep[Option[MyClass]]) = {
        new JsonColumnExtensionMethods[MyClass, Option[MyClass]](c)
      }

I didn’t test it. But you can give it a try.

0reactions
tmingleicommented, Mar 29, 2021

@1gorsh ok! <g-emoji alias="smile" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f604.png" class="emoji">😄</g-emoji>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storing json, jsonb, hstore, xml, enum, ipaddr, etc fails with ...
Methods for implementing custom type handlers are driver-, language- and ORM-specific. Here's an example for Java and Hibernate for json .
Read more >
How to use PostgreSQL's JSONB data type with Hibernate
If you want to use a JSONB column with Hibernate 6, I have great news for you. ... In Hibernate 4 and 5,...
Read more >
12: 9.15. JSON Functions and Operators - PostgreSQL
A JSON null value is converted to a SQL null in all cases. · If the output column is of type json or...
Read more >
Slick Tutorial - Rock the JVM Blog
Slick Table maps the table fields to the case class. ... In the above code, we are filtering the personal column which is...
Read more >
Custom Types - MikroORM
Converts a value from its JS representation to its serialized JSON form of this type. ... we can use customType option and provide...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found