下面列出了怎么用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();
}
@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);
}
@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);
}
/** {@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;
}
protected StdSerializerProvider(SerializerProvider src,
SerializationConfig config,SerializerFactory f) {
super(src, config, f);
}
@Override
public StdSerializerProvider createInstance(SerializationConfig config,
SerializerFactory jsf) {
return new StdSerializerProvider(this, config, jsf);
}
/**
* 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);
}