JsonParameterMetadataDeserializer implements ParameterMetadataDeserializer { @Override public AppendableParameter deserialize(BufferedReader reader) throws ParamSerializationException { String jsonConfig = null; try { jsonConfig = StreamPartReader.readPart(reader, '{', '}'); } catch (IOException exception) { throw new ParamSerializationException("Unable to read config part from stream.", exception); } DeserializedParameter parameter = gson.fromJson(jsonConfig, DeserializedParameter.class); parameter.normalizeLevels(); return parameter; } JsonParameterMetadataDeserializer(); @Override AppendableParameter deserialize(BufferedReader reader);  }
@Test public void shouldDeserializeParameterMetadataSectionFromJSON() throws ParamSerializationException { String json = "{ \"name\": \"parameter\", \"cacheable\": \"true\"," + "\"nullable\": \"true\", \"inputLevels\": 1, \"levels\": [" + "{\"name\": \"level1\", \"levelCreator\": \"level1Creator\", \"type\": \"level1Type\", \"matcher\": \"level1Matcher\"}," + "{\"name\": \"level2\"}" + "]}"; Parameter parameter = deserializer.deserialize(StringStreamUtil.reader(json)); assertThat(parameter).hasName("parameter").isCacheable().isNullable().hasInputLevels(1).hasLevels(2) .level(0).hasName("level1").hasLevelCreator("level1Creator") .hasType("level1Type").hasMatcher("level1Matcher"); }
@Test public void shouldDeserializeNonStrictJSON() throws ParamSerializationException { String json = "{ name: \"parameter\" }"; Parameter parameter = deserializer.deserialize(StringStreamUtil.reader(json)); assertThat(parameter).isNotNull().hasName("parameter"); }
@Test public void shouldBeLiberalAboutDanglingCommas() throws ParamSerializationException { String json = "{ levels: [ {name: 'level'}, ] }"; Parameter parameter = deserializer.deserialize(StringStreamUtil.reader(json)); assertThat(parameter).isNotNull().hasLevels(1); }
@Test public void shouldDeserializeOnlyFirstJSONObject() throws Exception { String json = "{ name: \"parameter\" }\n" + "hello;hello"; BufferedReader reader = StringStreamUtil.reader(json); Parameter parameter = deserializer.deserialize(reader); assertThat(parameter).isNotNull().hasName("parameter"); assertThat(reader).hasTextLeft("\nhello;hello"); }