Search in sources :

Example 1 with JsonStringFormatVisitor

use of com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor in project jackson-databind by FasterXML.

the class EnumSerializer method acceptJsonFormatVisitor.

@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);
    }
}
Also used : JsonStringFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor)

Example 2 with JsonStringFormatVisitor

use of com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor in project jackson-databind by FasterXML.

the class JsonValueSerializer method _acceptJsonFormatVisitorForEnum.

/**
     * 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JsonStringFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

JsonStringFormatVisitor (com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedHashSet (java.util.LinkedHashSet)1