JsonConverter { public static <T> T jsonBlobToObject(@NonNull String jsonBlob, @NonNull Class<T> clazz) { T object = null; try { object = mapper.readValue(jsonBlob, clazz); } catch (Exception e) { LOG.error("Exception in parsing out json blob {}", jsonBlob, e); } return object; }  static T jsonBlobToObject(@NonNull String jsonBlob, @NonNull Class<T> clazz); static String objectToJsonBlob(@NonNull T object);  }
@Test public void parseJsonSchema() { String regionBlob = "{ \"realmKey\" : \"RTC\",\"realmDomainComponent\" : \"oraclecloud-foobar.com\",\"regionKey\" : \"rrr\",\"regionIdentifier\" : \"us-rrr-1\"}"; RegionSchema regionSchema = JsonConverter.jsonBlobToObject(regionBlob, RegionSchema.class); assertThat(regionSchema.getRegionIdentifier(), is("us-rrr-1")); assertThat(regionSchema.getRegionKey(), is("rrr")); assertThat(regionSchema.getRealmKey(), is("RTC")); assertThat(regionSchema.getRealmDomainComponent(), is("oraclecloud-foobar.com")); }
@Test public void parseJsonSchemaInvalid() { String regionBlob = "{ \"realmKey\" :: \"RTC\",\"realmDomainComponent\" : \"oraclecloud-foobar.com\",\"regionKey\" : \"rrr\",\"regionIdentifier\" : \"us-rrr-1\"}"; RegionSchema regionSchema = JsonConverter.jsonBlobToObject(regionBlob, RegionSchema.class); assertNull(regionSchema); }
@Test public void parseJsonArray() { String jsonArr = "[{ \"realmKey\" : \"RYT\",\"realmDomainComponent\" : \"oracleclour-bar.com\",\"regionKey\" : \"atl\",\"regionIdentifier\" : \"ap-atl-1\"},{ \"realmKey\" :\"YUT\",\"realmDomainComponent\" : \"oraclefoobar-cloud.com\",\"regionKey\" : \"LAV\",\"regionIdentifier\" : \"us-lav-1\"}]"; RegionSchema[] regionSchemas = JsonConverter.jsonBlobToObject(jsonArr, RegionSchema[].class); assertThat(regionSchemas.length, is(2)); }