How do I cast a json field to a string in exposed DSL (using postgres db)?
d
How do I cast a json field to a string in exposed DSL (using postgres db)?
d
Yeah, but I don't need to convert the json to classes, I just need the raw json as a string in Kotlin... (I saw that issue before I asked...)
b
jsonb > Exposed: varchar type
or text
d
I have
val json = varchar("json", 10000)
, but it still gives me a
org.postgresql.util.PGobject cannot be cast to java.lang.String
error...
b
hmm. It worked for me like. I’ll be at home with a home project - I’ll see how I wrote there in the example ...
d
That's why I wanted to just cast it to a string at the sql query level, so the db will actually return a String...
This seems to work:
Copy code
val jsonTextField = MyEntity.json.castTo<String>(TextColumnType())
no docs on this, but 👍🏼 to Exposed for allowing this... just a bit funny that is seems the column type won't matter in such a case... and is alright wrong in the first place and will explode if used ...
t
@dave08 do you get an error when storing or retrieving a json from DB?
d
Yeah, but that just happened to be a View that created the json field, not a real table, so I really just had needed the jsonb as text. Was using barchar or text supposed to work?
t
I would advise to use text as it allows to store string data of any size.
1392 Views