@Test public void extractFieldValidTest() { Gson gson = new GsonBuilder().create(); AzureFieldsExtractor azureFieldsExtractor = new AzureFieldsExtractor(gson); FieldsTestClass originalCopy = createTestClass("a value", 42, "a different value", 8675309); String jsonString = gson.toJson(originalCopy); JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class); Optional<String> field1Value = azureFieldsExtractor.extractField(jsonObject, FieldsTestClass.FIELD_1); assertTrue(field1Value.isPresent(), "Field 1 was not present"); assertEquals(originalCopy.getField1(), field1Value.get()); Optional<Integer> field2Value = azureFieldsExtractor.extractField(jsonObject, FieldsTestClass.FIELD_2); assertTrue(field2Value.isPresent(), "Field 2 was not present"); assertEquals(originalCopy.getField2(), field2Value.get()); Optional<FieldsTestInnerClass> field3Value = azureFieldsExtractor.extractField(jsonObject, FieldsTestClass.FIELD_3); assertTrue(field3Value.isPresent(), "Field 3 was not present"); FieldsTestInnerClass field3Object = field3Value.get(); assertEquals(originalCopy.getField3().getInnerField1(), field3Object.getInnerField1()); assertEquals(originalCopy.getField3().getInnerField2(), field3Object.getInnerField2()); }
public <T> Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition) { JsonElement foundField = fieldsObject.get(fieldDefinition.getFieldName()); if (null != foundField) { T fieldValue = gson.fromJson(foundField, fieldDefinition.getFieldType()); return Optional.of(fieldValue); } return Optional.empty(); }
AzureFieldsExtractor { public <T> Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition) { JsonElement foundField = fieldsObject.get(fieldDefinition.getFieldName()); if (null != foundField) { T fieldValue = gson.fromJson(foundField, fieldDefinition.getFieldType()); return Optional.of(fieldValue); } return Optional.empty(); } }
AzureFieldsExtractor { public <T> Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition) { JsonElement foundField = fieldsObject.get(fieldDefinition.getFieldName()); if (null != foundField) { T fieldValue = gson.fromJson(foundField, fieldDefinition.getFieldType()); return Optional.of(fieldValue); } return Optional.empty(); } AzureFieldsExtractor(Gson gson); }
AzureFieldsExtractor { public <T> Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition) { JsonElement foundField = fieldsObject.get(fieldDefinition.getFieldName()); if (null != foundField) { T fieldValue = gson.fromJson(foundField, fieldDefinition.getFieldType()); return Optional.of(fieldValue); } return Optional.empty(); } AzureFieldsExtractor(Gson gson); Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition); }
AzureFieldsExtractor { public <T> Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition) { JsonElement foundField = fieldsObject.get(fieldDefinition.getFieldName()); if (null != foundField) { T fieldValue = gson.fromJson(foundField, fieldDefinition.getFieldType()); return Optional.of(fieldValue); } return Optional.empty(); } AzureFieldsExtractor(Gson gson); Optional<T> extractField(JsonObject fieldsObject, AzureFieldDefinition<T> fieldDefinition);  }