-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Gson fails to parse simple map #501
Comments
Original comment by |
|
1 similar comment
|
Original comment by |
Original comment by |
Original comment by |
Original comment by |
Original comment by |
Original comment by |
I faced the same trouble. But I figured out it was because of we have map<enum, T> and ones server started returns new enum type, we had not had yet, so it leaded to this error, two nulls as keys. |
I'm serializing a map.. the json object only has one key in it. Every time I attempt to deserialize this, it throws this exception.
|
Since issue is still open, i'm guessing its still here. Here is how i solved my case: Here is an example code, similar to mine. Changed variable names etc. public static <T extends Enum, V> Map<T, V> fromOrdinal(Map<Integer, V> ordMap, Class<T> enumClass) {
Map<T, V> org = new HashMap<>();
for(int ord : ordMap.keySet()) {
org.put(enumClass.getEnumConstants()[ord], ordMap.get(ord));
}
return org;
}
public static <T extends Enum, V> Map<Integer, V> toOrdinal(Map<T, V> map) {
Map<Integer, V> ord = new HashMap<>();
for(T key : map.keySet()) {
ord.put(key.ordinal(), map.get(key));
}
return ord;
} |
I don't think there's enough information in this issue to be actionable. There may be more than one underlying issue, or there may be none and people are just not using the API right. (So there might be a documentation issue.) If you have a problem that seems like this one, please show an exception backtrace and the code you are using to deserialize. |
Here is the exact code i am testing it right now with the latest version "2.8.8": package net.mahmutkocas.dummy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.*;
public class Main {
public static void main(String[] args) {
TestClass testClass = new TestClass();
for(TestEnum s : TestEnum.values()) {
testClass.typesMap.put(s, "test");
}
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
try {
String json= gson.toJson(testClass);
System.out.println(json); // Output is provided at below with backtrace.
gson.fromJson(json, TestClass.class);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public enum TestEnum {
ENUM1,
ENUM2,
ENUM3,
ENUM4;
@Override
public String toString() {
return super.toString().toLowerCase(Locale.ENGLISH);
}
}
public static class TestClass {
public Map<TestEnum, String> typesMap = new HashMap<>();
}
} And here is the backtrace:
Hope it helps. |
Thanks @TheDoctorOne, that does look suspicious and we should look into it. |
See #1950 , as far as i tested, it works. And should have backwards compatibility. Edit: It checks on the highest level, where de-serialization done. It can be also done at serialization part which may be more convenient for future use. I don't know if that would be backwards compatible, if backwards compatibility matters to the project. |
Original issue reported on code.google.com by
phil...@meisen.net
on 15 Feb 2013 at 12:04The text was updated successfully, but these errors were encountered: