下面列出了com.fasterxml.jackson.databind.ser.SerializerFactory#com.fasterxml.jackson.databind.ser.DefaultSerializerProvider 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public SequenceWriter(DefaultSerializerProvider prov, JsonGenerator gen,
boolean closeGenerator, ObjectWriter.Prefetch prefetch)
throws IOException
{
_provider = prov;
_generator = gen;
_closeGenerator = closeGenerator;
_rootSerializer = prefetch.getValueSerializer();
_typeSerializer = prefetch.getTypeSerializer();
_config = prov.getConfig();
_cfgFlush = _config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
_cfgCloseCloseable = _config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE);
// important: need to cache "root value" serializers, to handle polymorphic
// types properly
_dynamicSerializers = PropertySerializerMap.emptyForRootValues();
}
private BeanHelper getBeanHelper(Class<?> beanClass) {
if (beanClass == null) {
return null;
}
if (beanClass == Enum.class) {
return null;
}
try {
final DefaultSerializerProvider.Impl serializerProvider1 = (DefaultSerializerProvider.Impl) objectMapper.getSerializerProvider();
final DefaultSerializerProvider.Impl serializerProvider2 = serializerProvider1.createInstance(objectMapper.getSerializationConfig(), objectMapper.getSerializerFactory());
final JavaType simpleType = objectMapper.constructType(beanClass);
final JsonSerializer<?> jsonSerializer = BeanSerializerFactory.instance.createSerializer(serializerProvider2, simpleType);
if (jsonSerializer == null) {
return null;
}
if (jsonSerializer instanceof BeanSerializer) {
return new BeanHelper((BeanSerializer) jsonSerializer);
} else {
return null;
}
} catch (JsonMappingException e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructor() throws Exception {
ObjectMapper mapper1 = new ObjectMapper();
ObjectMapper mapper2 = new ObjectMapper(new JsonFactory());
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
Constructor<?> omConstructor = ObjectMapper.class.getConstructor(JsonFactory.class, DefaultSerializerProvider.class, DefaultDeserializationContext.class);
Constructor<?> omConstructor1 = ObjectMapper.class.getConstructor();
Constructor<?> omConstructor2 = ObjectMapper.class.getConstructor(JsonFactory.class);
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor));
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor1));
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor));
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor2));
verifier.verifyTraceCount(0);
}
@Test
public void testConstructor() throws Exception {
ObjectMapper mapper1 = new ObjectMapper();
ObjectMapper mapper2 = new ObjectMapper(new JsonFactory());
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
Constructor<?> omConstructor = ObjectMapper.class.getConstructor(JsonFactory.class, DefaultSerializerProvider.class, DefaultDeserializationContext.class);
Constructor<?> omConstructor1 = ObjectMapper.class.getConstructor();
Constructor<?> omConstructor2 = ObjectMapper.class.getConstructor(JsonFactory.class);
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor));
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor1));
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor));
verifier.verifyTrace(event(SERVICE_TYPE, omConstructor2));
verifier.verifyTraceCount(0);
}
public static synchronized void reset() {
for (ObjectMapper mapper : U.list(MAPPER, PRETTY_MAPPER)) {
SerializerProvider serializerProvider = mapper.getSerializerProvider();
if (serializerProvider instanceof DefaultSerializerProvider) {
DefaultSerializerProvider provider = (DefaultSerializerProvider) serializerProvider;
provider.flushCachedSerializers();
} else {
Log.warn("Couldn't clear the cache of Jackson serializers!", "class", Cls.of(serializerProvider));
}
DeserializationContext deserializationContext = mapper.getDeserializationContext();
Object cache = Cls.getFieldValue(deserializationContext, "_cache");
if (cache instanceof DeserializerCache) {
DeserializerCache deserializerCache = (DeserializerCache) cache;
deserializerCache.flushCachedDeserializers();
} else {
Log.warn("Couldn't clear the cache of Jackson deserializers!", "class", Cls.of(cache));
}
}
}
@Test
public void serializeBigIntXWithNanoPlotType_resultJsonHasStringXs() throws IOException {
//when
ConstantBand constantBand = new ConstantBand();
constantBand.setX(
Arrays.asList(
new BigInteger("12345678901234567891000"), new BigInteger("12345678901234567892000")));
constantBand.setPlotType(NanoPlot.class);
constantBandSerializer.serialize(constantBand, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("x")).isTrue();
ArrayNode arrayNode = (ArrayNode) actualObj.get("x");
Assertions.assertThat(arrayNode.get(0).isTextual()).isTrue();
}
@Test
public void serializeWidthLine_resultJsonHasWidth() throws IOException {
//when
line.setWidth(1f);
lineSerializer.serialize(line, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("width")).isTrue();
Assertions.assertThat(actualObj.get("width").asDouble()).isEqualTo(1.0);
}
@Test
public void serializeRangeMaxOfHistogram_resultJsonHasRangeMax() throws IOException {
//when
histogram.setRangeMax(11);
histogramSerializer.serialize(histogram, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("range_max")).isTrue();
Assertions.assertThat(actualObj.get("range_max").asInt()).isEqualTo(11);
}
@Test
public void serializePositionOfLegendPosition_resultJsonHasPosition() throws IOException {
//when
legendPosition.setPosition(LegendPosition.Position.LEFT);
legendPositionSerializer.serialize(legendPosition, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("position")).isTrue();
Assertions.assertThat(actualObj.get("position").asText()).isEqualTo("LEFT");
}
@Test
public void serializeOutlineColorsCategoryPoints_resultJsonHasOutlineColors() throws IOException {
//when
CategoryPoints categoryPoints = new CategoryPoints();
categoryPoints.setOutlineColor(Arrays.asList(Color.BLUE, Color.GREEN, Color.BLACK));
categoryPointsSerializer.serialize(categoryPoints, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("outline_colors")).isTrue();
ArrayNode arrayNode = (ArrayNode) actualObj.get("outline_colors");
Assertions.assertThat(arrayNode.get(1).get("rgb").asInt()).isEqualTo(Color.GREEN.getRGB());
}
@Test
public void serializeOrientationCategoryPlot_resultJsonHasOrientation() throws IOException {
//when
CategoryPlot categoryPlot = new CategoryPlot();
categoryPlot.setOrientation(PlotOrientationType.VERTICAL);
categoryPlotSerializer.serialize(categoryPlot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("orientation")).isTrue();
Assertions.assertThat(actualObj.get("orientation").asText()).isEqualTo("VERTICAL");
}
@Test
public void serializeConstantBandsOfXYChartPlot_resultJsonHasConstantBands() throws IOException {
//given
ConstantBand constantBand = new ConstantBand();
plot.add(constantBand);
//when
xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("constant_bands")).isTrue();
Assertions.assertThat(actualObj.get("constant_bands")).isNotEmpty();
}
@Test
public void serializeGraphicsOfXYChartPlot_resultJsonHasGraphicsList() throws IOException {
//given
Line line = new Line();
line.setX(Arrays.asList(1, 2, 3));
line.setY(Arrays.asList(2, 3, 4));
plot.add(line);
//when
xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("graphics_list")).isTrue();
Assertions.assertThat(actualObj.get("graphics_list")).isNotEmpty();
}
@Test
public void serializeYAxis_resultJsonHasType() throws IOException {
//when
yAxisSerializer.serialize(yAxis, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("type")).isTrue();
Assertions.assertThat(actualObj.get("type").asText()).isEqualTo("YAxis");
}
@Test
public void serializeShowLabelConstantLine_resultJsonHasShowLabel() throws IOException {
//when
ConstantLine constantLine = new ConstantLine();
constantLine.setShowLabel(true);
constantLineSerializer.serialize(constantLine, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("showLabel")).isTrue();
Assertions.assertThat(actualObj.get("showLabel").asBoolean()).isTrue();
}
@Test
public void serializeLegendPosition_resultJsonHasType() throws IOException {
//when
legendPositionSerializer.serialize(legendPosition, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("type")).isTrue();
Assertions.assertThat(actualObj.get("type").asText()).isEqualTo("LegendPosition");
}
@Test
public void serializeXLowerBoundOfXYChartPlot_resultJsonHasXLowerBound() throws IOException {
//when
plot.setXBound(0.5, 1.5);
xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("x_lower_bound")).isTrue();
Assertions.assertThat(actualObj.get("x_lower_bound").asDouble()).isEqualTo(0.5);
}
@Test
public void serializeDisplayNameOfXYGraphicsLine_resultJsonHasDisplayName() throws IOException {
//when
line.setDisplayName("some display name");
xyGraphicsSerializer.serialize(line, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("display_name")).isTrue();
Assertions.assertThat(actualObj.get("display_name").asText()).isEqualTo("some display name");
}
@Test
public void serializeInitWidthOfCombinedPlot_resultJsonHasInitWidth() throws IOException {
//when
CombinedPlot combinedPlot = new CombinedPlot();
combinedPlot.setInitWidth(600);
combinedPlotSerializer.serialize(combinedPlot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("init_width")).isTrue();
Assertions.assertThat(actualObj.get("init_width").asInt()).isEqualTo(600);
}
@Test
public void serializeWidthStems_resultJsonHasWidth() throws IOException {
//when
stems.setWidth(11f);
stemsSerializer.serialize(stems, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("width")).isTrue();
Assertions.assertThat(actualObj.get("width").asInt()).isEqualTo(11);
}
@Test
public void serializeConstantBand_resultJsonHasType() throws IOException {
//when
ConstantBand constantBand = new ConstantBand() {};
constantBandSerializer.serialize(constantBand, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("type")).isTrue();
Assertions.assertThat(actualObj.get("type").asText()).isEqualTo("ConstantBand");
}
@Test
public void serializeRangeMinOfHistogram_resultJsonHasRangeMin() throws IOException {
//when
histogram.setRangeMin(11);
histogramSerializer.serialize(histogram, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("range_min")).isTrue();
Assertions.assertThat(actualObj.get("range_min").asInt()).isEqualTo(11);
}
@Test
public void serializeYConstantBand_resultJsonHasY() throws IOException {
//when
ConstantBand constantBand = new ConstantBand();
constantBand.setY(Arrays.asList(1, 2, 3));
constantBandSerializer.serialize(constantBand, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("y")).isTrue();
Assertions.assertThat(actualObj.get("y")).isNotEmpty();
}
@Test
public void serializeLodThresholdOfXYChartPlot_resultJsonHasLodThreshold() throws IOException {
//when
plot.setLodThreshold(11);
xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("lodThreshold")).isTrue();
Assertions.assertThat(actualObj.get("lodThreshold").asInt()).isEqualTo(11);
}
@Test
public void serializeYAxisConstantBand_resultJsonHasYAxis() throws IOException {
//when
ConstantBand constantBand = new ConstantBand();
constantBand.setyAxis("Y Axis name");
constantBandSerializer.serialize(constantBand, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("yAxis")).isTrue();
Assertions.assertThat(actualObj.get("yAxis").asText()).isEqualTo("Y Axis name");
}
@Test
public void serializeXLogBaseOfXYChartPlot_resultJsonHasXLogBase() throws IOException {
//when
plot.setxLogBase(1.5);
xyChartSerializer.serialize(plot, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("x_log_base")).isTrue();
Assertions.assertThat(actualObj.get("x_log_base").asDouble()).isEqualTo(1.5);
}
@Test
public void serializeRatioOfTreeMap_resultJsonHasRatio() throws IOException {
//when
treeMap.setRatio(2.0);
treeMapSerializer.serialize(treeMap, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("ratio")).isTrue();
Assertions.assertThat(actualObj.get("ratio").asDouble()).isEqualTo(2.0);
}
@Test
public void serializeColorOfText_resultJsonHasColor() throws IOException {
//when
text.setColor(Color.GREEN);
textSerializer.serialize(text, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("color")).isTrue();
Assertions.assertThat(actualObj.get("color").get("rgb").asInt())
.isEqualTo(Color.GREEN.getRGB());
}
@Test
public void serializeWidthCategoryLines_resultJsonHasWidth() throws IOException {
//when
CategoryLines categoryLines = new CategoryLines();
categoryLines.setWidth(11f);
categoryLinesSerializer.serialize(categoryLines, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("width")).isTrue();
Assertions.assertThat(actualObj.get("width").asInt()).isEqualTo(11);
}
@Test
public void serializeStrokeTypeStems_resultJsonHasStyle() throws IOException {
//when
CategoryLines categoryLines = new CategoryLines();
categoryLines.setStyle(StrokeType.SOLID);
categoryLinesSerializer.serialize(categoryLines, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
JsonNode actualObj = mapper.readTree(sw.toString());
Assertions.assertThat(actualObj.has("style")).isTrue();
Assertions.assertThat(actualObj.get("style").asText()).isEqualTo("SOLID");
}