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

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

Closed
hhniao opened this issue Aug 4, 2018 · 13 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hhniao
Copy link

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 4, 2018
@ALTree ALTree added this to the Go1.12 milestone Aug 4, 2018
@meirf meirf changed the title json: invalid use of ,string struct tag, trying to unmarshal "" into int encoding/json: invalid use of ,string struct tag, trying to unmarshal "" into int Aug 4, 2018
@erikdubbelboer
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

@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
Copy link
Author

hhniao commented Sep 11, 2018

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

@erikdubbelboer
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

hhniao commented Sep 11, 2018

may i should close this issue?

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

I think that it should parse empty string as a default parameter (like nil)
If we got an empty string it means that it's default (default for string is "").
If we got a default string it means that we want to use default value for int, isn't it?

@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@dataflowjs
Copy link

so?! Update i coming?! Working with not my api and cant improve it... plix add! or change json:"field,string,omitempty" to json:"field,omitempty,string" if field is empty just leave (,string tag didnt launched)...

@erikdubbelboer
Copy link
Contributor

@asiman161 then what if in Go I have a string but in my JSON I get 0 (not a string). Should it then also result in an empty string seeing as 0 is the default for numbers?

@bsbak I don't think the Go team will fix this as it's not a bug.
If you're working with a broken JSON API you should either ask them to fix their API, you should use json.RawMessage or you implement your own type that can be both a number and an empty string: https://play.golang.org/p/nWl2toE2RAE

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@agnivade
Copy link
Contributor

The error message is there for a reason. Empty strings do not mean 0. There are various ways you can fix this already mentioned above.

  • Fix the frontend.
  • Change the type to string, and then handle "" to 0 later in the code.
  • Use a custom Unmarshaler.

Probably others that I can't think of now.

Closing as this is WAI.

/cc @mvdan if I missed something.

@golang golang locked and limited conversation to collaborators Oct 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

10 participants