java - How can I make an i18n compliant EnumTypeAdapter? -


i'm working on nice solution internationalize enums gson deserialize (.tojson).

for have it:

private static final class genericenumtypeadapter<t extends enum<t>> extends typeadapter<t> {      private resourcebundle bundle = resourcebundle.getbundle("messages");      private class<t> classoft;      public genericenumtypeadapter(class<t> classoft) {         this.classoft = classoft;     }      public t read(jsonreader in) throws ioexception {         if (in.peek() == jsontoken.null) {             in.nextnull();             return null;         }         return enum.valueof(classoft, in.nextstring());     }      public void write(jsonwriter out, t value) throws ioexception {         out.value(value == null ? null : bundle.getstring("enum." + value.getclass().getsimplename() + "."                 + value.name()));     } } 

the problem of solution is: each enum should register new adapter:

gsonbuilder.registertypeadapter(eventsensorstate.class,     new genericenumtypeadapter<>(firstenum.class) 

do has idea better?

use typeadapterfactory generate adapters. see how implement typeadapterfactory in gson?

to convert typeadapter typeadapterfactory, key detecting class properly, , using create method. warning: solution register every type of enum in system; may have tweak work enums implement particular interface, or register enum classes subclass, etc. created enumgenerator class of work of reading conversion, should able figure out on own.

public class enumadapterfactory implements typeadapterfactory {   private final resourcebundle bundle;   private final enumgenerator generator;    public enumadapterfactory(resourcebundle bundle, enumgenerator generator) {     this.bundle = bundle;     this.generator = generator;   }    @suppresswarnings("unchecked")   @override   public <t> typeadapter<t> create(gson gson, typetoken<t> type) {     if (!enum.class.isassignablefrom(type.getrawtype())) return null;      return (typeadapter<t>) new genericenumtypeadapter();   }    private final class genericenumtypeadapter<t extends enum<t>> extends typeadapter<t> {     public t read(jsonreader in) throws ioexception {       if (in.peek() == jsontoken.null) {         in.nextnull();         return null;       }       return generator.create(in.nextstring());     }      public void write(jsonwriter out, t value) throws ioexception {       if(value == null) {         out.nullvalue();         return;       }       out.value(bundle.getstring("enum."              + value.getclass().getsimplename() + "."             + value.name()));     }   } } 

and enumgenerator's interface:

public interface enumgenerator {   <t extends enum<t>> t create(string nextstring); } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

php - How do you embed a video into a custom theme on WordPress? -