PortTelemetrySerializationAdapter implements JsonSerializer<PortTelemetry>,
        JsonDeserializer<PortTelemetry> { @Override public PortTelemetry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { return PortTelemetry.valueOf(json.getAsString()); } else if (json.isJsonArray()) { for (JsonElement element : json.getAsJsonArray()) { String elementAsString = element.getAsString(); if (PortTelemetry.contains(elementAsString)) { return PortTelemetry.valueOf(elementAsString); } } } throw new JsonParseException("Unknown port telemetry value type. " + "Expected value types are String or String[] (String array)"); }  @Override PortTelemetry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(PortTelemetry src, Type typeOfSrc, JsonSerializationContext context);  }
@Test public void shouldDeserializeStringValueToPortTelemetry() { String portTelemetryValueAsJsonValue = "\"ENABLED\""; PortTelemetry portTelemetry = adapter.deserialize(parser.parse(portTelemetryValueAsJsonValue), null, null); assertThat(portTelemetry, is(PortTelemetry.ENABLED)); }
@Test public void shouldDeserializeStringArrayValueToPortTelemetry() { String portTelemetryValueAsJsonArray = "[\"PortTelemetry\",\"ENABLED\"]"; PortTelemetry portTelemetry = adapter.deserialize(parser.parse(portTelemetryValueAsJsonArray), null, null); assertThat(portTelemetry, is(PortTelemetry.ENABLED)); }
@Test(expected = JsonParseException.class) public void shouldThrowExceptionWhileTryingToDeserializeUnknownTypeOfPortTelemetry() { String portTelemetryValueAsJsonArray = "{\"PortTelemetry\":\"ENABLED\"}"; adapter.deserialize(parser.parse(portTelemetryValueAsJsonArray), null, null); }