-
Notifications
You must be signed in to change notification settings - Fork 6k
[Java] Inheritance support Java client (Retrofit Gson) #4729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
fb09f98
c60703a
663273f
10d61f4
b16e878
53bec92
7d256d1
a1b4043
8ac8b80
28b6473
e351dd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,17 +20,24 @@ | |
| import com.google.gson.internal.bind.util.ISO8601Utils; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import com.google.gson.JsonElement; | ||
| import io.gsonfire.GsonFireBuilder; | ||
| import io.gsonfire.TypeSelector; | ||
| import org.threeten.bp.LocalDate; | ||
| import org.threeten.bp.OffsetDateTime; | ||
| import org.threeten.bp.format.DateTimeFormatter; | ||
|
|
||
| import io.swagger.client.model.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.StringReader; | ||
| import java.lang.reflect.Type; | ||
| import java.text.DateFormat; | ||
| import java.text.ParseException; | ||
| import java.text.ParsePosition; | ||
| import java.util.Date; | ||
| import java.util.Map; | ||
| import java.util.HashMap; | ||
|
|
||
| public class JSON { | ||
| private Gson gson; | ||
|
|
@@ -39,8 +46,25 @@ public class JSON { | |
| private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); | ||
| private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); | ||
|
|
||
| public static GsonBuilder createGson() { | ||
| GsonFireBuilder fireBuilder = new GsonFireBuilder() | ||
| .registerTypeSelector(Animal.class, new TypeSelector() { | ||
| @Override | ||
| public Class getClassForElement(JsonElement readElement) { | ||
| Map classByDiscriminatorValue = new HashMap(); | ||
| classByDiscriminatorValue.put("Cat".toUpperCase(), Cat.class); | ||
| classByDiscriminatorValue.put("Dog".toUpperCase(), Dog.class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed my case with abstract superclass would be most typical. I added support for it and sample is including also |
||
| String discriminatorField = "className"; | ||
| String discriminatorValue = readElement.getAsJsonObject().get(discriminatorField).getAsString(); | ||
|
||
| return (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase()); | ||
|
||
| } | ||
| }) | ||
| ; | ||
| return fireBuilder.createGsonBuilder(); | ||
| } | ||
|
|
||
| public JSON() { | ||
| gson = new GsonBuilder() | ||
| gson = createGson() | ||
| .registerTypeAdapter(Date.class, dateTypeAdapter) | ||
| .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter) | ||
| .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra line