why do two seemingly identical hashmaps have different behavior when serialized by gson?
Clash Royale CLAN TAG#URR8PPP
up vote
15
down vote
favorite
input:
public static void main(String args)
final String key = "some key";
final String value = "some value";
Map<String, String> map1 = new HashMap<String, String>()put(key, value);;
System.out.println(new Gson().toJson(map1) + " " + map1.get(key));
Map<String, String> map2 = new HashMap<>();
map2.put(key, value);
System.out.println(new Gson().toJson(map2) + " " + map2.get(key));
output:
null some value
"some key":"some value" some value
Process finished with exit code 0
java hashmap gson
add a comment |Â
up vote
15
down vote
favorite
input:
public static void main(String args)
final String key = "some key";
final String value = "some value";
Map<String, String> map1 = new HashMap<String, String>()put(key, value);;
System.out.println(new Gson().toJson(map1) + " " + map1.get(key));
Map<String, String> map2 = new HashMap<>();
map2.put(key, value);
System.out.println(new Gson().toJson(map2) + " " + map2.get(key));
output:
null some value
"some key":"some value" some value
Process finished with exit code 0
java hashmap gson
5
Don't abuse "double brace initialization"...
â user202729
Aug 10 at 6:40
also related: stackoverflow.com/questions/1958636/â¦
â Hulk
Aug 10 at 9:36
add a comment |Â
up vote
15
down vote
favorite
up vote
15
down vote
favorite
input:
public static void main(String args)
final String key = "some key";
final String value = "some value";
Map<String, String> map1 = new HashMap<String, String>()put(key, value);;
System.out.println(new Gson().toJson(map1) + " " + map1.get(key));
Map<String, String> map2 = new HashMap<>();
map2.put(key, value);
System.out.println(new Gson().toJson(map2) + " " + map2.get(key));
output:
null some value
"some key":"some value" some value
Process finished with exit code 0
java hashmap gson
input:
public static void main(String args)
final String key = "some key";
final String value = "some value";
Map<String, String> map1 = new HashMap<String, String>()put(key, value);;
System.out.println(new Gson().toJson(map1) + " " + map1.get(key));
Map<String, String> map2 = new HashMap<>();
map2.put(key, value);
System.out.println(new Gson().toJson(map2) + " " + map2.get(key));
output:
null some value
"some key":"some value" some value
Process finished with exit code 0
java hashmap gson
edited Aug 10 at 4:21
IÃ Âya Bursov
17.6k32443
17.6k32443
asked Aug 10 at 4:18
dQw4w9WyXcQ
784
784
5
Don't abuse "double brace initialization"...
â user202729
Aug 10 at 6:40
also related: stackoverflow.com/questions/1958636/â¦
â Hulk
Aug 10 at 9:36
add a comment |Â
5
Don't abuse "double brace initialization"...
â user202729
Aug 10 at 6:40
also related: stackoverflow.com/questions/1958636/â¦
â Hulk
Aug 10 at 9:36
5
5
Don't abuse "double brace initialization"...
â user202729
Aug 10 at 6:40
Don't abuse "double brace initialization"...
â user202729
Aug 10 at 6:40
also related: stackoverflow.com/questions/1958636/â¦
â Hulk
Aug 10 at 9:36
also related: stackoverflow.com/questions/1958636/â¦
â Hulk
Aug 10 at 9:36
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
21
down vote
accepted
For map1
, you have created an anonymous subclass. Assuming your class that contains main()
is called ExampleClass
, then:
System.out.println(map1.getClass().getName())
prints out:
ExampleClass$1
Whereas printing the class for map2
yields:
java.util.HashMap
As to the exact reason that Gson doesn't serialise it - Gson uses the classname to lookup a converter. If you instead serialise it using:
System.out.println(new Gson().toJson(map1, HashMap.class));
... it works as expected.
1
Relevant documentation.
â VGR
Aug 10 at 4:35
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
21
down vote
accepted
For map1
, you have created an anonymous subclass. Assuming your class that contains main()
is called ExampleClass
, then:
System.out.println(map1.getClass().getName())
prints out:
ExampleClass$1
Whereas printing the class for map2
yields:
java.util.HashMap
As to the exact reason that Gson doesn't serialise it - Gson uses the classname to lookup a converter. If you instead serialise it using:
System.out.println(new Gson().toJson(map1, HashMap.class));
... it works as expected.
1
Relevant documentation.
â VGR
Aug 10 at 4:35
add a comment |Â
up vote
21
down vote
accepted
For map1
, you have created an anonymous subclass. Assuming your class that contains main()
is called ExampleClass
, then:
System.out.println(map1.getClass().getName())
prints out:
ExampleClass$1
Whereas printing the class for map2
yields:
java.util.HashMap
As to the exact reason that Gson doesn't serialise it - Gson uses the classname to lookup a converter. If you instead serialise it using:
System.out.println(new Gson().toJson(map1, HashMap.class));
... it works as expected.
1
Relevant documentation.
â VGR
Aug 10 at 4:35
add a comment |Â
up vote
21
down vote
accepted
up vote
21
down vote
accepted
For map1
, you have created an anonymous subclass. Assuming your class that contains main()
is called ExampleClass
, then:
System.out.println(map1.getClass().getName())
prints out:
ExampleClass$1
Whereas printing the class for map2
yields:
java.util.HashMap
As to the exact reason that Gson doesn't serialise it - Gson uses the classname to lookup a converter. If you instead serialise it using:
System.out.println(new Gson().toJson(map1, HashMap.class));
... it works as expected.
For map1
, you have created an anonymous subclass. Assuming your class that contains main()
is called ExampleClass
, then:
System.out.println(map1.getClass().getName())
prints out:
ExampleClass$1
Whereas printing the class for map2
yields:
java.util.HashMap
As to the exact reason that Gson doesn't serialise it - Gson uses the classname to lookup a converter. If you instead serialise it using:
System.out.println(new Gson().toJson(map1, HashMap.class));
... it works as expected.
answered Aug 10 at 4:34
Greg Kopff
10.5k53762
10.5k53762
1
Relevant documentation.
â VGR
Aug 10 at 4:35
add a comment |Â
1
Relevant documentation.
â VGR
Aug 10 at 4:35
1
1
Relevant documentation.
â VGR
Aug 10 at 4:35
Relevant documentation.
â VGR
Aug 10 at 4:35
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51778907%2fwhy-do-two-seemingly-identical-hashmaps-have-different-behavior-when-serialized%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
5
Don't abuse "double brace initialization"...
â user202729
Aug 10 at 6:40
also related: stackoverflow.com/questions/1958636/â¦
â Hulk
Aug 10 at 9:36