Skip to content

JSONText zero value is invalid JSON #216

@bfallik

Description

@bfallik

Generally in Go the zero value of a type is usable as that type. JSONText does not follow that pattern:

func Test_JSONText(t *testing.T) {
    v := types.JSONText{}
    assert.NoError(t, func() error { _, err := v.Value(); return err }())
}

yields

--- FAIL: Test_JSONText (0.00s)
        Location:       template_test.go:20
        Error:          No error is expected but got unexpected end of JSON input

Any interest in a patch that treats the empty slice as a valid JSON value like null? e.g.

diff --git a/go/src/_vendor/github.com/jmoiron/sqlx/types/types.go b/go/src/_ven
dor/github.com/jmoiron/sqlx/types/types.go
index 53848bc..2308443 100644
--- a/go/src/_vendor/github.com/jmoiron/sqlx/types/types.go
+++ b/go/src/_vendor/github.com/jmoiron/sqlx/types/types.go
@@ -97,6 +99,10 @@ func (j *JSONText) Scan(src interface{}) error {

 // Unmarshal unmarshal's the json in j to v, as in json.Unmarshal.
 func (j *JSONText) Unmarshal(v interface{}) error {
+       if len([]byte(*j)) == 0 {
+               v = "null"
+               return nil
+       }
        return json.Unmarshal([]byte(*j), v)
 }

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