Java org.codehaus.jackson.map.annotate.JsonSerialize 代码实例

・8 分钟阅读

以下是展示如何使用org.codehaus.jackson.map.annotate.JsonSerialize的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * Method for checking whether given annotated entity (class, method,
 * field) defines which Bean/Map properties are to be included in
 * serialization.
 * If no annotation is found, method should return given second
 * argument; otherwise value indicated by the annotation
 *
 * @return Enumerated value indicating which properties to include
 *   in serialization
 */
public abstract JsonSerialize.Inclusion findSerializationInclusion(Annotated a, JsonSerialize.Inclusion defValue);
 

实例 2


@Override
public JsonSerialize.Inclusion findSerializationInclusion(Annotated a,
                                                           JsonSerialize.Inclusion defValue)
{
    /* This is bit trickier: need to combine results in a meaningful
     * way. Seems like it should be a disjoint; that is, most
     * restrictive value should be returned.
     * For enumerations, comparison is done by indexes, which
     * works: largest value is the last one, which is the most
     * restrictive value as well.
     */
    /* 09-Mar-2010, tatu: Actually, as per [JACKSON-256], it is probably better to just
     *    use strict overriding. Simpler, easier to understand.
     */
    // note: call secondary first, to give lower priority
    defValue = _secondary.findSerializationInclusion(a, defValue);
    defValue = _primary.findSerializationInclusion(a, defValue);
    return defValue;
}
 

实例 3


/**
 * Method that checks class annotations that the argument Object has,
 * and modifies settings of this configuration object accordingly,
 * similar to how those annotations would affect actual value classes
 * annotated with them, but with global scope. Note that not all
 * annotations have global significance, and thus only subset of
 * Jackson annotations will have any effect.
 *<p>
 * Serialization annotations that are known to have effect are:
 *<ul>
 * <li>{@link JsonWriteNullProperties}</li>
 * <li>{@link JsonAutoDetect}</li>
 * <li>{@link JsonSerialize#typing}</li>
 *</ul>
 * 
 * @param cls Class of which class annotations to use
 *   for changing configuration settings
 */
//@Override
public void fromAnnotations(Class<?> cls)
{
    /* 10-Jul-2009, tatu: Should be able to just pass null as
     *    'MixInResolver'; no mix-ins set at this point
     * 29-Jul-2009, tatu: Also, we do NOT ignore annotations here, even
     *    if Feature.USE_ANNOTATIONS was disabled, since caller
     *    specifically requested annotations to be added with this call
     */
    AnnotatedClass ac = AnnotatedClass.construct(cls, _annotationIntrospector, null);
    _visibilityChecker = _annotationIntrospector.findAutoDetectVisibility(ac, _visibilityChecker);
    // How about writing null property values?
    JsonSerialize.Inclusion incl = _annotationIntrospector.findSerializationInclusion(ac, null);
    if (incl != _serializationInclusion) {
        setSerializationInclusion(incl);
        }
    JsonSerialize.Typing typing = _annotationIntrospector.findSerializationTyping(ac);
    if (typing != null) {
        set(Feature.USE_STATIC_TYPING, (typing == JsonSerialize.Typing.STATIC));
    }
}
 

实例 4


/**
 * Method for determining whether null properties should be written
 * out for a Bean of introspected type. This is based on global
 * feature (lowest priority, passed as argument)
 * and per-class annotation (highest priority).
 */
public JsonSerialize.Inclusion findSerializationInclusion(JsonSerialize.Inclusion defValue)
{
    return _annotationIntrospector.findSerializationInclusion(_classInfo, defValue);
}
 

实例 5


@Override
public JsonSerialize.Inclusion findSerializationInclusion(Annotated a,
                                                           JsonSerialize.Inclusion defValue)
{
    /* This is bit trickier: need to combine results in a meaningful
     * way. Seems like it should be a disjoint; that is, most
     * restrictive value should be returned.
     * For enumerations, comparison is done by indexes, which
     * works: largest value is the last one, which is the most
     * restrictive value as well.
     */
    /* 09-Mar-2010, tatu: Actually, as per [JACKSON-256], it is probably better to just
     *    use strict overriding. Simpler, easier to understand.
     */
    // note: call secondary first, to give lower priority
    defValue = _secondary.findSerializationInclusion(a, defValue);
    defValue = _primary.findSerializationInclusion(a, defValue);
    return defValue;
}
 

实例 6


/**
 * @param contentTypeSer Optional explicit type information serializer
 *    to use for contained values (only used for properties that are
 *    of container type)
 */
protected BeanPropertyWriter buildProperty(String name, JsonSerializer<Object> ser,
        TypeSerializer typeSer, TypeSerializer contentTypeSer,
        AnnotatedMember am, boolean defaultUseStaticTyping)
{
    Field f;
    Method m;
    if (am instanceof AnnotatedField) {
        m = null;
        f = ((AnnotatedField) am).getAnnotated();
    } else {
        m = ((AnnotatedMethod) am).getAnnotated();
        f = null;
    }
    // do we have annotation that forces type to use (to declared type or its super type)?
    JavaType serializationType = findSerializationType(am, defaultUseStaticTyping);
    // Container types can have separate type serializers for content (value / element) type
    if (contentTypeSer != null) {
        /* 04-Feb-2010, tatu: Let's force static typing for collection, if there is
         *    type information for contents. Should work well (for JAXB case); can be
         *    revisited if this causes problems.
         */
        if (serializationType == null) {
            serializationType = TypeFactory.type(am.getGenericType());
        }
        JavaType ct = serializationType.getContentType();
        ct.setTypeHandler(contentTypeSer);
    }
    Object suppValue = null;
    boolean suppressNulls = false;
    JsonSerialize.Inclusion methodProps = _annotationIntrospector.findSerializationInclusion(am, _outputProps);
    if (methodProps != null) {
        switch (methodProps) {
        case NON_DEFAULT:
            suppValue = getDefaultValue(name, m, f);
            if (suppValue == null) {
                suppressNulls = true;
            }
            break;
        case NON_NULL:
            suppressNulls = true;
            break;
        }
    }
    return new BeanPropertyWriter(name, ser, typeSer, serializationType, m, f, suppressNulls, suppValue);
}
 
讨论
淘淘あ西西 profile image