Java Code Examples for com.fasterxml.jackson.annotation.JsonFormat#Shape

The following examples show how to use com.fasterxml.jackson.annotation.JsonFormat#Shape . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnumSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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")));
}
 
Example 2
Source File: JSR310FormattedSerializerBase.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase<?> base,
        DateTimeFormatter dtf,
        Boolean useTimestamp, Boolean useNanoseconds, 
        JsonFormat.Shape shape)
{
    super(base.handledType());
    _useTimestamp = useTimestamp;
    _useNanoseconds = useNanoseconds;
    _formatter = dtf;
    _shape = shape;
}
 
Example 3
Source File: Jackson2ConfigurationResolved.java    From typescript-generator with MIT License 5 votes vote down vote up
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;
}
 
Example 4
Source File: BooleanSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 5
Source File: BooleanSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 6
Source File: ZonedDateTimeSerializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
protected JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter formatter, 
        Boolean useTimestamp,
        JsonFormat.Shape shape)
{
    return new ZonedDateTimeSerializer(this, formatter,
            useTimestamp, _useNanoseconds, _writeZoneId);
}
 
Example 7
Source File: YearDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected YearDeserializer withShape(JsonFormat.Shape shape) { return this; }
 
Example 8
Source File: JSR310FormattedSerializerBase.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 9
Source File: LocalDateSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
protected LocalDateSerializer(LocalDateSerializer base, DateTimeFormatter dtf,
        Boolean useTimestamp, JsonFormat.Shape shape) {
    super(base, dtf, useTimestamp, null, shape);
}
 
Example 10
Source File: LocalTimeSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected JSR310FormattedSerializerBase<LocalTime> withFormat(DateTimeFormatter dtf, 
        Boolean useTimestamp, JsonFormat.Shape shape) {
    return new LocalTimeSerializer(this, dtf, useTimestamp, _useNanoseconds);
}
 
Example 11
Source File: MonthDaySerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected MonthDaySerializer withFormat(DateTimeFormatter dtf,
        Boolean useTimestamp, JsonFormat.Shape shape) {
    return new MonthDaySerializer(this, dtf, useTimestamp);
}
 
Example 12
Source File: YearMonthDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected YearMonthDeserializer withShape(JsonFormat.Shape shape) { return this; }
 
Example 13
Source File: LocalDateTimeSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected JSR310FormattedSerializerBase<LocalDateTime> withFormat(DateTimeFormatter f,
        Boolean useTimestamp, JsonFormat.Shape shape) {
    return new LocalDateTimeSerializer(this, f, useTimestamp, _useNanoseconds);
}
 
Example 14
Source File: OffsetTimeSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected OffsetTimeSerializer withFormat(DateTimeFormatter dtf,
        Boolean useTimestamp, JsonFormat.Shape shape) {
    return new OffsetTimeSerializer(this, dtf, useTimestamp);
}
 
Example 15
Source File: InstantSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected JSR310FormattedSerializerBase<Instant> withFormat(DateTimeFormatter formatter,
        Boolean useTimestamp,
        JsonFormat.Shape shape) {
    return new InstantSerializer(this, useTimestamp, formatter);
}
 
Example 16
Source File: LocalDateSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected LocalDateSerializer withFormat(DateTimeFormatter dtf,
        Boolean useTimestamp, JsonFormat.Shape shape) {
    return new LocalDateSerializer(this, dtf, useTimestamp, shape);
}
 
Example 17
Source File: OffsetTimeDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected OffsetTimeDeserializer withShape(JsonFormat.Shape shape) { return this; }
 
Example 18
Source File: LocalDateDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected LocalDateDeserializer withShape(JsonFormat.Shape shape) { return new LocalDateDeserializer(this, shape); }
 
Example 19
Source File: MonthDayDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected MonthDayDeserializer withShape(JsonFormat.Shape shape) { return this; }
 
Example 20
Source File: JsonDeserializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <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);