下面列出了com.fasterxml.jackson.annotation.JsonFormat#Shape ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Helper method called to check whether serialization should be done using
* index (number) or not.
*/
protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass,
JsonFormat.Value format, boolean fromClass,
Boolean defaultValue)
{
JsonFormat.Shape shape = (format == null) ? null : format.getShape();
if (shape == null) {
return defaultValue;
}
// i.e. "default", check dynamically
if (shape == Shape.ANY || shape == Shape.SCALAR) {
return defaultValue;
}
// 19-May-2016, tatu: also consider "natural" shape
if (shape == Shape.STRING || shape == Shape.NATURAL) {
return Boolean.FALSE;
}
// 01-Oct-2014, tatu: For convenience, consider "as-array" to also mean 'yes, use index')
if (shape.isNumeric() || (shape == Shape.ARRAY)) {
return Boolean.TRUE;
}
// 07-Mar-2017, tatu: Also means `OBJECT` not available as property annotation...
throw new IllegalArgumentException(String.format(
"Unsupported serialization shape (%s) for Enum %s, not supported as %s annotation",
shape, enumClass.getName(), (fromClass? "class" : "property")));
}
private static Map<Class<?>, JsonFormat.Shape> resolveShapeConfigOverrides(List<String> overrides, ClassLoader classLoader) {
if (overrides == null) {
return null;
}
final Map<Class<?>, JsonFormat.Shape> resolvedOverrides = new LinkedHashMap<>();
final Map<String, String> overridesMap = Settings.convertToMap(overrides);
for (Map.Entry<String, String> entry : overridesMap.entrySet()) {
final Class<?> cls = Settings.loadClass(classLoader, entry.getKey(), Object.class);
final JsonFormat.Shape shape = JsonFormat.Shape.valueOf(entry.getValue());
resolvedOverrides.put(cls, shape);
}
return resolvedOverrides;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(serializers,
property, Boolean.class);
if (format != null) {
JsonFormat.Shape shape = format.getShape();
if (shape.isNumeric()) {
return new AsNumber(_forPrimitive);
}
}
return this;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(serializers,
property, Boolean.class);
if (format != null) {
JsonFormat.Shape shape = format.getShape();
if (!shape.isNumeric()) {
return new BooleanSerializer(_forPrimitive);
}
}
return this;
}
@Override
protected JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter formatter,
Boolean useTimestamp,
JsonFormat.Shape shape)
{
return new ZonedDateTimeSerializer(this, formatter,
useTimestamp, _useNanoseconds, _writeZoneId);
}
protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase<?> base,
DateTimeFormatter dtf,
Boolean useTimestamp, Boolean useNanoseconds,
JsonFormat.Shape shape)
{
super(base.handledType());
_useTimestamp = useTimestamp;
_useNanoseconds = useNanoseconds;
_formatter = dtf;
_shape = shape;
}
@Override
protected LocalDateSerializer withFormat(DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape) {
return new LocalDateSerializer(this, dtf, useTimestamp, shape);
}
@Override
protected MonthDayDeserializer withShape(JsonFormat.Shape shape) { return this; }
@Override
protected LocalDateDeserializer withShape(JsonFormat.Shape shape) { return new LocalDateDeserializer(this, shape); }
@Override
protected OffsetTimeDeserializer withShape(JsonFormat.Shape shape) { return this; }
@Override
protected YearDeserializer withShape(JsonFormat.Shape shape) { return this; }
@Override
protected JSR310FormattedSerializerBase<Instant> withFormat(DateTimeFormatter formatter,
Boolean useTimestamp,
JsonFormat.Shape shape) {
return new InstantSerializer(this, useTimestamp, formatter);
}
@Override
protected OffsetTimeSerializer withFormat(DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape) {
return new OffsetTimeSerializer(this, dtf, useTimestamp);
}
@Override
protected JSR310FormattedSerializerBase<LocalDateTime> withFormat(DateTimeFormatter f,
Boolean useTimestamp, JsonFormat.Shape shape) {
return new LocalDateTimeSerializer(this, f, useTimestamp, _useNanoseconds);
}
@Override
protected YearMonthDeserializer withShape(JsonFormat.Shape shape) { return this; }
@Override
protected MonthDaySerializer withFormat(DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape) {
return new MonthDaySerializer(this, dtf, useTimestamp);
}
@Override
protected JSR310FormattedSerializerBase<LocalTime> withFormat(DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape) {
return new LocalTimeSerializer(this, dtf, useTimestamp, _useNanoseconds);
}
protected LocalDateSerializer(LocalDateSerializer base, DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape) {
super(base, dtf, useTimestamp, null, shape);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(prov, property, handledType());
if (format != null) {
Boolean useTimestamp = null;
// Simple case first: serialize as numeric timestamp?
JsonFormat.Shape shape = format.getShape();
if (shape == JsonFormat.Shape.ARRAY || shape.isNumeric() ) {
useTimestamp = Boolean.TRUE;
} else {
useTimestamp = (shape == JsonFormat.Shape.STRING) ? Boolean.FALSE : null;
}
DateTimeFormatter dtf = _formatter;
// If not, do we have a pattern?
if (format.hasPattern()) {
final String pattern = format.getPattern();
final Locale locale = format.hasLocale() ? format.getLocale() : prov.getLocale();
if (locale == null) {
dtf = DateTimeFormatter.ofPattern(pattern);
} else {
dtf = DateTimeFormatter.ofPattern(pattern, locale);
}
//Issue #69: For instant serializers/deserializers we need to configure the formatter with
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
if (format.hasTimeZone()) {
dtf = dtf.withZone(format.getTimeZone().toZoneId());
}
}
JSR310FormattedSerializerBase<?> ser = this;
if ((shape != _shape) || (useTimestamp != _useTimestamp) || (dtf != _formatter)) {
ser = ser.withFormat(dtf, useTimestamp, shape);
}
Boolean writeZoneId = format.getFeature(JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID);
Boolean writeNanoseconds = format.getFeature(JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
if ((writeZoneId != null) || (writeNanoseconds != null)) {
ser = ser.withFeatures(writeZoneId, writeNanoseconds);
}
return ser;
}
return this;
}
/**
* <p>setShape.</p>
*
* @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
* @return a {@link org.dominokit.jacksonapt.JsonDeserializerParameters} object.
*/
JsonDeserializerParameters setShape(JsonFormat.Shape shape);