Ejemplo n.º 1
0
  /**
   * Method called to serialize fields, when the value type is statically known, so that value
   * serializer is passed and does not need to be fetched from provider.
   */
  public void serializeFieldsUsing(
      Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, JsonSerializer<Object> ser)
      throws IOException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final Set<String> ignored = _ignoredEntries;
    final TypeSerializer typeSer = _valueTypeSerializer;

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object keyElem = entry.getKey();
      if (ignored != null && ignored.contains(keyElem)) continue;

      if (keyElem == null) {
        provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
      } else {
        keySerializer.serialize(keyElem, gen, provider);
      }
      final Object valueElem = entry.getValue();
      if (valueElem == null) {
        provider.defaultSerializeNull(gen);
      } else {
        try {
          if (typeSer == null) {
            ser.serialize(valueElem, gen, provider);
          } else {
            ser.serializeWithType(valueElem, gen, provider, typeSer);
          }
        } catch (Exception e) {
          wrapAndThrow(provider, e, value, String.valueOf(keyElem));
        }
      }
    }
  }
 public void serializeTypedContents(
     Object[] paramArrayOfObject,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider)
     throws IOException {
   int k = paramArrayOfObject.length;
   TypeSerializer localTypeSerializer = this._valueTypeSerializer;
   int j = 0;
   int i = 0;
   Object localObject1 = null;
   for (; ; ) {
     try {
       PropertySerializerMap localPropertySerializerMap = this._dynamicSerializers;
       if (i < k) {
         Object localObject2 = paramArrayOfObject[i];
         if (localObject2 == null) {
           localObject1 = localObject2;
           j = i;
           paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
         } else {
           localObject1 = localObject2;
           j = i;
           Class localClass = localObject2.getClass();
           localObject1 = localObject2;
           j = i;
           JsonSerializer localJsonSerializer2 =
               localPropertySerializerMap.serializerFor(localClass);
           JsonSerializer localJsonSerializer1 = localJsonSerializer2;
           if (localJsonSerializer2 == null) {
             localObject1 = localObject2;
             j = i;
             localJsonSerializer1 =
                 _findAndAddDynamic(
                     localPropertySerializerMap, localClass, paramSerializerProvider);
           }
           localObject1 = localObject2;
           j = i;
           localJsonSerializer1.serializeWithType(
               localObject2, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
         }
       }
     } catch (IOException paramArrayOfObject) {
       throw paramArrayOfObject;
     } catch (Exception paramArrayOfObject) {
       if (((paramArrayOfObject instanceof InvocationTargetException))
           && (paramArrayOfObject.getCause() != null)) {
         paramArrayOfObject = paramArrayOfObject.getCause();
         continue;
       }
       if ((paramArrayOfObject instanceof Error)) {
         throw ((Error) paramArrayOfObject);
       }
       throw JsonMappingException.wrapWithPath(paramArrayOfObject, localObject1, j);
     }
     return;
     i += 1;
   }
 }
Ejemplo n.º 3
0
  /** @since 2.5 */
  public void serializeTypedFields(
      Map<?, ?> value,
      JsonGenerator gen,
      SerializerProvider provider,
      Object suppressableValue) // since 2.5
      throws IOException {
    final Set<String> ignored = _ignoredEntries;
    final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object keyElem = entry.getKey();
      JsonSerializer<Object> keySerializer;
      if (keyElem == null) {
        keySerializer = provider.findNullKeySerializer(_keyType, _property);
      } else {
        // One twist: is entry ignorable? If so, skip
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer = _keySerializer;
      }
      final Object valueElem = entry.getValue();

      // And then value
      JsonSerializer<Object> valueSer;
      if (valueElem == null) {
        if (_suppressNulls) { // all suppression include null suppression
          continue;
        }
        valueSer = provider.getDefaultNullValueSerializer();
      } else {
        valueSer = _valueSerializer;
        if (valueSer == null) {
          valueSer = _findSerializer(provider, valueElem);
        }
        // also may need to skip non-empty values:
        if (checkEmpty) {
          if (valueSer.isEmpty(provider, valueElem)) {
            continue;
          }
        } else if (suppressableValue != null) {
          if (suppressableValue.equals(valueElem)) {
            continue;
          }
        }
      }
      keySerializer.serialize(keyElem, gen, provider);
      try {
        valueSer.serializeWithType(valueElem, gen, provider, _valueTypeSerializer);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, String.valueOf(keyElem));
      }
    }
  }
Ejemplo n.º 4
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);
        }
      }
    }
  }
  @Override
  public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)
      throws Exception {
    Object value = get(bean);
    if (value == null) {
      // Hmmh. I assume we MUST pretty much suppress nulls, since we
      // can't really unwrap them...
      return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
      Class<?> cls = value.getClass();
      PropertySerializerMap map = _dynamicSerializers;
      ser = map.serializerFor(cls);
      if (ser == null) {
        ser = _findAndAddDynamic(map, cls, prov);
      }
    }
    if (_suppressableValue != null) {
      if (MARKER_FOR_EMPTY == _suppressableValue) {
        if (ser.isEmpty(value)) {
          return;
        }
      } else if (_suppressableValue.equals(value)) {
        return;
      }
    }
    // For non-nulls, first: simple check for direct cycles
    if (value == bean) {
      if (_handleSelfReference(bean, jgen, prov, ser)) {
        return;
      }
    }

    // note: must verify we are using unwrapping serializer; if not, will write field name
    if (!ser.isUnwrappingSerializer()) {
      jgen.writeFieldName(_name);
    }

    if (_typeSerializer == null) {
      ser.serialize(value, jgen, prov);
    } else {
      ser.serializeWithType(value, jgen, prov, _typeSerializer);
    }
  }
 public void serializeContentsUsing(
     Object[] paramArrayOfObject,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider,
     JsonSerializer<Object> paramJsonSerializer)
     throws IOException {
   int j = paramArrayOfObject.length;
   TypeSerializer localTypeSerializer = this._valueTypeSerializer;
   int i = 0;
   for (; ; ) {
     Object localObject;
     if (i < j) {
       localObject = paramArrayOfObject[i];
       if (localObject == null) {}
       try {
         paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
       } catch (IOException paramArrayOfObject) {
         throw paramArrayOfObject;
         paramJsonSerializer.serializeWithType(
             localObject, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
       } catch (Exception paramArrayOfObject) {
         while (((paramArrayOfObject instanceof InvocationTargetException))
             && (paramArrayOfObject.getCause() != null)) {
           paramArrayOfObject = paramArrayOfObject.getCause();
         }
         if (!(paramArrayOfObject instanceof Error)) {
           break label108;
         }
         throw ((Error) paramArrayOfObject);
         throw JsonMappingException.wrapWithPath(paramArrayOfObject, localObject, i);
       }
       if (localTypeSerializer == null) {
         paramJsonSerializer.serialize(localObject, paramJsonGenerator, paramSerializerProvider);
         break label118;
       }
     }
     label108:
     return;
     label118:
     i += 1;
   }
 }
 @Override
 public void serialize(Optional<?> opt, JsonGenerator gen, SerializerProvider provider)
     throws IOException {
   if (!opt.isPresent()) {
     // 22-Oct-2015, tatu: With unwrapping we can not serialize value, just key/value pairs so:
     if (_unwrapper == null) {
       provider.defaultSerializeNull(gen);
     }
     return;
   }
   Object value = opt.get();
   JsonSerializer<Object> ser = _valueSerializer;
   if (ser == null) {
     ser = _findCachedSerializer(provider, value.getClass());
   }
   if (_valueTypeSerializer != null) {
     ser.serializeWithType(value, gen, provider, _valueTypeSerializer);
   } else {
     ser.serialize(value, gen, provider);
   }
 }
Ejemplo n.º 8
0
  /**
   * Method called to serialize fields, when the value type is statically known, so that value
   * serializer is passed and does not need to be fetched from provider.
   */
  protected void serializeFieldsUsing(
      Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider, JsonSerializer<Object> ser)
      throws IOException, JsonGenerationException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final HashSet<String> ignored = _ignoredEntries;
    final TypeSerializer typeSer = _valueTypeSerializer;
    final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object valueElem = entry.getValue();
      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;
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer.serialize(keyElem, jgen, provider);
      }
      if (valueElem == null) {
        provider.defaultSerializeNull(jgen);
      } else {
        try {
          if (typeSer == null) {
            ser.serialize(valueElem, jgen, provider);
          } else {
            ser.serializeWithType(valueElem, jgen, provider, typeSer);
          }
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          String keyDesc = "" + keyElem;
          wrapAndThrow(provider, e, value, keyDesc);
        }
      }
    }
  }
 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);
       }
     }
   }
 }