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 4087834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:54:18+00:00 2026-05-20T18:54:18+00:00

I have a Map containing a mixture of types like in this simple example

  • 0

I have a Map containing a mixture of types like in this simple example

final Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("a", 1);
map.put("b", "a");
map.put("c", 2);
final Gson gson = new Gson();
final String string = gson.toJson(map);
final Type type = new TypeToken<LinkedHashMap<String, Object>>(){}.getType();
final Map<Object, Object> map2 = gson.fromJson(string, type);
for (final Entry<Object, Object> entry : map2.entrySet()) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

What I get back are plain Objects, no Integers, no Strings. The output looks like

a : java.lang.Object@48d19bc8
b : java.lang.Object@394a8cd1
c : java.lang.Object@4d630ab9

Can I fix it somehow? I’d expect that such simple cases will be handled correctly by default.

I know that the information about the type can’t always be preserved, and possibly 1 and "1" means exactly the same in JSON. However, returning plain content-less objects just makes no sense to me.

Update: The serialized version (i.e. the string above) looks fine:

{"a":1,"b":"a","c":2}
  • 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-20T18:54:19+00:00Added an answer on May 20, 2026 at 6:54 pm

    Gson isn’t that smart. Rather provide a clear and static data structure in flavor of a Javabean class so that Gson understands what type the separate properties are supposed to be deserialized to.

    E.g.

    public class Data {
        private Integer a;
        private String b;
        private Integer c;
        // ...
    }
    

    in combination with

    Data data1 = new Data(1, "a", 2);
    String json = gson.toJson(data1);
    Data data2 = gson.fromJson(json, Data.class);
    

    Update: as per the comments, the keyset seems to be not fixed (although you seem to be able to convert it manually afterwards without knowing the structure beforehand). You could create a custom deserializer. Here’s a quick’n’dirty example.

    public class ObjectDeserializer implements JsonDeserializer<Object> {
    
        @Override
        public Object deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
            String value = element.getAsString();
            try {
                return Long.valueOf(value);
            } catch (NumberFormatException e) {
                return value;
            }
        }
    
    }
    

    which you use as follows:

    final Gson gson = new GsonBuilder().registerTypeAdapter(Object.class, new ObjectDeserializer()).create();
    // ... 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
In C++, I have a map<string, string> , containing an unknown number of entries.
I have a Seq containing objects of a class that looks like this: class
I have seen an example of Singleton class object where it has a map
I'm trying to map a Dictionary containing Lists. I have the following set of
I have a view containing a UIWebView which is loading a google map (so
I have a map of a building and I'd like to programmatically highlight specific
I have a map declared as follows: map < string , list < string
I would like to create a zoomable image map containing irregular shapes upon which
I have a class containing a std::unique_ptr<> and I want to put instances of

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.