AbstractEntityReferenceDeserializer extends StdScalarDeserializer<T> { @Override @SuppressWarnings("unchecked") public final T deserialize(final JsonParser parser, final DeserializationContext context) throws IOException { String id = _parseString(parser, context); try { T instance = (T) handledType().newInstance(); instance.setId(IdUtil.fromString(id)); return instance; } catch (InstantiationException | IllegalAccessException ie) { throw context.mappingException("Cannot instantiate mapped " + "type"); } } protected  AbstractEntityReferenceDeserializer(final Class<?> vc); @Override @SuppressWarnings("unchecked") final T deserialize(final JsonParser parser,
                               final DeserializationContext context);  }
@Test public void testAbstractConstructor() throws Exception { BigInteger intId = IdUtil.next(); String id = String.format("\"%s\"", IdUtil.toString(intId)); StdScalarDeserializer deserializer = new TestDeserializer(); JsonFactory f = new JsonFactory(); JsonParser preloadedParser = f.createParser(id); preloadedParser.nextToken(); TestChildEntity e = (TestChildEntity) deserializer.deserialize(preloadedParser, mock(DeserializationContext.class)); assertEquals(intId, e.getId()); }
@Test public void testDeserializeSimple() throws Exception { BigInteger intId = IdUtil.next(); String id = String.format("\"%s\"", IdUtil.toString(intId)); JsonFactory f = new JsonFactory(); JsonParser preloadedParser = f.createParser(id); preloadedParser.nextToken(); TestDeserializer deserializer = new TestDeserializer(); TestChildEntity e = deserializer.deserialize(preloadedParser, mock(DeserializationContext.class)); assertEquals(intId, e.getId()); }
@Test(expected = JsonMappingException.class) public void testSerializePrivate() throws Exception { BigInteger intId = IdUtil.next(); String id = String.format("\"%s\"", IdUtil.toString(intId)); JsonFactory f = new JsonFactory(); JsonParser preloadedParser = f.createParser(id); preloadedParser.nextToken(); TestPrivateDeserializer deserializer = new TestPrivateDeserializer(); DeserializationContext context = mock(DeserializationContext.class); doCallRealMethod().when(context) .mappingException(Matchers.anyString()); deserializer.deserialize(preloadedParser, context); }
@Test(expected = JsonMappingException.class) public void testSerializeNoValue() throws Exception { JsonFactory f = new JsonFactory(); JsonParser preloadedParser = f.createParser("\"\""); preloadedParser.nextToken(); TestPrivateDeserializer deserializer = new TestPrivateDeserializer(); DeserializationContext context = mock(DeserializationContext.class); doCallRealMethod().when(context) .mappingException(Matchers.anyString()); deserializer.deserialize(preloadedParser, context); }
@Test(expected = JsonMappingException.class) public void testSerializeNullValue() throws Exception { JsonFactory f = new JsonFactory(); JsonParser preloadedParser = f.createParser("\"null\""); preloadedParser.nextToken(); TestPrivateDeserializer deserializer = new TestPrivateDeserializer(); DeserializationContext context = mock(DeserializationContext.class); doCallRealMethod().when(context) .mappingException(Matchers.anyString()); deserializer.deserialize(preloadedParser, context); }