类com.fasterxml.jackson.databind.ser.SerializerFactory源码实例Demo

下面列出了怎么用com.fasterxml.jackson.databind.ser.SerializerFactory的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
	SerializerFactory factory = new DelegatingSerializerFactory(null);
	ObjectMapper mapper = new ObjectMapper();
	mapper.setSerializerFactory(factory);
	view.setObjectMapper(mapper);

	Object bean = new TestBeanSimple();
	Map<String, Object> model = new HashMap<>();
	model.put("foo", bean);
	model.put("bar", new TestChildBean());

	view.render(model, request, response);

	String result = response.getContentAsString();
	assertTrue(result.length() > 0);
	assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}"));

	validateResult();
}
 
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
	SerializerFactory factory = new DelegatingSerializerFactory(null);
	XmlMapper mapper = new XmlMapper();
	mapper.setSerializerFactory(factory);
	view.setObjectMapper(mapper);

	Object bean = new TestBeanSimple();
	Map<String, Object> model = new HashMap<>();
	model.put("foo", bean);

	view.render(model, request, response);

	String result = response.getContentAsString();
	assertTrue(result.length() > 0);
	assertTrue(result.contains("custom</testBeanSimple>"));

	validateResult();
}
 
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
	SerializerFactory factory = new DelegatingSerializerFactory(null);
	ObjectMapper mapper = new ObjectMapper();
	mapper.setSerializerFactory(factory);
	view.setObjectMapper(mapper);

	Object bean = new TestBeanSimple();
	Map<String, Object> model = new HashMap<>();
	model.put("foo", bean);
	model.put("bar", new TestChildBean());

	view.render(model, request, response);

	String result = response.getContentAsString();
	assertTrue(result.length() > 0);
	assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}"));

	validateResult();
}
 
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
	SerializerFactory factory = new DelegatingSerializerFactory(null);
	XmlMapper mapper = new XmlMapper();
	mapper.setSerializerFactory(factory);
	view.setObjectMapper(mapper);

	Object bean = new TestBeanSimple();
	Map<String, Object> model = new HashMap<>();
	model.put("foo", bean);

	view.render(model, request, response);

	String result = response.getContentAsString();
	assertTrue(result.length() > 0);
	assertTrue(result.contains("custom</testBeanSimple>"));

	validateResult();
}
 
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
	SerializerFactory factory = new DelegatingSerializerFactory(null);
	ObjectMapper mapper = new ObjectMapper();
	mapper.setSerializerFactory(factory);
	view.setObjectMapper(mapper);

	Object bean = new TestBeanSimple();
	Map<String, Object> model = new HashMap<String, Object>();
	model.put("foo", bean);
	model.put("bar", new TestChildBean());

	view.render(model, request, response);

	String result = response.getContentAsString();
	assertTrue(result.length() > 0);
	assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}"));

	validateResult();
}
 
@Test
public void renderWithCustomSerializerLocatedByFactory() throws Exception {
	SerializerFactory factory = new DelegatingSerializerFactory(null);
	XmlMapper mapper = new XmlMapper();
	mapper.setSerializerFactory(factory);
	view.setObjectMapper(mapper);

	Object bean = new TestBeanSimple();
	Map<String, Object> model = new HashMap<String, Object>();
	model.put("foo", bean);

	view.render(model, request, response);

	String result = response.getContentAsString();
	assertTrue(result.length() > 0);
	assertTrue(result.contains("custom</testBeanSimple>"));

	validateResult();
}
 
源代码7 项目: caravan   文件: CustomBeanSerializerFactory.java
@Override
public SerializerFactory withConfig(SerializerFactoryConfig config)
{
  if (_factoryConfig == config) {
    return this;
  }
  if (getClass() != CustomBeanSerializerFactory.class) {
    throw new IllegalStateException("Subtype of BeanSerializerFactory ("+getClass().getName()
        +") has not properly overridden method 'withAdditionalSerializers': can not instantiate subtype with "
        +"additional serializer definitions");
  }
  return new CustomBeanSerializerFactory(config);
}
 
源代码8 项目: tutorials   文件: JacksonJsonViewUnitTest.java
@Test
public void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
    final User user = new User(1, "John");
    final SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(new MyBeanSerializerModifier());

    final ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializerFactory(serializerFactory);

    final String result = mapper.writerWithView(Views.Public.class)
        .writeValueAsString(user);
    assertThat(result, containsString("JOHN"));
    assertThat(result, containsString("1"));
}
 
public MyNullSerializerContexts(TokenStreamFactory tsf, SerializerFactory serializerFactory,
        SerializerCache cache) {
    super(tsf, serializerFactory, cache);
}
 
@Override
public SerializationContexts forMapper(Object mapper,
        TokenStreamFactory tsf, SerializerFactory serializerFactory,
        SerializerCache cache) {
    return new MyNullSerializerContexts(tsf, serializerFactory, cache);
}
 
public MyNullSerializerProvider(TokenStreamFactory streamFactory,
        SerializerCache cache, SerializationConfig config,
        GeneratorSettings genSettings, SerializerFactory f) {
    super(streamFactory, config, genSettings, f, cache);
}
 
源代码12 项目: ignite   文件: GridJettyObjectMapper.java
/** {@inheritDoc} */
@Override public DefaultSerializerProvider createInstance(SerializationConfig cfg, SerializerFactory jsf) {
    return new CustomSerializerProvider(this, cfg, jsf);
}
 
@Override
public DefaultSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf) {
    return new ConfigurableSerializerProvider(config, this, jsf);
}
 
public ConfigurableSerializerProvider(SerializationConfig config, ConfigurableSerializerProvider src, SerializerFactory jsf) {
    super(src, config, jsf);
    unknownTypeSerializer = src.unknownTypeSerializer;
}
 
源代码15 项目: maven-archetype   文件: StdSerializerProvider.java
protected StdSerializerProvider(SerializerProvider src,
        SerializationConfig config,SerializerFactory f) {
    super(src, config, f);
}
 
源代码16 项目: maven-archetype   文件: StdSerializerProvider.java
@Override
public StdSerializerProvider createInstance(SerializationConfig config,
        SerializerFactory jsf) {
    return new StdSerializerProvider(this, config, jsf);
}
 
源代码17 项目: ignite   文件: GridJettyObjectMapper.java
/**
 * Full constructor.
 *
 * @param src Blueprint object used as the baseline for this instance.
 * @param cfg Provider configuration.
 * @param f Serializers factory.
 */
CustomSerializerProvider(SerializerProvider src, SerializationConfig cfg, SerializerFactory f) {
    super(src, cfg, f);
}
 
 同包方法