jackc / pgtype

MIT License
300 stars 111 forks source link

Question: JSONBArray EncodeText #184

Closed waltcow closed 1 year ago

waltcow commented 1 year ago

Hello, I am using JSONBArray to store jsonb string array value

userRoles := make([]string, 2)
userRoles[0] = "admin"
userRoles[1] = "user"
userRolesJsonb := &pgtype.JSONBArray{}
userRolesJsonb.Set(userRoles)

JSONBArray parse details as follow

image

when value encode into byte[], it call EncodeText method, result as string is {admin, user}, expected value should be ["admin", "user"] , I am not sure if my usage is right ?

image
jackc commented 1 year ago

You are running into multiple subtle ambiguities.

First is PostgreSQL array vs. JSON array. JSONBArray represents a PostgreSQL array of jsonb not a JSON document that happens to be an array.

Next is whether a Go string contains encoded JSON or needs to be encoded into JSON. e.g. is [] already JSON or is it a string that needs to be encoded to "[]".

You probably want a jsonb that stores a JSON array rather than a PostgreSQL array of jsonb.

waltcow commented 1 year ago

Thanks for letting me know the difference between JSONBArray and JSON array, I will try jsonb to store my JSON array