コード例 #1
0
 protected boolean _useStatic(
     SerializerProvider provider, BeanProperty property, JavaType referredType) {
   // First: no serializer for `Object.class`, must be dynamic
   if (referredType.isJavaLangObject()) {
     return false;
   }
   // but if type is final, might as well fetch
   if (referredType.isFinal()) { // or should we allow annotation override? (only if requested...)
     return true;
   }
   // also: if indicated by typing, should be considered static
   if (referredType.useStaticType()) {
     return true;
   }
   // if neither, maybe explicit annotation?
   AnnotationIntrospector intr = provider.getAnnotationIntrospector();
   if ((intr != null) && (property != null)) {
     Annotated ann = property.getMember();
     if (ann != null) {
       JsonSerialize.Typing t = intr.findSerializationTyping(property.getMember());
       if (t == JsonSerialize.Typing.STATIC) {
         return true;
       }
       if (t == JsonSerialize.Typing.DYNAMIC) {
         return false;
       }
     }
   }
   // and finally, may be forced by global static typing (unlikely...)
   return provider.isEnabled(MapperFeature.USE_STATIC_TYPING);
 }
コード例 #2
0
ファイル: MapSerializer.java プロジェクト: chenhn/joyplus-tv
  //  @Override
  public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
      throws JsonMappingException {
    /* 29-Sep-2012, tatu: Actually, we need to do much more contextual
     *    checking here since we finally know for sure the property,
     *    and it may have overrides
     */
    JsonSerializer<?> ser = null;
    JsonSerializer<?> keySer = null;

    // First: if we have a property, may have property-annotation overrides
    if (property != null) {
      AnnotatedMember m = property.getMember();
      if (m != null) {
        Object serDef;
        final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
        serDef = intr.findKeySerializer(m);
        if (serDef != null) {
          keySer = provider.serializerInstance(m, serDef);
        }
        serDef = intr.findContentSerializer(m);
        if (serDef != null) {
          ser = provider.serializerInstance(m, serDef);
        }
      }
    }
    if (ser == null) {
      ser = _valueSerializer;
    }
    if (ser == null) {
      // 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
      //   we can consider it a static case as well.
      if (_valueTypeIsStatic || hasContentTypeAnnotation(provider, property)) {
        ser = provider.findValueSerializer(_valueType, property);
      }
    } else if (ser instanceof ContextualSerializer) {
      ser = ((ContextualSerializer) ser).createContextual(provider, property);
    }
    if (keySer == null) {
      keySer = _keySerializer;
    }
    if (keySer == null) {
      keySer = provider.findKeySerializer(_keyType, property);
    } else if (keySer instanceof ContextualSerializer) {
      keySer = ((ContextualSerializer) keySer).createContextual(provider, property);
    }
    HashSet<String> ignored = this._ignoredEntries;
    AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if (intr != null && property != null) {
      String[] moreToIgnore = intr.findPropertiesToIgnore(property.getMember());
      if (moreToIgnore != null) {
        ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
        for (String str : moreToIgnore) {
          ignored.add(str);
        }
      }
    }
    return withResolved(property, keySer, ser, ignored);
  }
コード例 #3
0
 protected JsonSchema propertySchema(BeanProperty prop) throws JsonMappingException {
   if (prop == null) {
     throw new IllegalArgumentException("Null property");
   }
   SchemaFactoryWrapper visitor = wrapperFactory.getWrapper(getProvider());
   JsonSerializer<Object> ser = getSer(prop);
   if (ser != null) {
     JavaType type = prop.getType();
     if (type == null) {
       throw new IllegalStateException("Missing type for property '" + prop.getName() + "'");
     }
     ser.acceptJsonFormatVisitor(visitor, type);
   }
   return visitor.finalSchema();
 }
コード例 #4
0
 public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
     throws JsonMappingException {
   JsonSerializer<?> ser = null;
   if (property != null) {
     AnnotatedMember m = property.getMember();
     if (m != null) {
       Object serDef = provider.getAnnotationIntrospector().findContentSerializer(m);
       if (serDef != null) {
         ser = provider.serializerInstance(m, serDef);
       }
     }
   }
   if (ser == null) {
     ser = this._elementSerializer;
   }
   ser = findConvertingContentSerializer(provider, property, ser);
   if (ser == null) {
     ser = provider.findValueSerializer(String.class, property);
   } else {
     ser = provider.handleSecondaryContextualization(ser, property);
   }
   if (isDefaultSerializer(ser)) {
     ser = null;
   }
   return ser == this._elementSerializer ? this : new StringArraySerializer(this, property, ser);
 }
コード例 #5
0
 @Override
 public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
     throws JsonMappingException {
   TypeSerializer vts = _valueTypeSerializer;
   if (vts != null) {
     vts = vts.forProperty(property);
   }
   JsonSerializer<?> ser = findContentSerializer(provider, property);
   if (ser == null) {
     ser = _valueSerializer;
     if (ser == null) {
       // A few conditions needed to be able to fetch serializer here:
       if (_useStatic(provider, property, _referredType)) {
         ser = _findSerializer(provider, _referredType, property);
       }
     } else {
       ser = provider.handlePrimaryContextualization(ser, property);
     }
   }
   // Also: may want to have more refined exclusion based on referenced value
   JsonInclude.Include contentIncl = _contentInclusion;
   if (property != null) {
     JsonInclude.Value incl = property.findPropertyInclusion(provider.getConfig(), Optional.class);
     JsonInclude.Include newIncl = incl.getContentInclusion();
     if ((newIncl != contentIncl) && (newIncl != JsonInclude.Include.USE_DEFAULTS)) {
       contentIncl = newIncl;
     }
   }
   return withResolved(property, vts, ser, _unwrapper, contentIncl);
 }
コード例 #6
0
 protected JsonSerializer<Object> getSer(BeanProperty prop) throws JsonMappingException {
   JsonSerializer<Object> ser = null;
   // 26-Jul-2013, tatu: This is ugly, should NOT require cast...
   if (prop instanceof BeanPropertyWriter) {
     ser = ((BeanPropertyWriter) prop).getSerializer();
   }
   if (ser == null) {
     ser = getProvider().findValueSerializer(prop.getType(), prop);
   }
   return ser;
 }
  @Override
  public Object findInjectableValue(
      Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) {
    // if a container, get content type

    JavaType type = forProperty.getType();
    if (type.isCollectionLikeType() | type.isArrayType()) {
      type = type.getContentType();
    }
    // checking annotation catches properties with interface types
    if (ObjBase.class.isAssignableFrom(type.getRawClass())
        || forProperty.getMember().getAnnotated().isAnnotationPresent(Reference.class)) {
      try {
        return super.findInjectableValue(valueId, ctxt, forProperty, beanInstance);
      } catch (IllegalArgumentException e) {
        Object value = getValue(valueId, type);
        return value;
      }

    } else {
      return super.findInjectableValue(valueId, ctxt, forProperty, beanInstance);
    }
  }
コード例 #8
0
 // !!! TODO: added late in 2.7 in `jackson-databind`: remove from 2.8
 protected JsonSerializer<?> findContentSerializer(
     SerializerProvider serializers, BeanProperty property) throws JsonMappingException {
   if (property != null) {
     AnnotatedMember m = property.getMember();
     final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
     if (m != null) {
       Object serDef = intr.findContentSerializer(m);
       if (serDef != null) {
         return serializers.serializerInstance(m, serDef);
       }
     }
   }
   return null;
 }
コード例 #9
0
 public JsonSerializer<?> createContextual(
     SerializerProvider paramSerializerProvider, BeanProperty paramBeanProperty)
     throws JsonMappingException {
   Object localObject2 = null;
   Object localObject1 = localObject2;
   if (paramBeanProperty != null) {
     AnnotatedMember localAnnotatedMember = paramBeanProperty.getMember();
     localObject1 = localObject2;
     if (localAnnotatedMember != null) {
       Object localObject3 =
           paramSerializerProvider
               .getAnnotationIntrospector()
               .findContentSerializer(localAnnotatedMember);
       localObject1 = localObject2;
       if (localObject3 != null) {
         localObject1 =
             paramSerializerProvider.serializerInstance(localAnnotatedMember, localObject3);
       }
     }
   }
   localObject2 = localObject1;
   if (localObject1 == null) {
     localObject2 = this._valueSerializer;
   }
   localObject1 =
       findConvertingContentSerializer(
           paramSerializerProvider, paramBeanProperty, (JsonSerializer) localObject2);
   if (localObject1 == null) {
     if (!this._staticTyping) {
       break label111;
     }
     paramSerializerProvider =
         withValueSerializer(
             paramBeanProperty,
             paramSerializerProvider.findValueSerializer(this._valueType, paramBeanProperty));
   }
   label111:
   do {
     return paramSerializerProvider;
     localObject1 =
         paramSerializerProvider.handleSecondaryContextualization(
             (JsonSerializer) localObject1, paramBeanProperty);
     paramSerializerProvider = this;
   } while (localObject1 == this._valueSerializer);
   return withValueSerializer(paramBeanProperty, (JsonSerializer) localObject1);
 }
コード例 #10
0
 @Override
 public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
     throws JsonMappingException {
   /* 29-Sep-2012, tatu: Actually, we need to do much more contextual
    *    checking here since we finally know for sure the property,
    *    and it may have overrides
    */
   JsonSerializer<?> ser = null;
   // First: if we have a property, may have property-annotation overrides
   if (property != null) {
     AnnotatedMember m = property.getMember();
     if (m != null) {
       Object serDef = provider.getAnnotationIntrospector().findContentSerializer(m);
       if (serDef != null) {
         ser = provider.serializerInstance(m, serDef);
       }
     }
   }
   if (ser == null) {
     ser = _serializer;
   }
   // #124: May have a content converter
   ser = findConvertingContentSerializer(provider, property, ser);
   if (ser == null) {
     ser = provider.findValueSerializer(String.class, property);
   } else if (ser instanceof ContextualSerializer) {
     ser = ((ContextualSerializer) ser).createContextual(provider, property);
   }
   // Optimization: default serializer just writes String, so we can avoid a call:
   if (isDefaultSerializer(ser)) {
     ser = null;
   }
   // note: will never have TypeSerializer, because Strings are "natural" type
   if (ser == _serializer) {
     return this;
   }
   return new IndexedStringListSerializer(ser);
 }
コード例 #11
0
  @Override
  public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
      throws JsonMappingException {
    if (property != null) {
      JsonFormat.Value ann =
          prov.getAnnotationIntrospector().findFormat((Annotated) property.getMember());
      if (ann != null) {
        JacksonJodaDateFormat format = _format;

        Boolean useTimestamp;

        // Simple case first: serialize as numeric timestamp?
        if (ann.getShape().isNumeric()) {
          useTimestamp = Boolean.TRUE;
        } else if (ann.getShape() == JsonFormat.Shape.STRING) {
          useTimestamp = Boolean.FALSE;
        } else if (ann.getShape() == JsonFormat.Shape.ARRAY) {
          // 17-Nov-2014, tatu: also, arrays typically contain non-string representation
          useTimestamp = Boolean.TRUE;
        } else {
          useTimestamp = null;
        }
        // must not call if flag defined, to rely on defaults:
        if (useTimestamp != null) {
          format = format.withUseTimestamp(useTimestamp);
        }
        // for others, safe to call, null/empty just ignored
        format = format.withFormat(ann.getPattern().trim());
        format = format.withLocale(ann.getLocale());
        format = format.withTimeZone(ann.getTimeZone());
        if (format != _format) {
          return withFormat(format);
        }
      }
    }
    return this;
  }
コード例 #12
0
 @Override
 public KeyDeserializer createContextual(DeserializationContext ctxt, BeanProperty property)
     throws JsonMappingException {
   return new ContextualDeser((property == null) ? "ROOT" : property.getName());
 }
コード例 #13
0
 /**
  * Override this to add information specific to the property of bean For example, bean validation
  * annotations could be used to specify value constraints in the schema
  *
  * @param beanProperty
  */
 public void enrichWithBeanProperty(BeanProperty beanProperty) {
   setDescription(beanProperty.getMetadata().getDescription());
 }
コード例 #14
0
 public JsonSerializer<?> createContextual(
     SerializerProvider paramSerializerProvider, BeanProperty paramBeanProperty)
     throws JsonMappingException {
   Object localObject1 = this._valueTypeSerializer;
   Object localObject3 = localObject1;
   if (localObject1 != null) {
     localObject3 = ((TypeSerializer) localObject1).forProperty(paramBeanProperty);
   }
   Object localObject2 = null;
   JsonFormat.Value localValue = null;
   Object localObject5 = null;
   Object localObject4 = localObject5;
   if (paramBeanProperty != null) {
     localObject4 = paramBeanProperty.getMember();
     localObject2 = paramSerializerProvider.getAnnotationIntrospector();
     localObject1 = localValue;
     if (localObject4 != null) {
       Object localObject6 =
           ((AnnotationIntrospector) localObject2).findContentSerializer((Annotated) localObject4);
       localObject1 = localValue;
       if (localObject6 != null) {
         localObject1 =
             paramSerializerProvider.serializerInstance((Annotated) localObject4, localObject6);
       }
     }
     localValue = paramBeanProperty.findFormatOverrides((AnnotationIntrospector) localObject2);
     localObject2 = localObject1;
     localObject4 = localObject5;
     if (localValue != null) {
       localObject4 = localValue.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
       localObject2 = localObject1;
     }
   }
   localObject1 = localObject2;
   if (localObject2 == null) {
     localObject1 = this._elementSerializer;
   }
   localObject2 =
       findConvertingContentSerializer(
           paramSerializerProvider, paramBeanProperty, (JsonSerializer) localObject1);
   if (localObject2 == null) {
     localObject1 = localObject2;
     if (this._elementType != null) {
       if (!this._staticTyping) {
         localObject1 = localObject2;
         if (!hasContentTypeAnnotation(paramSerializerProvider, paramBeanProperty)) {
           break label185;
         }
       }
     }
   }
   for (localObject1 =
           paramSerializerProvider.findValueSerializer(this._elementType, paramBeanProperty);
       ;
       localObject1 =
           paramSerializerProvider.handleSecondaryContextualization(
               (JsonSerializer) localObject2, paramBeanProperty)) {
     label185:
     return withResolved(
         paramBeanProperty,
         (TypeSerializer) localObject3,
         (JsonSerializer) localObject1,
         (Boolean) localObject4);
   }
 }
コード例 #15
0
  @Override
  public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
      throws JsonMappingException {
    JsonSerializer<?> ser = null;
    JsonSerializer<?> keySer = null;
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember();

    // First: if we have a property, may have property-annotation overrides
    if ((propertyAcc != null) && (intr != null)) {
      Object serDef = intr.findKeySerializer(propertyAcc);
      if (serDef != null) {
        keySer = provider.serializerInstance(propertyAcc, serDef);
      }
      serDef = intr.findContentSerializer(propertyAcc);
      if (serDef != null) {
        ser = provider.serializerInstance(propertyAcc, serDef);
      }
    }
    if (ser == null) {
      ser = _valueSerializer;
    }
    // [databind#124]: May have a content converter
    ser = findContextualConvertingSerializer(provider, property, ser);
    if (ser == null) {
      // 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
      //   we can consider it a static case as well.
      // 20-Aug-2013, tatu: Need to avoid trying to access serializer for java.lang.Object tho
      if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
        ser = provider.findValueSerializer(_valueType, property);
      }
    }
    if (keySer == null) {
      keySer = _keySerializer;
    }
    if (keySer == null) {
      keySer = provider.findKeySerializer(_keyType, property);
    } else {
      keySer = provider.handleSecondaryContextualization(keySer, property);
    }
    Set<String> ignored = _ignoredEntries;
    boolean sortKeys = false;
    if ((intr != null) && (propertyAcc != null)) {
      JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(propertyAcc);
      if (ignorals != null) {
        Set<String> newIgnored = ignorals.findIgnoredForSerialization();
        if ((newIgnored != null) && !newIgnored.isEmpty()) {
          ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
          for (String str : newIgnored) {
            ignored.add(str);
          }
        }
      }
      Boolean b = intr.findSerializationSortAlphabetically(propertyAcc);
      sortKeys = (b != null) && b.booleanValue();
    }
    JsonFormat.Value format = findFormatOverrides(provider, property, Map.class);
    if (format != null) {
      Boolean B = format.getFeature(JsonFormat.Feature.WRITE_SORTED_MAP_ENTRIES);
      if (B != null) {
        sortKeys = B.booleanValue();
      }
    }
    MapSerializer mser = withResolved(property, keySer, ser, ignored, sortKeys);

    // [databind#307]: allow filtering
    if (property != null) {
      AnnotatedMember m = property.getMember();
      if (m != null) {
        Object filterId = intr.findFilterId(m);
        if (filterId != null) {
          mser = mser.withFilterId(filterId);
        }
      }
      JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null);
      if (inclV != null) {
        JsonInclude.Include incl = inclV.getContentInclusion();

        if (incl != JsonInclude.Include.USE_DEFAULTS) {
          Object valueToSuppress;
          boolean suppressNulls;
          switch (incl) {
            case NON_DEFAULT:
              valueToSuppress = BeanUtil.getDefaultValue(_valueType);
              suppressNulls = true;
              if (valueToSuppress != null) {
                if (valueToSuppress.getClass().isArray()) {
                  valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
                }
              }
              break;
            case NON_ABSENT:
              suppressNulls = true;
              valueToSuppress = _valueType.isReferenceType() ? MARKER_FOR_EMPTY : null;
              break;
            case NON_EMPTY:
              suppressNulls = true;
              valueToSuppress = MARKER_FOR_EMPTY;
              break;
            case CUSTOM:
              valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
              if (valueToSuppress == null) { // is this legal?
                suppressNulls = true;
              } else {
                suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
              }
              break;
            case NON_NULL:
              valueToSuppress = null;
              suppressNulls = true;
              break;
            case ALWAYS: // default
            default:
              valueToSuppress = null;
              // 30-Sep-2016, tatu: Should not need to check global flags here,
              //   if inclusion forced to be ALWAYS
              suppressNulls = false;
              break;
          }
          mser = mser.withContentInclusion(valueToSuppress, suppressNulls);
        }
      }
    }
    return mser;
  }