Java Code Examples for com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor

The following examples show how to use com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor. These examples are extracted from open source projects. 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 Project: lams   Source File: JsonValueSerializer.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Overridable helper method used for special case handling of schema information for
 * Enums.
 * 
 * @return True if method handled callbacks; false if not; in latter case caller will
 *   send default callbacks
 *
 * @since 2.6
 */
protected boolean _acceptJsonFormatVisitorForEnum(JsonFormatVisitorWrapper visitor,
        JavaType typeHint, Class<?> enumType)
    throws JsonMappingException
{
    // Copied from EnumSerializer#acceptJsonFormatVisitor
    JsonStringFormatVisitor stringVisitor = visitor.expectStringFormat(typeHint);
    if (stringVisitor != null) {
        Set<String> enums = new LinkedHashSet<String>();
        for (Object en : enumType.getEnumConstants()) {
            try {
                // 21-Apr-2016, tatu: This is convoluted to the max, but essentially we
                //   call `@JsonValue`-annotated accessor method on all Enum members,
                //   so it all "works out". To some degree.
                enums.add(String.valueOf(_accessor.getValue(en)));
            } catch (Exception e) {
                Throwable t = e;
                while (t instanceof InvocationTargetException && t.getCause() != null) {
                    t = t.getCause();
                }
                ClassUtil.throwIfError(t);
                throw JsonMappingException.wrapWithPath(t, en, _accessor.getName() + "()");
            }
        }
        stringVisitor.enumTypes(enums);
    }
    return true;
}
 
Example 2
Source Project: lams   Source File: EnumSerializer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
    throws JsonMappingException
{
    SerializerProvider serializers = visitor.getProvider();
    if (_serializeAsIndex(serializers)) {
        visitIntFormat(visitor, typeHint, JsonParser.NumberType.INT);
        return;
    }
    JsonStringFormatVisitor stringVisitor = visitor.expectStringFormat(typeHint);
    if (stringVisitor != null) {
        Set<String> enums = new LinkedHashSet<String>();
        
        // Use toString()?
        if ((serializers != null) && 
                serializers.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) {
            for (Enum<?> e : _values.enums()) {
                enums.add(e.toString());
            }
        } else {
            // No, serialize using name() or explicit overrides
            for (SerializableString value : _values.values()) {
                enums.add(value.getValue());
            }
        }
        stringVisitor.enumTypes(enums);
    }
}
 
Example 3
Source Project: jackson-modules-java8   Source File: YearMonthSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void _acceptTimestampVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
{
    SerializerProvider provider = visitor.getProvider();
    boolean useTimestamp = (provider != null) && useTimestamp(provider);
    if (useTimestamp) {
        super._acceptTimestampVisitor(visitor, typeHint);
    } else {
        JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
        if (v2 != null) {
            v2.format(JsonValueFormat.DATE_TIME);
        }
    }
}
 
Example 4
Source Project: jackson-modules-java8   Source File: LocalTimeSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
    throws JsonMappingException
{
    if (useTimestamp(visitor.getProvider())) {
        _acceptTimestampVisitor(visitor, typeHint);
    } else {
        JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
        if (v2 != null) {
            v2.format(JsonValueFormat.TIME);
        }
    }
}
 
Example 5
Source Project: jackson-modules-java8   Source File: LocalDateSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
{
    SerializerProvider provider = visitor.getProvider();
    boolean useTimestamp = (provider != null) && useTimestamp(provider);
    if (useTimestamp) {
        _acceptTimestampVisitor(visitor, typeHint);
    } else {
        JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
        if (v2 != null) {
            v2.format(JsonValueFormat.DATE);
        }
    }
}
 
Example 6
@Override
public JsonStringFormatVisitor expectStringFormat(JavaType type) throws JsonMappingException {
    addFieldIfPresent("String");
    return new JsonStringFormatVisitor.Base();
}