JsonDeserializer implements Deserializer { public Object[] deserialize(InputStream inputStream, ResourceMethod method) { Method javaMethod = method.getMethod(); Class<?>[] types = javaMethod.getParameterTypes(); checkArgument(types.length > 0, "Methods that consumes representations must receive just one argument: the root element"); XStream xStream = getConfiguredXStream(javaMethod, types); Object[] params = new Object[types.length]; chooseParam(types, params, xStream.fromXML(inputStream)); return params; } JsonDeserializer(ParameterNameProvider provider,TypeNameExtractor extractor, XStreamBuilder builder); Object[] deserialize(InputStream inputStream, ResourceMethod method); XStream getConfiguredXStream(Method javaMethod, Class<?>[] types);  }
@Test(expected=IllegalArgumentException.class) public void shouldNotAcceptMethodsWithoutArguments() throws Exception { deserializer.deserialize(new ByteArrayInputStream(new byte[0]), meow); }
@Test public void shouldBeAbleToDeserializeACat() throws Exception { InputStream stream = new ByteArrayInputStream("{\"cat\":{\"name\": \"Samantha\", \"age\": 2}}".getBytes()); when(provider.parameterNamesFor(roll.getMethod())).thenReturn(new String[] {"cat"}); Object[] deserialized = deserializer.deserialize(stream, roll); assertThat(deserialized.length, is(1)); assertThat(deserialized[0], is(instanceOf(Cat.class))); Cat cat = (Cat) deserialized[0]; assertThat(cat.name, is("Samantha")); assertThat(cat.age, is(2)); }
@Test public void shouldBeAbleToDeserializeACatWhenMethodHasMoreThanOneArgument() throws Exception { InputStream stream = new ByteArrayInputStream("{\"cat\":{\"name\": \"Zulu\", \"age\": 1}}".getBytes()); when(provider.parameterNamesFor(jump.getMethod())).thenReturn(new String[] {"cat", "times"}); Object[] deserialized = deserializer.deserialize(stream, jump); assertThat(deserialized.length, is(2)); assertThat(deserialized[0], is(instanceOf(Cat.class))); Cat cat = (Cat) deserialized[0]; assertThat(cat.name, is("Zulu")); assertThat(cat.age, is(1)); }
@Test public void shouldBeAbleToDeserializeACatWhenMethodHasMoreThanOneArgumentAndTheXmlIsTheLastOne() throws Exception { InputStream stream = new ByteArrayInputStream("{\"cat\":{\"name\": \"Tigre\", \"age\": 3}}".getBytes()); when(provider.parameterNamesFor(sleep.getMethod())).thenReturn(new String[] {"hours", "cat"}); Object[] deserialized = deserializer.deserialize(stream, sleep); assertThat(deserialized.length, is(2)); assertThat(deserialized[1], is(instanceOf(Cat.class))); Cat cat = (Cat) deserialized[1]; assertThat(cat.name, is("Tigre")); assertThat(cat.age, is(3)); }
@Test public void shouldBeAbleToDeserializeACatNamedDifferently() throws Exception { InputStream stream = new ByteArrayInputStream("{\"wrong\":{\"name\": \"Samantha\", \"age\": 2}}".getBytes()); when(provider.parameterNamesFor(roll.getMethod())).thenReturn(new String[] {"wrong"}); Object[] deserialized = deserializer.deserialize(stream, roll); assertThat(deserialized.length, is(1)); assertThat(deserialized[0], is(instanceOf(Cat.class))); Cat cat = (Cat) deserialized[0]; assertThat(cat.name, is("Samantha")); assertThat(cat.age, is(2)); }
@Test public void shouldBeAbleToDeserializeACatWhenAliasConfiguredByAnnotations() { InputStream stream = new ByteArrayInputStream("{\"catAnnotated\":{\"nameAnnotated\": \"Zulu\", \"ageAnnotated\": 1}}".getBytes()); when(provider.parameterNamesFor(annotated.getMethod())).thenReturn(new String[] {"cat"}); Object[] deserialized = deserializer.deserialize(stream, annotated); assertThat(deserialized.length, is(1)); assertThat(deserialized[0], is(instanceOf(CatWithAnnotations.class))); CatWithAnnotations cat = (CatWithAnnotations) deserialized[0]; assertThat(cat.name, is("Zulu")); assertThat(cat.age, is(1)); }