Ejemplo n.º 1
0
  /** Method called to serialize fields, when the value type is not statically known. */
  public void serializeFields(Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    // If value type needs polymorphic type handling, some more work needed:
    if (_valueTypeSerializer != null) {
      serializeTypedFields(value, jgen, provider);
      return;
    }
    final JsonSerializer<Object> keySerializer = _keySerializer;

    final HashSet<String> ignored = _ignoredEntries;
    final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);

    PropertySerializerMap serializers = _dynamicValueSerializers;

    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] 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> serializer = serializers.serializerFor(cc);
        if (serializer == null) {
          if (_valueType.hasGenericTypes()) {
            serializer =
                _findAndAddDynamic(
                    serializers, provider.constructSpecializedType(_valueType, cc), provider);
          } else {
            serializer = _findAndAddDynamic(serializers, cc, provider);
          }
          serializers = _dynamicValueSerializers;
        }
        try {
          serializer.serialize(valueElem, jgen, provider);
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          String keyDesc = "" + keyElem;
          wrapAndThrow(provider, e, value, keyDesc);
        }
      }
    }
  }
Ejemplo n.º 2
0
 private final JsonSerializer<Object> _findSerializer(SerializerProvider provider, Object value)
     throws JsonMappingException {
   final Class<?> cc = value.getClass();
   JsonSerializer<Object> valueSer = _dynamicValueSerializers.serializerFor(cc);
   if (valueSer != null) {
     return valueSer;
   }
   if (_valueType.hasGenericTypes()) {
     return _findAndAddDynamic(
         _dynamicValueSerializers, provider.constructSpecializedType(_valueType, cc), provider);
   }
   return _findAndAddDynamic(_dynamicValueSerializers, cc, provider);
 }
  // 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;
  }
 public void serializeContents(
     Object[] paramArrayOfObject,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider)
     throws IOException {
   int k = paramArrayOfObject.length;
   if (k == 0) {
     return;
   }
   if (this._elementSerializer != null) {
     serializeContentsUsing(
         paramArrayOfObject, paramJsonGenerator, paramSerializerProvider, this._elementSerializer);
     return;
   }
   if (this._valueTypeSerializer != null) {
     serializeTypedContents(paramArrayOfObject, paramJsonGenerator, paramSerializerProvider);
     return;
   }
   int j = 0;
   int i = 0;
   Object localObject1 = null;
   for (; ; ) {
     try {
       localPropertySerializerMap = this._dynamicSerializers;
       if (i >= k) {
         break;
       }
       localObject2 = paramArrayOfObject[i];
       if (localObject2 == null) {
         localObject1 = localObject2;
         j = i;
         paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
         break label268;
       }
       localObject1 = localObject2;
       j = i;
       localClass = localObject2.getClass();
       localObject1 = localObject2;
       j = i;
       JsonSerializer localJsonSerializer2 = localPropertySerializerMap.serializerFor(localClass);
       localJsonSerializer1 = localJsonSerializer2;
       if (localJsonSerializer2 == null) {
         localObject1 = localObject2;
         j = i;
         if (!this._elementType.hasGenericTypes()) {
           continue;
         }
         localObject1 = localObject2;
         j = i;
         localJsonSerializer1 =
             _findAndAddDynamic(
                 localPropertySerializerMap,
                 paramSerializerProvider.constructSpecializedType(this._elementType, localClass),
                 paramSerializerProvider);
       }
     } catch (IOException paramArrayOfObject) {
       PropertySerializerMap localPropertySerializerMap;
       Object localObject2;
       Class localClass;
       throw paramArrayOfObject;
       localObject1 = localObject2;
       j = i;
       JsonSerializer localJsonSerializer1 =
           _findAndAddDynamic(localPropertySerializerMap, localClass, paramSerializerProvider);
       continue;
     } catch (Exception paramArrayOfObject) {
       if ((!(paramArrayOfObject instanceof InvocationTargetException))
           || (paramArrayOfObject.getCause() == null)) {
         continue;
       }
       paramArrayOfObject = paramArrayOfObject.getCause();
       continue;
       if (!(paramArrayOfObject instanceof Error)) {
         continue;
       }
       throw ((Error) paramArrayOfObject);
       throw JsonMappingException.wrapWithPath(paramArrayOfObject, localObject1, j);
     }
     localObject1 = localObject2;
     j = i;
     localJsonSerializer1.serialize(localObject2, paramJsonGenerator, paramSerializerProvider);
     label268:
     i += 1;
   }
 }