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

_examples/json is misleading #1312

Open
Jaculabilis opened this issue Jan 21, 2025 · 0 comments · May be fixed by #1313
Open

_examples/json is misleading #1312

Jaculabilis opened this issue Jan 21, 2025 · 0 comments · May be fixed by #1313

Comments

@Jaculabilis
Copy link

I believe the example at _examples/json/json.go may be misleading. It declares a table as create table foo (tag jsonb), but since the table isn't declared as strict, the type is merely advisory.

In this case, because the marshal/unmarshal code converts the type to and from a string, the value that SQLite receives is actually a string, and therefore it is stored as json, i.e. text, not as jsonb, i.e. blob. If you change the table declaration to create table foo (tag blob) strict and rerun the example, you will get the error cannot store TEXT value in BLOB column foo.tag, which indicates that the value was converted to a string and not bytes.

The example can be made to work with the strict table, and therefore with values that are really jsonb, with the following changes:

  • The insert uses jsonb() to convert the marshalled string to the binary type: insert into foo(tag) values(jsonb(?)).
  • The select tag converts the binary type to the string type: select json(tag). The same isn't needed for select tag->>'country' because the return type isn't JSON.
  • The update uses jsonb() on the tag: update foo set tag = jsonb(?)

Either the table should be created as create table foo (tag json) or the queries should be updated to use jsonb as above.

@Jaculabilis Jaculabilis linked a pull request Jan 21, 2025 that will close this issue
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 a pull request may close this issue.

1 participant