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

JsonTreeWriter.put(): IllegalStateException #1592

Closed
cqjason opened this issue Oct 16, 2019 · 4 comments
Closed

JsonTreeWriter.put(): IllegalStateException #1592

cqjason opened this issue Oct 16, 2019 · 4 comments

Comments

@cqjason
Copy link

@cqjason cqjason commented Oct 16, 2019

After calling JsonTreeWriter.name(null), calling JsonTreeWriter.vule() will cause JsonTreeWriter.put(): IllegalStateException because of internal calling JsonTreeWriter.put(). So why not check if the param of name() is null? The check is done in super class!

@fmmmlee
Copy link
Contributor

@fmmmlee fmmmlee commented Nov 20, 2019

Can you provide more detail on how to reproduce the issue?

@cqjason
Copy link
Author

@cqjason cqjason commented Nov 21, 2019

public void testNullStringNameInSuperClass() throws IOException {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.beginObject();
jsonWriter.name(null); //throw java.lang.NullPointerException: name == null
//at com.google.gson.stream.JsonWriter.name(JsonWriter.java:386)
jsonWriter.value((String) null);
jsonWriter.endObject();
}

public void testNullStringName() throws IOException {
JsonWriter jsonWriter = new JsonTreeWriter();
jsonWriter.beginObject();
jsonWriter.name(null);
jsonWriter.value((String) null);//throw java.lang.IllegalStateException
//at com.google.gson.internal.bind.JsonTreeWriter.put(JsonTreeWriter.java:89)
//at com.google.gson.internal.bind.JsonTreeWriter.nullValue(JsonTreeWriter.java:153)
//at com.google.gson.internal.bind.JsonTreeWriter.value(JsonTreeWriter.java:146)
jsonWriter.endObject();
}

   I think it is appropriate behavior that throw NullPointerException when name(null). Because It can help client discover problem early. It has done in super class. But it‘s poor that JsonTreeWriter.name() didn't do it when JsonTreeWriter.name() override it in super class. 

@fmmmlee
Copy link
Contributor

@fmmmlee fmmmlee commented Nov 22, 2019

(Note: your reply has the response in code ticks, while the code is in normal text formatting - it's switched around for some reason)

I'll look more at the relevant areas of the code and most likely make a pull request; I agree that it makes sense for the behavior here to be consistent with the superclass unless there is a demonstrable use case in which the inconsistent behavior is necessary.

fmmmlee added a commit to fmmmlee/gson that referenced this issue Nov 22, 2019
adding null parameter check to name(), fixing the issue and being consistent with superclass behavior
@cqjason
Copy link
Author

@cqjason cqjason commented Nov 25, 2019

It's fine to fix it and it‘s very nice to commit it quickly. I‘ll close the issue for solving.

@cqjason cqjason closed this as completed Nov 25, 2019
fmmmlee added a commit to fmmmlee/gson that referenced this issue Nov 26, 2019
adding null parameter check to name(), fixing the issue and being consistent with superclass behavior
inder123 pushed a commit that referenced this issue Mar 2, 2020
adding null parameter check to name(), fixing the issue and being consistent with superclass behavior
tibor-universe pushed a commit to getuniverse/gson that referenced this issue Oct 20, 2021
adding null parameter check to name(), fixing the issue and being consistent with superclass behavior
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

No branches or pull requests

2 participants