so i was looking to convert a bundle object to json using gson by implementing this bundletypeadapter that i found in the FUNF project.
my issue is that i’m not entirely sure how to properly implement that block of code. The only way that currently makes sense to me results in a circular dependency.
how you normally would implement a new TypeAdapter:
...
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer());
gsonBuilder.registerTypeAdapter(Location.class, new LocationSerializer());
Gson gson = gsonBuilder.create();
...
or, what i would expect in this case:
...
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Bundle.class, new BundleTypeAdapter());
Gson gson = gsonBuilder.create();
...
my issue is that the only constructor that BundleTypeAdapter has is BundleTypeAdapter(Gson gson) and I’m not quite sure how to go about passing in an object that I have yet to create or instantiate…
any ideas?
Gson has a solution for the case you describe, TypeAdapterFactory. BundleTypeAdapter defines the needed factory for you in a static member variable called FACTORY. To use it, call: