@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);
 }
 @Override
 public void serializeWithType(
     byte[] value, JsonGenerator g, SerializerProvider provider, TypeSerializer typeSer)
     throws IOException {
   typeSer.writeTypePrefixForScalar(value, g);
   g.writeBinary(provider.getConfig().getBase64Variant(), value, 0, value.length);
   typeSer.writeTypeSuffixForScalar(value, g);
 }
 public JsonNode getSchema(SerializerProvider paramSerializerProvider, Type paramType)
     throws JsonMappingException {
   ObjectNode localObjectNode1 = createSchemaNode("object", true);
   if ((paramType instanceof ParameterizedType)) {
     Object localObject = ((ParameterizedType) paramType).getActualTypeArguments();
     if (localObject.length == 2) {
       paramType = paramSerializerProvider.constructType(localObject[0]);
       localObject = paramSerializerProvider.constructType(localObject[1]);
       ObjectNode localObjectNode2 = JsonNodeFactory.instance.objectNode();
       Enum[] arrayOfEnum = (Enum[]) paramType.getRawClass().getEnumConstants();
       int j = arrayOfEnum.length;
       int i = 0;
       if (i < j) {
         Enum localEnum = arrayOfEnum[i];
         paramType =
             paramSerializerProvider.findValueSerializer(
                 ((JavaType) localObject).getRawClass(), this._property);
         if ((paramType instanceof SchemaAware)) {}
         for (paramType = ((SchemaAware) paramType).getSchema(paramSerializerProvider, null);
             ;
             paramType = JsonSchema.getDefaultSchemaNode()) {
           localObjectNode2.set(
               paramSerializerProvider
                   .getConfig()
                   .getAnnotationIntrospector()
                   .findEnumValue(localEnum),
               paramType);
           i += 1;
           break;
         }
       }
       localObjectNode1.set("properties", localObjectNode2);
     }
   }
   return localObjectNode1;
 }
 @Override
 public void serialize(byte[] value, JsonGenerator g, SerializerProvider provider)
     throws IOException {
   g.writeBinary(provider.getConfig().getBase64Variant(), value, 0, value.length);
 }
예제 #5
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;
  }