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

JSONText broken #266

Closed
gravis opened this issue Dec 6, 2016 · 4 comments
Closed

JSONText broken #266

gravis opened this issue Dec 6, 2016 · 4 comments

Comments

@gravis
Copy link

gravis commented Dec 6, 2016

Until recently, JSONText was acting like a string, and since 6708aed, we have a lot of issues with this type.
See the following code:

package main

import (
        "encoding/json"
        "fmt"
        "github.com/jmoiron/sqlx/types"
)

type X struct {
        Z types.JSONText
}

func main() {
        x := X{
                Z: types.JSONText("[]"),
        }
        bytes, _ := json.Marshal(x)
        fmt.Printf("%s\n", string(bytes))
}

before the commit, it was returning {"Z":[]} as expected, and after {"Z":"W10="}

@gravis
Copy link
Author

gravis commented Dec 6, 2016

Ok, got it.

// MarshalJSON returns the *j as the JSON encoding of j.
func (j *JSONText) MarshalJSON() ([]byte, error) {
	if len(*j) == 0 {
		*j = _EMPTY_JSON
	}
	return *j, nil
}

should be

// MarshalJSON returns the j as the JSON encoding of j.
func (j JSONText) MarshalJSON() ([]byte, error) {
	if len(j) == 0 {
		j = _EMPTY_JSON
	}
	return j, nil
}

Otherwise the func is never called.

gravis pushed a commit to gravis/sqlx that referenced this issue Dec 6, 2016
closes jmoiron#266

When using `JSONText` in a struct, the field is not a pointer, and therefore the func is never called, leading to unexpected results.
This change works with `*JSONText`, as well as `JSONText`.

Thanks.
@gravis
Copy link
Author

gravis commented Dec 7, 2016

@jmoiron 6708aed is really is breaking change for us, can you take a look at this?
Thanks

jmoiron added a commit that referenced this issue Dec 7, 2016
@jmoiron
Copy link
Owner

jmoiron commented Dec 7, 2016

I don't see a reason to have that mutation in the first place. f9b4c7a passed tests, can you make sure it works for you?

@gravis
Copy link
Author

gravis commented Dec 7, 2016

It works!
Thanks a lot :)

@gravis gravis closed this as completed Dec 7, 2016
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.

2 participants