com.fasterxml.jackson.annotation.JsonFormat.Shape Java Examples

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: AbstractBeanJsonCreator.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
/**
 * Add the common property parameters to the code builder.
 *
 * @param paramBuilder the code builder
 * @param property the information about the property
 */
protected final void buildCommonPropertyParameters( CodeBlock.Builder paramBuilder, PropertyInfo property ) {
    if ( property.getFormat().isPresent() ) {
        JsonFormat format = property.getFormat().get();

        if ( !Strings.isNullOrEmpty( format.pattern() ) ) {
            paramBuilder.add( "\n.setPattern($S)", format.pattern() );
        }

        paramBuilder.add( "\n.setShape($T.$L)", Shape.class, format.shape().name() );

        if ( !Strings.isNullOrEmpty( format.locale() ) && !JsonFormat.DEFAULT_LOCALE.equals( format.locale() ) ) {
            logger.log( Type.WARN, "JsonFormat.locale is not supported by default" );
            paramBuilder.add( "\n.setLocale($S)", format.locale() );
        }
    }

    if ( property.getIgnoredProperties().isPresent() ) {
        for ( String ignoredProperty : property.getIgnoredProperties().get() ) {
            paramBuilder.add( "\n.addIgnoredProperty($S)", ignoredProperty );
        }
    }
}
 
Example #2
Source File: JSR310FormattedSerializerBase.java    From jackson-modules-java8 with Apache License 2.0 6 votes vote down vote up
protected boolean useTimestamp(SerializerProvider provider) {
    if (_useTimestamp != null) {
        return _useTimestamp.booleanValue();
    }
    if (_shape != null) {
        if (_shape == Shape.STRING) {
            return false;
        }
        if (_shape == Shape.NUMBER_INT) {
            return true;
        }
    }
    // assume that explicit formatter definition implies use of textual format
    return (_formatter == null) && (provider != null)
            && provider.isEnabled(getTimestampsFeature());
}
 
Example #3
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 #4
Source File: BaseDateJsonSerializer.java    From domino-jackson with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSerialize(JsonWriter writer, Date value, JsonSerializationContext ctx, JsonSerializerParameters params) {
    if ((ctx.isWriteDatesAsTimestamps() || params.getShape().isNumeric()) && params.getShape() != Shape.STRING) {
        writer.value(value.getTime());
    } else {
        String date = JacksonContextProvider.get().dateFormat().format(params, value);
        if (null == params.getPattern()) {
            writer.unescapeValue(date);
        } else {
            writer.value(date);
        }
    }
}
 
Example #5
Source File: BaseDateJsonSerializer.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSerialize( JsonWriter writer, Date value, JsonSerializationContext ctx, JsonSerializerParameters params ) {
    if ( (ctx.isWriteDatesAsTimestamps() || params.getShape().isNumeric()) && params.getShape() != Shape.STRING ) {
        writer.value( value.getTime() );
    } else {
        String date = DateFormat.format( params, value );
        if ( null == params.getPattern() ) {
            writer.unescapeValue( date );
        } else {
            writer.value( date );
        }
    }
}
 
Example #6
Source File: JSR310FormattedSerializerBase.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
protected boolean useNanoseconds(SerializerProvider provider) {
    if (_useNanoseconds != null) {
        return _useNanoseconds.booleanValue();
    }
    if (_shape != null) {
        if (_shape == Shape.NUMBER_INT) {
            return false;
        }
        if (_shape == Shape.NUMBER_FLOAT) {
            return true;
        }
    }
    return (provider != null)
            && provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
}
 
Example #7
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 #8
Source File: JsonFormatTester.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
public FormatDateBean( @JsonProperty( "dateInParameter" ) @JsonFormat( shape = Shape.STRING,
        pattern = "'P'yyyy/MM/dd" ) Date dateParameter ) {
    this.dateInParameter = dateParameter;
}
 
Example #9
Source File: POJOAsArrayTest.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public JsonFormat.Value findFormat(MapperConfig<?> config, Annotated a) {
    return new JsonFormat.Value().withShape(JsonFormat.Shape.ARRAY);
}
 
Example #10
Source File: JSR310DateTimeDeserializerBase.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
        BeanProperty property) throws JsonMappingException
{
    JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
    JSR310DateTimeDeserializerBase<?> deser = this;
    if (format != null) {
        // 17-Aug-2019, tatu: For 2.10 let's start considering leniency/strictness too
        if (format.hasLenient()) {
            Boolean leniency = format.getLenient();
            if (leniency != null) {
                deser = deser.withLeniency(leniency);
            }
        }
        if (format.hasPattern()) {
            final String pattern = format.getPattern();
            final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
            DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
            if (acceptCaseInsensitiveValues(ctxt, format)) {
                builder.parseCaseInsensitive();
            }
            builder.appendPattern(pattern);
            DateTimeFormatter df;
            if (locale == null) {
                df = builder.toFormatter();
            } else {
                df = builder.toFormatter(locale);
            }

            // [#148]: allow strict parsing
            if (!deser.isLenient()) {
                df = df.withResolverStyle(ResolverStyle.STRICT);
            }

            // [#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()) {
                df = df.withZone(format.getTimeZone().toZoneId());
            }
            deser = deser.withDateFormat(df);
        }
        // [#58]: For LocalDate deserializers we need to configure the formatter with
        //a shape picked up from JsonFormat annotation, to decide if the value is EpochSeconds
        JsonFormat.Shape shape = format.getShape();
        if (shape != null && shape != _shape) {
            deser = deser.withShape(shape);
        }
        // any use for TimeZone?
    }
    return deser;
}
 
Example #11
Source File: JSR310DateTimeDeserializerBase.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
protected JSR310DateTimeDeserializerBase(JSR310DateTimeDeserializerBase<T> base,
        Shape shape) {
    super(base);
    _formatter = base._formatter;
    _shape = shape;
}
 
Example #12
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 #13
Source File: JSR310FormattedSerializerBase.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
protected abstract JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter dtf,
Boolean useTimestamp, JsonFormat.Shape shape);
 
Example #14
Source File: ServerJacksonJsonSerializerParameters.java    From domino-jackson with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Setter for the field <code>shape</code>.</p>
 */
@Override
public JsonSerializerParameters setShape(Shape shape) {
    this.shape = shape;
    return this;
}
 
Example #15
Source File: GwtJacksonJsonDeserializerParameters.java    From domino-jackson with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Setter for the field <code>shape</code>.</p>
 */
@Override
public JsonDeserializerParameters setShape(Shape shape) {
    this.shape = shape;
    return this;
}
 
Example #16
Source File: GwtJacksonJsonSerializerParameters.java    From domino-jackson with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Setter for the field <code>shape</code>.</p>
 */
@Override
public JsonSerializerParameters setShape(Shape shape) {
    this.shape = shape;
    return this;
}
 
Example #17
Source File: ServerJacksonJsonDeserializerParameters.java    From domino-jackson with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Setter for the field <code>shape</code>.</p>
 */
@Override
public JsonDeserializerParameters setShape(Shape shape) {
    this.shape = shape;
    return this;
}
 
Example #18
Source File: ServerJacksonJsonSerializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Getter for the field <code>shape</code>.</p>
 */
@Override
public Shape getShape() {
    return shape;
}
 
Example #19
Source File: GwtJacksonJsonDeserializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Getter for the field <code>shape</code>.</p>
 */
@Override
public Shape getShape() {
    return shape;
}
 
Example #20
Source File: JsonDeserializerParameters.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Getter for the field <code>shape</code>.</p>
 *
 * @return a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
 */
public Shape getShape() {
    return shape;
}
 
Example #21
Source File: JsonDeserializerParameters.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Setter for the field <code>shape</code>.</p>
 *
 * @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
 * @return a {@link com.github.nmorel.gwtjackson.client.JsonDeserializerParameters} object.
 */
public JsonDeserializerParameters setShape( Shape shape ) {
    this.shape = shape;
    return this;
}
 
Example #22
Source File: GwtJacksonJsonSerializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Getter for the field <code>shape</code>.</p>
 */
@Override
public Shape getShape() {
    return shape;
}
 
Example #23
Source File: JsonSerializerParameters.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Getter for the field <code>shape</code>.</p>
 *
 * @return a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
 */
public Shape getShape() {
    return shape;
}
 
Example #24
Source File: JsonSerializerParameters.java    From gwt-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * <p>Setter for the field <code>shape</code>.</p>
 *
 * @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object.
 * @return a {@link com.github.nmorel.gwtjackson.client.JsonSerializerParameters} object.
 */
public JsonSerializerParameters setShape( Shape shape ) {
    this.shape = shape;
    return this;
}
 
Example #25
Source File: ServerJacksonJsonDeserializerParameters.java    From domino-jackson with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Getter for the field <code>shape</code>.</p>
 */
@Override
public Shape getShape() {
    return shape;
}
 
Example #26
Source File: JSR310DateTimeDeserializerBase.java    From jackson-modules-java8 with Apache License 2.0 votes vote down vote up
protected abstract JSR310DateTimeDeserializerBase<T> withShape(Shape shape);