@Test(expected = JsonParseException.class) public void testAddressAsPrimitive() { JsonPrimitive jsonPrimitive = new JsonPrimitive("addressLine1"); adapter.deserialize(jsonPrimitive, Address.class, null); }
@Override public Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("Address must be a JSON object", e); } String addressLine1 = obj.getAsJsonPrimitive("addressLine1").getAsString(); String city = obj.getAsJsonPrimitive("city").getAsString(); String country = obj.getAsJsonPrimitive("country").getAsString(); JsonPrimitive addressLine2Obj = obj.getAsJsonPrimitive("addressLine2"), postalCodeObj = obj.getAsJsonPrimitive("postalCode"), regionObj = obj.getAsJsonPrimitive("region"); String addressLine2 = addressLine2Obj != null ? addressLine2Obj.getAsString() : ""; String postalCode = postalCodeObj != null ? postalCodeObj.getAsString() : ""; String region = regionObj != null ? regionObj.getAsString() : ""; return new Address(addressLine1, addressLine2, postalCode, city, region, country); }
AddressTypeAdapterDeserializer implements TypeAdapterDeserializer<Address> { @Override public Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("Address must be a JSON object", e); } String addressLine1 = obj.getAsJsonPrimitive("addressLine1").getAsString(); String city = obj.getAsJsonPrimitive("city").getAsString(); String country = obj.getAsJsonPrimitive("country").getAsString(); JsonPrimitive addressLine2Obj = obj.getAsJsonPrimitive("addressLine2"), postalCodeObj = obj.getAsJsonPrimitive("postalCode"), regionObj = obj.getAsJsonPrimitive("region"); String addressLine2 = addressLine2Obj != null ? addressLine2Obj.getAsString() : ""; String postalCode = postalCodeObj != null ? postalCodeObj.getAsString() : ""; String region = regionObj != null ? regionObj.getAsString() : ""; return new Address(addressLine1, addressLine2, postalCode, city, region, country); } }
AddressTypeAdapterDeserializer implements TypeAdapterDeserializer<Address> { @Override public Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("Address must be a JSON object", e); } String addressLine1 = obj.getAsJsonPrimitive("addressLine1").getAsString(); String city = obj.getAsJsonPrimitive("city").getAsString(); String country = obj.getAsJsonPrimitive("country").getAsString(); JsonPrimitive addressLine2Obj = obj.getAsJsonPrimitive("addressLine2"), postalCodeObj = obj.getAsJsonPrimitive("postalCode"), regionObj = obj.getAsJsonPrimitive("region"); String addressLine2 = addressLine2Obj != null ? addressLine2Obj.getAsString() : ""; String postalCode = postalCodeObj != null ? postalCodeObj.getAsString() : ""; String region = regionObj != null ? regionObj.getAsString() : ""; return new Address(addressLine1, addressLine2, postalCode, city, region, country); }  }
AddressTypeAdapterDeserializer implements TypeAdapterDeserializer<Address> { @Override public Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("Address must be a JSON object", e); } String addressLine1 = obj.getAsJsonPrimitive("addressLine1").getAsString(); String city = obj.getAsJsonPrimitive("city").getAsString(); String country = obj.getAsJsonPrimitive("country").getAsString(); JsonPrimitive addressLine2Obj = obj.getAsJsonPrimitive("addressLine2"), postalCodeObj = obj.getAsJsonPrimitive("postalCode"), regionObj = obj.getAsJsonPrimitive("region"); String addressLine2 = addressLine2Obj != null ? addressLine2Obj.getAsString() : ""; String postalCode = postalCodeObj != null ? postalCodeObj.getAsString() : ""; String region = regionObj != null ? regionObj.getAsString() : ""; return new Address(addressLine1, addressLine2, postalCode, city, region, country); }  @Override Class<?> getAdaptedType(); @Override Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext); }
AddressTypeAdapterDeserializer implements TypeAdapterDeserializer<Address> { @Override public Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("Address must be a JSON object", e); } String addressLine1 = obj.getAsJsonPrimitive("addressLine1").getAsString(); String city = obj.getAsJsonPrimitive("city").getAsString(); String country = obj.getAsJsonPrimitive("country").getAsString(); JsonPrimitive addressLine2Obj = obj.getAsJsonPrimitive("addressLine2"), postalCodeObj = obj.getAsJsonPrimitive("postalCode"), regionObj = obj.getAsJsonPrimitive("region"); String addressLine2 = addressLine2Obj != null ? addressLine2Obj.getAsString() : ""; String postalCode = postalCodeObj != null ? postalCodeObj.getAsString() : ""; String region = regionObj != null ? regionObj.getAsString() : ""; return new Address(addressLine1, addressLine2, postalCode, city, region, country); }  @Override Class<?> getAdaptedType(); @Override Address deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext);  }