Java Code Examples for com.fasterxml.jackson.databind.SerializerProvider#isEnabled()

The following examples show how to use com.fasterxml.jackson.databind.SerializerProvider#isEnabled() . 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: CollectionSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final void serialize(Collection<?> value, JsonGenerator g, SerializerProvider provider) throws IOException
{
    final int len = value.size();
    if (len == 1) {
        if (((_unwrapSingle == null) &&
                provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
                || (_unwrapSingle == Boolean.TRUE)) {
            serializeContents(value, g, provider);
            return;
        }
    }
    g.writeStartArray(len);
    serializeContents(value, g, provider);
    g.writeEndArray();
}
 
Example 2
Source File: CharIterableSerializer.java    From jackson-datatypes-collections with Apache License 2.0 6 votes vote down vote up
@Override
public void serializeWithType(
        CharIterable value,
        JsonGenerator g,
        SerializerProvider ctxt,
        TypeSerializer typeSer
) throws IOException {
    g.setCurrentValue(value);
    WritableTypeId typeIdDef;
    if (ctxt.isEnabled(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)) {
        typeIdDef = typeSer.writeTypePrefix(g, ctxt, typeSer.typeId(value, JsonToken.START_ARRAY));
        writeContentsAsArray(value, g);
    } else {
        typeIdDef = typeSer.writeTypePrefix(g, ctxt, typeSer.typeId(value, JsonToken.VALUE_STRING));
        char[] chars = value.toArray();
        g.writeString(chars, 0, chars.length);
    }
    typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
}
 
Example 3
Source File: ZonedDateTimeKeySerializer.java    From jackson-modules-java8 with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException,
    JsonProcessingException {
    /* [modules-java8#127]: Serialization of timezone data is disabled by default, but can be
     * turned on by enabling `SerializationFeature.WRITE_DATES_WITH_ZONE_ID`
     */
    if (serializers.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)) {
        gen.writeFieldName(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value));
    } else if (useTimestamps(serializers)) {
        if (useNanos(serializers)) {
            gen.writeFieldName(DecimalUtils.toBigDecimal(value.toEpochSecond(), value.getNano()).toString());
        } else {
            gen.writeFieldName(String.valueOf(value.toInstant().toEpochMilli()));
        }
    } else {
        gen.writeFieldName(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value));
    }
}
 
Example 4
Source File: PrimitiveIterableSerializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public final void serialize(C value, JsonGenerator gen, SerializerProvider ctxt) throws IOException {
    if (((_unwrapSingle == null) &&
            ctxt.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
            || (Boolean.TRUE.equals(_unwrapSingle))) {
        if (hasSingleElement(value)) {
            serializeContents(value, gen);
            return;
        }
    }
    gen.writeStartArray();
    serializeContents(value, gen);
    gen.writeEndArray();
}
 
Example 5
Source File: CharIterableSerializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(CharIterable value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (provider.isEnabled(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)) {
        gen.writeStartArray();
        writeContentsAsArray(value, gen);
        gen.writeEndArray();
    } else {
        char[] chars = value.toArray();
        gen.writeString(chars, 0, chars.length);
    }
}
 
Example 6
Source File: DateTimeSerializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
        jgen.writeNumber(value.getMillis());
    } else {
        value = value.withZone(DateTimeZone.UTC);
        jgen.writeString(value.toString(ISODateTimeFormat.dateTime()));
    }
}
 
Example 7
Source File: DateTimeRfc1123Serializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
@Override
public void serialize(DateTimeRfc1123 value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
        jgen.writeNumber(value.dateTime().getMillis());
    } else {
        jgen.writeString(value.toString()); //Use the default toString as it is RFC1123.
    }
}
 
Example 8
Source File: ZonedDateTimeSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
public boolean shouldWriteWithZoneId(SerializerProvider ctxt) {
    return (_writeZoneId != null) ? _writeZoneId :
        ctxt.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
}
 
Example 9
Source File: ZonedDateTimeKeySerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
private static boolean useNanos(SerializerProvider serializers) {
    return serializers.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
}
 
Example 10
Source File: ZonedDateTimeKeySerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
private static boolean useTimestamps(SerializerProvider serializers) {
    return serializers.isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS);
}
 
Example 11
Source File: ProtobufSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
private static boolean writeEnumsUsingIndex(SerializerProvider config) {
  return config.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX);
}
 
Example 12
Source File: MessageSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
private static boolean writeEmptyArrays(SerializerProvider config) {
  return config.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
}
 
Example 13
Source File: MessageSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
private static boolean writeSingleElementArraysUnwrapped(SerializerProvider config) {
  return config.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
}