Colton Idle
10/03/2024, 12:33 PMjson
@Serializable
class Config(
val type: String = "abc",
val config: String = "", //this is json in postgres
)
If I try to select a config then I get this
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 83: Expected beginning of the string, but got { at path: $[0].config
I kinda figured that the postgres json is just a string. Do I need to set it as something else?Colton Idle
10/03/2024, 12:39 PM@Serializable
class Config(
val type: String = "abc",
val config: JsonElement //this is json in postgres
)
worked. Honestly a little surprised as I could've sworn that when I worked with other apis that stuffed json into an api response for a specific field. id get a string back. I wonder what actually comes over the wire. anyway. wont worry about it now. but if anyone wants to shine some light on this ill take it.Jan
10/03/2024, 12:49 PMjson
type does send the data as actual JSON. So if you have a json object in your row, then it will be a JsonObject. Otherwise you'd just have a text
column.
So you can just use a generic JsonElement
as your type or more precise a JsonObject
or even a custom data class.Colton Idle
10/03/2024, 12:55 PMJan
10/03/2024, 12:58 PMPostgrestResult#data
.Colton Idle
10/03/2024, 1:03 PMColton Idle
10/03/2024, 1:30 PMJan
10/03/2024, 1:46 PMtest
with a json column example
, just do something like this:
@Serializable
data class Test(val example: JsonObject)
val toInsert = Test(buildJsonObject {
put("key", "value")
})
supabase.from("test").insert(toInsert)
Or if the data in your json column is always following the same scheme, you can also use a custom data type:
@Serializable
data class Test(val example: MyDataType)
@Serializable
data class MyDataType(val key: String)
val toInsert = Test(MyDataType("value"))
supabase.from("test").insert(toInsert)
Jan
10/03/2024, 1:59 PMColton Idle
10/03/2024, 2:00 PMJson.encodeToJsonElement(myObjectThatShouldBeConvertedToJson)
Colton Idle
10/03/2024, 2:01 PMJan
10/03/2024, 2:02 PMColton Idle
10/03/2024, 2:06 PM