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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:58:11+00:00 2026-06-11T20:58:11+00:00

AFAIK the most flexible gson customization is possible via TypeAdapterFactory , however it may

  • 0

AFAIK the most flexible gson customization is possible via TypeAdapterFactory, however it may get needlessly complicated. It forces me to write for each handled class both read and write, while sometimes only one method is really needed. Moreover, sometimes a JsonSerializer and/or JsonDeserializer were much easier to write, e.g. like here. This leads me to these questions:

  • Is it possible to write a TypeAdapter which simply delegates one of its methods (e.g. writing of ImmutableList to writing of List)?
  • Is it possible to somehow use JsonSerializer and/or JsonDeserializer together with the TypeAdapterFactory? Alternatively, is there a factory for them?
  • 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-06-11T20:58:12+00:00Added an answer on June 11, 2026 at 8:58 pm

    It is possible to create a TypeAdapter that delegates one of its methods. This use case is an important part of the API, and there’s a getDelegateAdapter() method for just this purpose. Pass this as the first argument to getDelegateAdapter which will return the adapter that takes precedence after the current factory.

    TypeAdapterFactory immutableListFactory = new TypeAdapterFactory() {
      @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        if (!(type.getType() instanceof ParameterizedType)
            || !type.getRawType().equals(ImmutableList.class)) {
          return null;
        }
    
        ParameterizedType parameterizedType = (ParameterizedType) type.getType();
        TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
        TypeAdapter<?> elementAdapter = gson.getAdapter(
            TypeToken.get(parameterizedType.getActualTypeArguments()[0]));
        return new ImmutableListAdapter(delegate, elementAdapter);
      }
    
      class ImmutableListAdapter<E> extends TypeAdapter<ImmutableList<E>> {
        private TypeAdapter<List<E>> delegate;
        private TypeAdapter<E> element;
    
        ImmutableListAdapter(TypeAdapter<List<E>> delegate, TypeAdapter<E> element) {
          this.delegate = delegate;
          this.element = element;
        }
    
        @Override public void write(JsonWriter out, ImmutableList<E> value) throws IOException {
          delegate.write(out, value);
        }
    
        @Override public ImmutableList<E> read(JsonReader in) throws IOException {
          if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return null;
          }
          ImmutableList.Builder<E> builder = ImmutableList.builder();
          in.beginArray();
          while (in.hasNext()) {
            builder.add(element.read(in));
          }
          in.endArray();
          return builder.build();
        }
      }
    };
    

    You can mix and match JsonSerializer/JsonDeserializer with TypeAdapterFactory, but not directly. The simplest way is to call back into Gson to serialize child values in your class. In this example we’d change the inner loop to this:

          while (in.hasNext()) {
            builder.add(gson.<E>fromJson(in, elementType));
          }
    

    The main difference between JsonSerializer/JsonDeserializer and TypeAdapter is how many stages it takes to go from JSON to your object model. With JsonSerializer/JsonDeserializer objects are first converted to Gson’s DOM model (JsonElement etc.) and then converted into your object model. With TypeAdapter, the intermediate step is skipped.

    This makes the type adapter code a little trickier to read and write, so you should prefer it only for optimized code.

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

Sidebar

Related Questions

Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} AFAIK, there
AFAIK, extern keyword should be used for declaration and no value can be associated
AFAIK, for pointers/references static_cast, if a class definition is not visible to compiler at
Afaik, you can change/manipulate browser settings in Mozilla/Netscape browsers. For Instance netscape.security.PrivilegeManager.enablePrivilege('someprivilege'); Of course
AFAIK when reducing an array we can only output once variable at the end
This question is about the public key that every .NET assembly has AFAIK (looking
The program below prints: my name is:null my name is:null Someclass static init AFAIK
I'd like to know if derivatives in glsl are working on GL_LINES primitives. AFAIK
We are using Silverlight 4 (we must use it. :p ) with VS2010. AFAIK
In LINQ Where is a streaming operator. Where-as OrderByDescending is a non-streaming operator. AFAIK,

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.