why do two seemingly identical hashmaps have different behavior when serialized by gson?

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question


















  • 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














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






share|improve this question


















  • 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












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






share|improve this question














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








share|improve this question













share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer
















  • 1




    Relevant documentation.
    – VGR
    Aug 10 at 4:35










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer
















  • 1




    Relevant documentation.
    – VGR
    Aug 10 at 4:35














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.






share|improve this answer
















  • 1




    Relevant documentation.
    – VGR
    Aug 10 at 4:35












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 10 at 4:34









Greg Kopff

10.5k53762




10.5k53762







  • 1




    Relevant documentation.
    – VGR
    Aug 10 at 4:35












  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

What does second last employer means? [closed]

One-line joke