Skip to content

_examples/json is misleading #1312

Open
@Jaculabilis

Description

@Jaculabilis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions