Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4107676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:32:50+00:00 2026-05-20T21:32:50+00:00

I want to gson and de-gson a hashmap with generics. I can do it

  • 0

I want to gson and de-gson a hashmap with generics.
I can do it successfully when my key is a string but not if it is an
object.
I have the two code part here . the first doesnt work and the second
does.

// doesnt work works
 try {
       TradableBean tradable = new TradableBean("Tradable");
       PositionBean position = new PositionBean(tradable);
       Map<TradableBean, PositionBean> map1 = new HashMap<TradableBean, PositionBean>();
       map1.put(tradable, position);
       String json1 = gson.toJson(map1);
       Map<TradableBean, PositionBean> map2 = gson.fromJson(json1, new TypeToken<Map<TradableBean, PositionBean>>(){}.getType());
       System.out.println(map2);
   } catch (Exception e) {
       System.out.println("failed");
   }

and now the working part –

 // works - with string
   try {
       String tradable = new String("Tradable");
       PositionBean position = new PositionBean(new TradableBean("Tradable"));
       Map<String, PositionBean> map1 = new HashMap<String,PositionBean>();
       map1.put(tradable, position);
       String json1 = gson.toJson(map1);
       Map<String, PositionBean> map2 = gson.fromJson(json1, new TypeToken<Map<String, PositionBean>>() {}.getType());
       System.out.println(map2);
   } catch (Exception e) {
       System.out.println("failed");
   }

and in the TradableBean I have:

@Override public int hashCode() {
   return getId();} 
@Override public boolean equals(Object obj) {
   boolean equals = false;
   if (obj instanceof AccountBean){
       TradableBean tradable_p = (TradableBean)obj;
       if (getId()==tradable_p.getId()){
           equals = true;
       }
   }
   return equals;

}

the exception:

com.google.gson.JsonParseException: Expecting object found:
"TradableBean{id=0, tradableName='Tradable', moneyMultiplier=1,
expirationDate=null}"
       at
com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:
100)
       at
com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:
150)
       at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
       at
com.google.gson.JsonDeserializationContextDefault.fromJsonPrimitive(JsonDeserializationContextDefault.java:
84)
       at
com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:
53)
       at com.google.gson.DefaultTypeAdapters
$MapTypeAdapter.deserialize(DefaultTypeAdapters.java:531)
       at com.google.gson.DefaultTypeAdapters
$MapTypeAdapter.deserialize(DefaultTypeAdapters.java:498)
       at
com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:
50)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:32:51+00:00Added an answer on May 20, 2026 at 9:32 pm

    I think you need to write your own custom serializator.

    Update: the tell-tale is the exception:

    com.google.gson.JsonParseException: Expecting object found: "TradableBean{id=0, tradableName='Tradable', moneyMultiplier=1, expirationDate=null}"
    

    The prepending string “TradableBean” seems to be written by a serialization that uses some sort of toString(). the Deserialization expected an object of the form {id=0,... }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to transfer a list object via Google Gson, but I don't know
Hi every one actually I want to make a com.google.gson.JsonArray of data:[1,2,3,4,5].How can i
want to have a Hyperlink-Button in a gridView in which I can display a
Want to code a key pad for an calculator. What I want to make
I want to deserialize a JSON object (using GSON, because I already use it
I have some difficulties with json deserialization using GSon and I hope somebody can
I have the following JSON object as a String: [{Add1:101,Description:null,ID:1,Name:Bundesverfassung,Short:BV},{Add1:220,Description:null,ID:2,Name:Obligationenrecht,Short:OR},{Add1:210,Description:null,ID:3,Name:Schweizerisches Zivilgesetzbuch,Short:ZGB},{Add1:311_0,Description:null,ID:4,Name:Schweizerisches Strafgesetzbuch,Short:null}] Now i
I don't like Jackson. I want to use ajax but with Google Gson. So
I want to parse JSON arrays and using gson. Firstly, I can log JSON
I want to parse this JSON string using Gson {name:name,type:[a,b,c]} is it possible? 1st

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.