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);
 }
 @Override
 public JsonArrayFormatVisitor expectArrayFormat(JavaType type) throws JsonMappingException {
   serializerProvider
       .findValueSerializer(type.getContentType())
       .acceptJsonFormatVisitor(createSubtraverser(baseName), type.getContentType());
   return new JsonArrayFormatVisitor.Base(serializerProvider);
 }
 public JsonNode getSchema(SerializerProvider paramSerializerProvider, Type paramType)
     throws JsonMappingException {
   ObjectNode localObjectNode = createSchemaNode("array", true);
   if (paramType != null) {
     paramType = paramSerializerProvider.constructType(paramType);
     if (paramType.isArrayType()) {
       paramType = ((ArrayType) paramType).getContentType().getRawClass();
       if (paramType != Object.class) {
         break label54;
       }
       localObjectNode.set("items", JsonSchema.getDefaultSchemaNode());
     }
   }
   return localObjectNode;
   label54:
   paramType = paramSerializerProvider.findValueSerializer(paramType, this._property);
   if ((paramType instanceof SchemaAware)) {}
   for (paramSerializerProvider =
           ((SchemaAware) paramType).getSchema(paramSerializerProvider, null);
       ;
       paramSerializerProvider = JsonSchema.getDefaultSchemaNode()) {
     localObjectNode.set("items", paramSerializerProvider);
     return localObjectNode;
   }
 }
Exemplo n.º 4
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;
    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);
  }
  // need to override as we must get unwrapping instance...
  @Override
  protected JsonSerializer<Object> _findAndAddDynamic(
      PropertySerializerMap map, Class<?> type, SerializerProvider provider)
      throws JsonMappingException {
    JsonSerializer<Object> serializer;
    if (_nonTrivialBaseType != null) {
      JavaType subtype = provider.constructSpecializedType(_nonTrivialBaseType, type);
      serializer = provider.findValueSerializer(subtype, this);
    } else {
      serializer = provider.findValueSerializer(type, this);
    }
    NameTransformer t = _nameTransformer;
    if (serializer.isUnwrappingSerializer()) {
      t =
          NameTransformer.chainedTransformer(
              t, ((UnwrappingBeanSerializer) serializer)._nameTransformer);
    }
    serializer = serializer.unwrappingSerializer(t);

    _dynamicSerializers = _dynamicSerializers.newWith(type, serializer);
    return serializer;
  }
Exemplo n.º 6
0
  protected void serializeTypedFields(
      Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    JsonSerializer<Object> prevValueSerializer = null;
    Class<?> prevValueClass = null;
    final HashSet<String> ignored = _ignoredEntries;
    final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object valueElem = entry.getValue();
      // First, serialize key
      Object keyElem = entry.getKey();
      if (keyElem == null) {
        provider.findNullKeySerializer(_keyType, _property).serialize(null, jgen, provider);
      } else {
        // [JACKSON-314] also may need to skip entries with null values
        if (skipNulls && valueElem == null) continue;
        // One twist: is entry ignorable? If so, skip
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer.serialize(keyElem, jgen, provider);
      }

      // And then value
      if (valueElem == null) {
        provider.defaultSerializeNull(jgen);
      } else {
        Class<?> cc = valueElem.getClass();
        JsonSerializer<Object> currSerializer;
        if (cc == prevValueClass) {
          currSerializer = prevValueSerializer;
        } else {
          currSerializer = provider.findValueSerializer(cc, _property);
          prevValueSerializer = currSerializer;
          prevValueClass = cc;
        }
        try {
          currSerializer.serializeWithType(valueElem, jgen, provider, _valueTypeSerializer);
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          String keyDesc = "" + keyElem;
          wrapAndThrow(provider, e, value, keyDesc);
        }
      }
    }
  }
 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);
 }
 @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);
 }
 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;
 }
 protected void serializeContentsUsing(
     EnumMap<? extends Enum<?>, ?> paramEnumMap,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider,
     JsonSerializer<Object> paramJsonSerializer)
     throws IOException, JsonGenerationException {
   Object localObject1 = this._keyEnums;
   int i;
   boolean bool;
   TypeSerializer localTypeSerializer;
   Iterator localIterator;
   if (!paramSerializerProvider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
     i = 1;
     bool = paramSerializerProvider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
     localTypeSerializer = this._valueTypeSerializer;
     localIterator = paramEnumMap.entrySet().iterator();
   }
   for (; ; ) {
     label45:
     if (!localIterator.hasNext()) {
       return;
     }
     Map.Entry localEntry = (Map.Entry) localIterator.next();
     Object localObject3 = localEntry.getValue();
     if ((i == 0) || (localObject3 != null)) {
       Enum localEnum = (Enum) localEntry.getKey();
       if (bool) {
         paramJsonGenerator.writeFieldName(localEnum.toString());
       }
       for (; ; ) {
         if (localObject3 != null) {
           break label182;
         }
         paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
         break label45;
         i = 0;
         break;
         Object localObject2 = localObject1;
         if (localObject1 == null) {
           localObject2 =
               ((EnumSerializer)
                       paramSerializerProvider.findValueSerializer(
                           localEnum.getDeclaringClass(), this._property))
                   .getEnumValues();
         }
         paramJsonGenerator.writeFieldName(
             ((EnumValues) localObject2).serializedValueFor(localEnum));
         localObject1 = localObject2;
       }
       label182:
       if (localTypeSerializer == null) {
         try {
           paramJsonSerializer.serialize(
               localObject3, paramJsonGenerator, paramSerializerProvider);
         } catch (Exception localException) {
           wrapAndThrow(
               paramSerializerProvider,
               localException,
               paramEnumMap,
               ((Enum) localEntry.getKey()).name());
         }
       } else {
         paramJsonSerializer.serializeWithType(
             localObject3, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
       }
     }
   }
 }
 protected void serializeContents(
     EnumMap<? extends Enum<?>, ?> paramEnumMap,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider)
     throws IOException, JsonGenerationException {
   if (this._valueSerializer != null) {
     serializeContentsUsing(
         paramEnumMap, paramJsonGenerator, paramSerializerProvider, this._valueSerializer);
   }
   label70:
   label207:
   label291:
   label303:
   for (; ; ) {
     return;
     Object localObject1 = null;
     Object localObject4 = null;
     Object localObject2 = this._keyEnums;
     int i;
     boolean bool;
     TypeSerializer localTypeSerializer;
     Iterator localIterator;
     if (!paramSerializerProvider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
       i = 1;
       bool = paramSerializerProvider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
       localTypeSerializer = this._valueTypeSerializer;
       localIterator = paramEnumMap.entrySet().iterator();
     }
     for (; ; ) {
       if (!localIterator.hasNext()) {
         break label303;
       }
       Map.Entry localEntry = (Map.Entry) localIterator.next();
       Object localObject6 = localEntry.getValue();
       if ((i == 0) || (localObject6 != null)) {
         Object localObject5 = (Enum) localEntry.getKey();
         if (bool) {
           paramJsonGenerator.writeFieldName(((Enum) localObject5).toString());
         }
         Object localObject3;
         for (; ; ) {
           if (localObject6 != null) {
             break label207;
           }
           paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
           break label70;
           i = 0;
           break;
           localObject3 = localObject2;
           if (localObject2 == null) {
             localObject3 =
                 ((EnumSerializer)
                         paramSerializerProvider.findValueSerializer(
                             ((Enum) localObject5).getDeclaringClass(), this._property))
                     .getEnumValues();
           }
           paramJsonGenerator.writeFieldName(
               ((EnumValues) localObject3).serializedValueFor((Enum) localObject5));
           localObject2 = localObject3;
         }
         localObject5 = localObject6.getClass();
         if (localObject5 == localObject4) {
           localObject3 = localObject1;
         }
         JsonSerializer localJsonSerializer;
         for (; ; ) {
           if (localTypeSerializer != null) {
             break label291;
           }
           try {
             ((JsonSerializer) localObject3)
                 .serialize(localObject6, paramJsonGenerator, paramSerializerProvider);
           } catch (Exception localException) {
             wrapAndThrow(
                 paramSerializerProvider,
                 localException,
                 paramEnumMap,
                 ((Enum) localEntry.getKey()).name());
           }
           break;
           localJsonSerializer =
               paramSerializerProvider.findValueSerializer((Class) localObject5, this._property);
           localObject1 = localJsonSerializer;
           localObject4 = localObject5;
         }
         localJsonSerializer.serializeWithType(
             localObject6, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
       }
     }
   }
 }
 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);
   }
 }
Exemplo n.º 13
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;
  }