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

encoding/json: invalid use of ,string struct tag, trying to unmarshal "" into int #26800

Open
hhniao opened this Issue Aug 4, 2018 · 9 comments

Comments

Projects
None yet
4 participants
@hhniao

hhniao commented Aug 4, 2018

https://play.golang.org/p/TabRbDnegfc
#7046

What version of Go are you using (go version)?

go version go1.10 windows/amd64

@ALTree ALTree added this to the Go1.12 milestone Aug 4, 2018

@meirf meirf changed the title from json: invalid use of ,string struct tag, trying to unmarshal "" into int to encoding/json: invalid use of ,string struct tag, trying to unmarshal "" into int Aug 4, 2018

@erikdubbelboer

This comment has been minimized.

Contributor

erikdubbelboer commented Aug 31, 2018

Doesn't it make sense that this is an error? You're telling it to decode a string into an integer but then don't provide a valid integer in the string.

@hhniao

This comment has been minimized.

hhniao commented Sep 11, 2018

@erikdubbelboer
usualy, we received data from web frontend.
for example,
the html code like this:

<select name="demo">
    <option value="">please select</option>
    <option value="1">option1</option>
    <option value="2">option2</option>
</select>

some time, them submit json data like this:

{ 
    "demo": ""
}

or them submit form data like this demo=""
ignore form data at the moment.
let we take a look json data. it is an empty string in the json data.

the go code:

type Data struct {

    Demo int `json:",string"`

}

zero value of string is "". so, in my opinion, received a zero value of string convert to json. must not get a error.

@erikdubbelboer

This comment has been minimized.

Contributor

erikdubbelboer commented Sep 11, 2018

@hhniao if your values are supposed to be numbers then it should obviously be <option value="0">please select</option>. Forms also don't submit JSON so there is some part of your code which takes the values of the form and turns it into JSON. This part is where you would normally make sure the JSON values are of the correct type.

@hhniao

This comment has been minimized.

hhniao commented Sep 11, 2018

because we cant change code of frontend .
we just try to re-write backend using golang from php.

@erikdubbelboer

This comment has been minimized.

Contributor

erikdubbelboer commented Sep 11, 2018

I don't think you not being able to fix your frontend means that the Go JSON parser should make exceptions for invalid JSON.

@hhniao

This comment has been minimized.

hhniao commented Sep 11, 2018

no, no, no.
for JSON. it is valid.
{
"demo": ""
}
{
"demo": "0"
}

and i think json:",string". it is must able to parse a valid string. any string. empty string also it is a string, valid string. is it?

@erikdubbelboer

This comment has been minimized.

Contributor

erikdubbelboer commented Sep 11, 2018

It's a valid string yes but not a valid number. What if it's "a" which is also a valid string, should it accept that as well? What should the value be then? 0, or 10 or ...?
I think it should produce an error with all strings that are not numbers, like it is now.

@hhniao

This comment has been minimized.

hhniao commented Sep 11, 2018

hmm, you are right. thought php can be parse empty string. but it is not standard or valid. for strict of standard, we must write "0" instead "".
thanks.

@hhniao

This comment has been minimized.

hhniao commented Sep 11, 2018

may i should close this issue?

@ianlancetaylor ianlancetaylor modified the milestones: Go1.12, Go1.13 Dec 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment