From 56d1caa5407bfbe57c5563332b2463f4aae5caa3 Mon Sep 17 00:00:00 2001 From: Rico Yao Date: Fri, 26 Jul 2019 13:25:41 -0700 Subject: [PATCH] Java model TypeAdapter write method should not use delegateAdapter. --- Examples/Java/Sources/Board.java | 78 +++++- Examples/Java/Sources/Everything.java | 234 +++++++++++++++++- Examples/Java/Sources/Image.java | 36 ++- Examples/Java/Sources/Model.java | 24 +- Examples/Java/Sources/Pin.java | 120 ++++++++- Examples/Java/Sources/User.java | 78 +++++- .../Java/Sources/VariableSubtitution.java | 42 +++- Sources/Core/JavaModelRenderer.swift | 79 +++--- 8 files changed, 560 insertions(+), 131 deletions(-) diff --git a/Examples/Java/Sources/Board.java b/Examples/Java/Sources/Board.java index bf34b62b..92f8b874 100644 --- a/Examples/Java/Sources/Board.java +++ b/Examples/Java/Sources/Board.java @@ -465,17 +465,13 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!Board.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new BoardTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new BoardTypeAdapter(gson); } } public static class BoardTypeAdapter extends TypeAdapter { - final private BoardTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter dateTypeAdapter; private TypeAdapter imageTypeAdapter; private TypeAdapter> map_String__Integer_TypeAdapter; @@ -483,19 +479,77 @@ public static class BoardTypeAdapter extends TypeAdapter { private TypeAdapter> set_User_TypeAdapter; private TypeAdapter stringTypeAdapter; - public BoardTypeAdapter(@NonNull Gson gson, BoardTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public BoardTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, Board value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getUidIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("id"), value.uid); + } + if (value.getContributorsIsSet()) { + if (this.set_User_TypeAdapter == null) { + this.set_User_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.set_User_TypeAdapter.write(writer.name("contributors"), value.contributors); + } + if (value.getCountsIsSet()) { + if (this.map_String__Integer_TypeAdapter == null) { + this.map_String__Integer_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__Integer_TypeAdapter.write(writer.name("counts"), value.counts); + } + if (value.getCreatedAtIsSet()) { + if (this.dateTypeAdapter == null) { + this.dateTypeAdapter = this.gson.getAdapter(Date.class).nullSafe(); + } + this.dateTypeAdapter.write(writer.name("created_at"), value.createdAt); + } + if (value.getCreatorIsSet()) { + if (this.map_String__String_TypeAdapter == null) { + this.map_String__String_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__String_TypeAdapter.write(writer.name("creator"), value.creator); + } + if (value.getCreatorURLIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("creator_url"), value.creatorURL); + } + if (value.getDescriptionIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("description"), value.description); + } + if (value.getImageIsSet()) { + if (this.imageTypeAdapter == null) { + this.imageTypeAdapter = this.gson.getAdapter(Image.class).nullSafe(); + } + this.imageTypeAdapter.write(writer.name("image"), value.image); + } + if (value.getNameIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("name"), value.name); + } + if (value.getUrlIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("url"), value.url); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Examples/Java/Sources/Everything.java b/Examples/Java/Sources/Everything.java index 03ceeaf8..79b65fae 100644 --- a/Examples/Java/Sources/Everything.java +++ b/Examples/Java/Sources/Everything.java @@ -1523,17 +1523,13 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!Everything.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new EverythingTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new EverythingTypeAdapter(gson); } } public static class EverythingTypeAdapter extends TypeAdapter { - final private EverythingTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter booleanTypeAdapter; private TypeAdapter dateTypeAdapter; private TypeAdapter doubleTypeAdapter; @@ -1568,19 +1564,233 @@ public static class EverythingTypeAdapter extends TypeAdapter { private TypeAdapter stringTypeAdapter; private TypeAdapter userTypeAdapter; - public EverythingTypeAdapter(@NonNull Gson gson, EverythingTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public EverythingTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, Everything value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getArrayPropIsSet()) { + if (this.list_Object_TypeAdapter == null) { + this.list_Object_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.list_Object_TypeAdapter.write(writer.name("array_prop"), value.arrayProp); + } + if (value.getBooleanPropIsSet()) { + if (this.booleanTypeAdapter == null) { + this.booleanTypeAdapter = this.gson.getAdapter(Boolean.class).nullSafe(); + } + this.booleanTypeAdapter.write(writer.name("boolean_prop"), value.booleanProp); + } + if (value.getCharEnumIsSet()) { + if (this.everythingCharEnumTypeAdapter == null) { + this.everythingCharEnumTypeAdapter = this.gson.getAdapter(EverythingCharEnum.class).nullSafe(); + } + this.everythingCharEnumTypeAdapter.write(writer.name("char_enum"), value.charEnum); + } + if (value.getDatePropIsSet()) { + if (this.dateTypeAdapter == null) { + this.dateTypeAdapter = this.gson.getAdapter(Date.class).nullSafe(); + } + this.dateTypeAdapter.write(writer.name("date_prop"), value.dateProp); + } + if (value.getIntEnumIsSet()) { + if (this.everythingIntEnumTypeAdapter == null) { + this.everythingIntEnumTypeAdapter = this.gson.getAdapter(EverythingIntEnum.class).nullSafe(); + } + this.everythingIntEnumTypeAdapter.write(writer.name("int_enum"), value.intEnum); + } + if (value.getIntPropIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("int_prop"), value.intProp); + } + if (value.getListPolymorphicValuesIsSet()) { + if (this.list_Object_TypeAdapter == null) { + this.list_Object_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.list_Object_TypeAdapter.write(writer.name("list_polymorphic_values"), value.listPolymorphicValues); + } + if (value.getListWithListAndOtherModelValuesIsSet()) { + if (this.list_List_User__TypeAdapter == null) { + this.list_List_User__TypeAdapter = this.gson.getAdapter(new TypeToken>>(){}).nullSafe(); + } + this.list_List_User__TypeAdapter.write(writer.name("list_with_list_and_other_model_values"), value.listWithListAndOtherModelValues); + } + if (value.getListWithMapAndOtherModelValuesIsSet()) { + if (this.list_Map_String__User__TypeAdapter == null) { + this.list_Map_String__User__TypeAdapter = this.gson.getAdapter(new TypeToken>>(){}).nullSafe(); + } + this.list_Map_String__User__TypeAdapter.write(writer.name("list_with_map_and_other_model_values"), value.listWithMapAndOtherModelValues); + } + if (value.getListWithObjectValuesIsSet()) { + if (this.list_String_TypeAdapter == null) { + this.list_String_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.list_String_TypeAdapter.write(writer.name("list_with_object_values"), value.listWithObjectValues); + } + if (value.getListWithOtherModelValuesIsSet()) { + if (this.list_User_TypeAdapter == null) { + this.list_User_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.list_User_TypeAdapter.write(writer.name("list_with_other_model_values"), value.listWithOtherModelValues); + } + if (value.getListWithPrimitiveValuesIsSet()) { + if (this.list_Integer_TypeAdapter == null) { + this.list_Integer_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.list_Integer_TypeAdapter.write(writer.name("list_with_primitive_values"), value.listWithPrimitiveValues); + } + if (value.getMapPolymorphicValuesIsSet()) { + if (this.map_String__EverythingMapPolymorphicValues_TypeAdapter == null) { + this.map_String__EverythingMapPolymorphicValues_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__EverythingMapPolymorphicValues_TypeAdapter.write(writer.name("map_polymorphic_values"), value.mapPolymorphicValues); + } + if (value.getMapPropIsSet()) { + if (this.map_String__Object_TypeAdapter == null) { + this.map_String__Object_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__Object_TypeAdapter.write(writer.name("map_prop"), value.mapProp); + } + if (value.getMapWithListAndOtherModelValuesIsSet()) { + if (this.map_String__List_User__TypeAdapter == null) { + this.map_String__List_User__TypeAdapter = this.gson.getAdapter(new TypeToken>>(){}).nullSafe(); + } + this.map_String__List_User__TypeAdapter.write(writer.name("map_with_list_and_other_model_values"), value.mapWithListAndOtherModelValues); + } + if (value.getMapWithMapAndOtherModelValuesIsSet()) { + if (this.map_String__Map_String__Object__TypeAdapter == null) { + this.map_String__Map_String__Object__TypeAdapter = this.gson.getAdapter(new TypeToken>>(){}).nullSafe(); + } + this.map_String__Map_String__Object__TypeAdapter.write(writer.name("map_with_map_and_other_model_values"), value.mapWithMapAndOtherModelValues); + } + if (value.getMapWithObjectValuesIsSet()) { + if (this.map_String__String_TypeAdapter == null) { + this.map_String__String_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__String_TypeAdapter.write(writer.name("map_with_object_values"), value.mapWithObjectValues); + } + if (value.getMapWithOtherModelValuesIsSet()) { + if (this.map_String__User_TypeAdapter == null) { + this.map_String__User_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__User_TypeAdapter.write(writer.name("map_with_other_model_values"), value.mapWithOtherModelValues); + } + if (value.getMapWithPrimitiveValuesIsSet()) { + if (this.map_String__Integer_TypeAdapter == null) { + this.map_String__Integer_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__Integer_TypeAdapter.write(writer.name("map_with_primitive_values"), value.mapWithPrimitiveValues); + } + if (value.getNsintegerEnumIsSet()) { + if (this.everythingNsintegerEnumTypeAdapter == null) { + this.everythingNsintegerEnumTypeAdapter = this.gson.getAdapter(EverythingNsintegerEnum.class).nullSafe(); + } + this.everythingNsintegerEnumTypeAdapter.write(writer.name("nsinteger_enum"), value.nsintegerEnum); + } + if (value.getNsuintegerEnumIsSet()) { + if (this.everythingNsuintegerEnumTypeAdapter == null) { + this.everythingNsuintegerEnumTypeAdapter = this.gson.getAdapter(EverythingNsuintegerEnum.class).nullSafe(); + } + this.everythingNsuintegerEnumTypeAdapter.write(writer.name("nsuinteger_enum"), value.nsuintegerEnum); + } + if (value.getNumberPropIsSet()) { + if (this.doubleTypeAdapter == null) { + this.doubleTypeAdapter = this.gson.getAdapter(Double.class).nullSafe(); + } + this.doubleTypeAdapter.write(writer.name("number_prop"), value.numberProp); + } + if (value.getOtherModelPropIsSet()) { + if (this.userTypeAdapter == null) { + this.userTypeAdapter = this.gson.getAdapter(User.class).nullSafe(); + } + this.userTypeAdapter.write(writer.name("other_model_prop"), value.otherModelProp); + } + if (value.getPolymorphicPropIsSet()) { + if (this.everythingPolymorphicPropTypeAdapter == null) { + this.everythingPolymorphicPropTypeAdapter = this.gson.getAdapter(EverythingPolymorphicProp.class).nullSafe(); + } + this.everythingPolymorphicPropTypeAdapter.write(writer.name("polymorphic_prop"), value.polymorphicProp); + } + if (value.getSetPropIsSet()) { + if (this.set_Object_TypeAdapter == null) { + this.set_Object_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.set_Object_TypeAdapter.write(writer.name("set_prop"), value.setProp); + } + if (value.getSetPropWithOtherModelValuesIsSet()) { + if (this.set_User_TypeAdapter == null) { + this.set_User_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.set_User_TypeAdapter.write(writer.name("set_prop_with_other_model_values"), value.setPropWithOtherModelValues); + } + if (value.getSetPropWithPrimitiveValuesIsSet()) { + if (this.set_Integer_TypeAdapter == null) { + this.set_Integer_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.set_Integer_TypeAdapter.write(writer.name("set_prop_with_primitive_values"), value.setPropWithPrimitiveValues); + } + if (value.getSetPropWithValuesIsSet()) { + if (this.set_String_TypeAdapter == null) { + this.set_String_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.set_String_TypeAdapter.write(writer.name("set_prop_with_values"), value.setPropWithValues); + } + if (value.getShortEnumIsSet()) { + if (this.everythingShortEnumTypeAdapter == null) { + this.everythingShortEnumTypeAdapter = this.gson.getAdapter(EverythingShortEnum.class).nullSafe(); + } + this.everythingShortEnumTypeAdapter.write(writer.name("short_enum"), value.shortEnum); + } + if (value.getStringEnumIsSet()) { + if (this.everythingStringEnumTypeAdapter == null) { + this.everythingStringEnumTypeAdapter = this.gson.getAdapter(EverythingStringEnum.class).nullSafe(); + } + this.everythingStringEnumTypeAdapter.write(writer.name("string_enum"), value.stringEnum); + } + if (value.getStringPropIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("string_prop"), value.stringProp); + } + if (value.getTypeIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("type"), value.type); + } + if (value.getUnsignedCharEnumIsSet()) { + if (this.everythingUnsignedCharEnumTypeAdapter == null) { + this.everythingUnsignedCharEnumTypeAdapter = this.gson.getAdapter(EverythingUnsignedCharEnum.class).nullSafe(); + } + this.everythingUnsignedCharEnumTypeAdapter.write(writer.name("unsigned_char_enum"), value.unsignedCharEnum); + } + if (value.getUnsignedIntEnumIsSet()) { + if (this.everythingUnsignedIntEnumTypeAdapter == null) { + this.everythingUnsignedIntEnumTypeAdapter = this.gson.getAdapter(EverythingUnsignedIntEnum.class).nullSafe(); + } + this.everythingUnsignedIntEnumTypeAdapter.write(writer.name("unsigned_int_enum"), value.unsignedIntEnum); + } + if (value.getUnsignedShortEnumIsSet()) { + if (this.everythingUnsignedShortEnumTypeAdapter == null) { + this.everythingUnsignedShortEnumTypeAdapter = this.gson.getAdapter(EverythingUnsignedShortEnum.class).nullSafe(); + } + this.everythingUnsignedShortEnumTypeAdapter.write(writer.name("unsigned_short_enum"), value.unsignedShortEnum); + } + if (value.getUriPropIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("uri_prop"), value.uriProp); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Examples/Java/Sources/Image.java b/Examples/Java/Sources/Image.java index d2b65ce4..89c976b6 100644 --- a/Examples/Java/Sources/Image.java +++ b/Examples/Java/Sources/Image.java @@ -215,33 +215,45 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!Image.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new ImageTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new ImageTypeAdapter(gson); } } public static class ImageTypeAdapter extends TypeAdapter { - final private ImageTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter integerTypeAdapter; private TypeAdapter stringTypeAdapter; - public ImageTypeAdapter(@NonNull Gson gson, ImageTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public ImageTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, Image value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getHeightIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("height"), value.height); + } + if (value.getUrlIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("url"), value.url); + } + if (value.getWidthIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("width"), value.width); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Examples/Java/Sources/Model.java b/Examples/Java/Sources/Model.java index f0dc0fa8..280be223 100644 --- a/Examples/Java/Sources/Model.java +++ b/Examples/Java/Sources/Model.java @@ -141,32 +141,32 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!Model.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new ModelTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new ModelTypeAdapter(gson); } } public static class ModelTypeAdapter extends TypeAdapter { - final private ModelTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter stringTypeAdapter; - public ModelTypeAdapter(@NonNull Gson gson, ModelTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public ModelTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, Model value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getUidIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("id"), value.uid); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Examples/Java/Sources/Pin.java b/Examples/Java/Sources/Pin.java index b273b4e6..284336b4 100644 --- a/Examples/Java/Sources/Pin.java +++ b/Examples/Java/Sources/Pin.java @@ -735,17 +735,13 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!Pin.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new PinTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new PinTypeAdapter(gson); } } public static class PinTypeAdapter extends TypeAdapter { - final private PinTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter boardTypeAdapter; private TypeAdapter dateTypeAdapter; private TypeAdapter imageTypeAdapter; @@ -758,19 +754,119 @@ public static class PinTypeAdapter extends TypeAdapter { private TypeAdapter pinInStockTypeAdapter; private TypeAdapter stringTypeAdapter; - public PinTypeAdapter(@NonNull Gson gson, PinTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public PinTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, Pin value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getAttributionIsSet()) { + if (this.map_String__String_TypeAdapter == null) { + this.map_String__String_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__String_TypeAdapter.write(writer.name("attribution"), value.attribution); + } + if (value.getAttributionObjectsIsSet()) { + if (this.list_PinAttributionObjects_TypeAdapter == null) { + this.list_PinAttributionObjects_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.list_PinAttributionObjects_TypeAdapter.write(writer.name("attribution_objects"), value.attributionObjects); + } + if (value.getBoardIsSet()) { + if (this.boardTypeAdapter == null) { + this.boardTypeAdapter = this.gson.getAdapter(Board.class).nullSafe(); + } + this.boardTypeAdapter.write(writer.name("board"), value.board); + } + if (value.getColorIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("color"), value.color); + } + if (value.getCountsIsSet()) { + if (this.map_String__Integer_TypeAdapter == null) { + this.map_String__Integer_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__Integer_TypeAdapter.write(writer.name("counts"), value.counts); + } + if (value.getCreatedAtIsSet()) { + if (this.dateTypeAdapter == null) { + this.dateTypeAdapter = this.gson.getAdapter(Date.class).nullSafe(); + } + this.dateTypeAdapter.write(writer.name("created_at"), value.createdAt); + } + if (value.getCreatorIsSet()) { + if (this.map_String__User_TypeAdapter == null) { + this.map_String__User_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__User_TypeAdapter.write(writer.name("creator"), value.creator); + } + if (value.getDescriptionIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("description"), value.description); + } + if (value.getUidIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("id"), value.uid); + } + if (value.getImageIsSet()) { + if (this.imageTypeAdapter == null) { + this.imageTypeAdapter = this.gson.getAdapter(Image.class).nullSafe(); + } + this.imageTypeAdapter.write(writer.name("image"), value.image); + } + if (value.getInStockIsSet()) { + if (this.pinInStockTypeAdapter == null) { + this.pinInStockTypeAdapter = this.gson.getAdapter(PinInStock.class).nullSafe(); + } + this.pinInStockTypeAdapter.write(writer.name("in_stock"), value.inStock); + } + if (value.getLinkIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("link"), value.link); + } + if (value.getMediaIsSet()) { + if (this.map_String__String_TypeAdapter == null) { + this.map_String__String_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__String_TypeAdapter.write(writer.name("media"), value.media); + } + if (value.getNoteIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("note"), value.note); + } + if (value.getTagsIsSet()) { + if (this.list_Map_String__Object__TypeAdapter == null) { + this.list_Map_String__Object__TypeAdapter = this.gson.getAdapter(new TypeToken>>(){}).nullSafe(); + } + this.list_Map_String__Object__TypeAdapter.write(writer.name("tags"), value.tags); + } + if (value.getUrlIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("url"), value.url); + } + if (value.getVisualSearchAttrsIsSet()) { + if (this.map_String__Object_TypeAdapter == null) { + this.map_String__Object_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__Object_TypeAdapter.write(writer.name("visual_search_attrs"), value.visualSearchAttrs); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Examples/Java/Sources/User.java b/Examples/Java/Sources/User.java index 5eb28fe7..f90303b3 100644 --- a/Examples/Java/Sources/User.java +++ b/Examples/Java/Sources/User.java @@ -469,36 +469,90 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!User.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new UserTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new UserTypeAdapter(gson); } } public static class UserTypeAdapter extends TypeAdapter { - final private UserTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter dateTypeAdapter; private TypeAdapter imageTypeAdapter; private TypeAdapter> map_String__Integer_TypeAdapter; private TypeAdapter stringTypeAdapter; private TypeAdapter userEmailFrequencyTypeAdapter; - public UserTypeAdapter(@NonNull Gson gson, UserTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public UserTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, User value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getBioIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("bio"), value.bio); + } + if (value.getCountsIsSet()) { + if (this.map_String__Integer_TypeAdapter == null) { + this.map_String__Integer_TypeAdapter = this.gson.getAdapter(new TypeToken>(){}).nullSafe(); + } + this.map_String__Integer_TypeAdapter.write(writer.name("counts"), value.counts); + } + if (value.getCreatedAtIsSet()) { + if (this.dateTypeAdapter == null) { + this.dateTypeAdapter = this.gson.getAdapter(Date.class).nullSafe(); + } + this.dateTypeAdapter.write(writer.name("created_at"), value.createdAt); + } + if (value.getEmailFrequencyIsSet()) { + if (this.userEmailFrequencyTypeAdapter == null) { + this.userEmailFrequencyTypeAdapter = this.gson.getAdapter(UserEmailFrequency.class).nullSafe(); + } + this.userEmailFrequencyTypeAdapter.write(writer.name("email_frequency"), value.emailFrequency); + } + if (value.getFirstNameIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("first_name"), value.firstName); + } + if (value.getUidIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("id"), value.uid); + } + if (value.getImageIsSet()) { + if (this.imageTypeAdapter == null) { + this.imageTypeAdapter = this.gson.getAdapter(Image.class).nullSafe(); + } + this.imageTypeAdapter.write(writer.name("image"), value.image); + } + if (value.getLastNameIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("last_name"), value.lastName); + } + if (value.getTypeIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("type"), value.type); + } + if (value.getUsernameIsSet()) { + if (this.stringTypeAdapter == null) { + this.stringTypeAdapter = this.gson.getAdapter(String.class).nullSafe(); + } + this.stringTypeAdapter.write(writer.name("username"), value.username); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Examples/Java/Sources/VariableSubtitution.java b/Examples/Java/Sources/VariableSubtitution.java index 30e8d311..dad18bf7 100644 --- a/Examples/Java/Sources/VariableSubtitution.java +++ b/Examples/Java/Sources/VariableSubtitution.java @@ -253,32 +253,50 @@ public TypeAdapter create(@NonNull Gson gson, @NonNull TypeToken typeT if (!VariableSubtitution.class.isAssignableFrom(typeToken.getRawType())) { return null; } - return (TypeAdapter) new VariableSubtitutionTypeAdapter(gson, this, typeToken); + return (TypeAdapter) new VariableSubtitutionTypeAdapter(gson); } } public static class VariableSubtitutionTypeAdapter extends TypeAdapter { - final private VariableSubtitutionTypeAdapterFactory factory; final private Gson gson; - final private TypeToken typeToken; - private TypeAdapter delegateTypeAdapter; - private TypeAdapter integerTypeAdapter; - public VariableSubtitutionTypeAdapter(@NonNull Gson gson, VariableSubtitutionTypeAdapterFactory factory, TypeToken typeToken) { - this.factory = factory; + public VariableSubtitutionTypeAdapter(Gson gson) { this.gson = gson; - this.typeToken = typeToken; } @Override public void write(@NonNull JsonWriter writer, VariableSubtitution value) throws IOException { - if (this.delegateTypeAdapter == null) { - this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken); + if (value == null) { + writer.nullValue(); + return; + } + writer.beginObject(); + if (value.getAllocPropIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("alloc_prop"), value.allocProp); + } + if (value.getCopyPropIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("copy_prop"), value.copyProp); + } + if (value.getMutableCopyPropIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("mutable_copy_prop"), value.mutableCopyProp); + } + if (value.getNewPropIsSet()) { + if (this.integerTypeAdapter == null) { + this.integerTypeAdapter = this.gson.getAdapter(Integer.class).nullSafe(); + } + this.integerTypeAdapter.write(writer.name("new_prop"), value.newProp); } - writer.setSerializeNulls(false); - this.delegateTypeAdapter.write(writer, value); } @Nullable diff --git a/Sources/Core/JavaModelRenderer.swift b/Sources/Core/JavaModelRenderer.swift index af45db76..18cf594b 100644 --- a/Sources/Core/JavaModelRenderer.swift +++ b/Sources/Core/JavaModelRenderer.swift @@ -247,7 +247,7 @@ public struct JavaModelRenderer: JavaFileRenderer { JavaIR.ifBlock(condition: "!" + className + ".class.isAssignableFrom(typeToken.getRawType())") { [ "return null;", ] }, - "return (TypeAdapter) new " + className + "TypeAdapter(gson, this, typeToken);", + "return (TypeAdapter) new " + className + "TypeAdapter(gson);", ] }] } @@ -258,35 +258,6 @@ public struct JavaModelRenderer: JavaFileRenderer { } func renderTypeAdapterProperties() -> [[JavaIR.Property]] { - let factory = JavaIR.Property( - annotations: [], - modifiers: [.final, .private], - type: className + "TypeAdapterFactory", - name: "factory", - initialValue: "" - ) - let gson = JavaIR.Property( - annotations: [], - modifiers: [.final, .private], - type: "Gson", - name: "gson", - initialValue: "" - ) - let typeToken = JavaIR.Property( - annotations: [], - modifiers: [.final, .private], - type: "TypeToken", - name: "typeToken", - initialValue: "" - ) - let delegate = JavaIR.Property( - annotations: [], - modifiers: [.private], - type: "TypeAdapter<" + className + ">", - name: "delegateTypeAdapter", - initialValue: "" - ) - let types = Set(transitiveProperties.map { param, schemaObj in unwrappedTypeFromSchema(param, schemaObj.schema) }).sorted() @@ -301,18 +272,24 @@ public struct JavaModelRenderer: JavaFileRenderer { ) } - return [[factory, gson, typeToken, delegate], typeAdapters] + let gson = JavaIR.Property( + annotations: [], + modifiers: [.final, .private], + type: "Gson", + name: "gson", + initialValue: "" + ) + + return [[gson] + typeAdapters] } func renderTypeAdapterMethods() -> [JavaIR.Method] { let constructor = JavaIR.method( annotations: [], [.public], - className + "TypeAdapter(@NonNull Gson gson, " + className + "TypeAdapterFactory factory, TypeToken typeToken)" + className + "TypeAdapter(Gson gson)" ) { [ - "this.factory = factory;", "this.gson = gson;", - "this.typeToken = typeToken;", ] } @@ -322,12 +299,24 @@ public struct JavaModelRenderer: JavaFileRenderer { "void write(@NonNull JsonWriter writer, " + className + " value)", ["IOException"] ) { [ - JavaIR.ifBlock(condition: "this.delegateTypeAdapter == null") { [ - "this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken);", + JavaIR.ifBlock(condition: "value == null") { [ + "writer.nullValue();", + "return;", ] }, - "writer.setSerializeNulls(false);", - "this.delegateTypeAdapter.write(writer, value);", - ] + "writer.beginObject();", + ] + + transitiveProperties.map { param, schemaObj in + let type = unwrappedTypeFromSchema(param, schemaObj.schema) + let typeAdapterVariableName = typeAdapterVariableNameForType(type) + return JavaIR.ifBlock(condition: "value.get" + Languages.java.snakeCaseToCapitalizedPropertyName(param) + "IsSet()") { [ + // Creates TypeAdapter if necessary + JavaIR.ifBlock(condition: "this." + typeAdapterVariableName + " == null") { [ + schemaObj.schema.isJavaCollection ? "this.\(typeAdapterVariableName) = this.gson.getAdapter(new TypeToken<\(type)>(){}).nullSafe();" : "this.\(typeAdapterVariableName) = this.gson.getAdapter(\(type).class).nullSafe();", + ] }, + // Write to JsonWriter + "this." + typeAdapterVariableNameForType(unwrappedTypeFromSchema(param, schemaObj.schema)) + ".write(writer.name(\"" + param + "\"), value." + Languages.java.snakeCaseToPropertyName(param) + ");", + ] } + } } let read = JavaIR.methodThatThrows( @@ -348,19 +337,15 @@ public struct JavaModelRenderer: JavaFileRenderer { JavaIR.switchBlock(variableToCheck: "name", defaultBody: ["reader.skipValue();"]) { transitiveProperties.map { param, schemaObj in let type = unwrappedTypeFromSchema(param, schemaObj.schema) - let variableName = typeAdapterVariableNameForType(type) - var line: String - if schemaObj.schema.isJavaCollection { - line = "this.\(variableName) = this.gson.getAdapter(new TypeToken<" + type + ">(){}).nullSafe();" - } else { - line = "this.\(variableName) = this.gson.getAdapter(\(type).class).nullSafe();" - } + let typeAdapterVariableName = typeAdapterVariableNameForType(type) return JavaIR.Case( variableEquals: "\"\(param)\"", body: [ + // Creates TypeAdapter if necessary JavaIR.ifBlock(condition: "this." + typeAdapterVariableNameForType(unwrappedTypeFromSchema(param, schemaObj.schema)) + " == null") { [ - line, + schemaObj.schema.isJavaCollection ? "this.\(typeAdapterVariableName) = this.gson.getAdapter(new TypeToken<\(type)>(){}).nullSafe();" : "this.\(typeAdapterVariableName) = this.gson.getAdapter(\(type).class).nullSafe();", ] }, + // Read from JsonReader "builder.set" + Languages.java.snakeCaseToCapitalizedPropertyName(param) + "(this." + typeAdapterVariableNameForType(unwrappedTypeFromSchema(param, schemaObj.schema)) + ".read(reader));", ] )