示例#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);
        }
      }
    }
  }
示例#2
0
 @Override
 public void serialize(Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider)
     throws IOException, JsonGenerationException {
   jgen.writeStartObject();
   if (!value.isEmpty()) {
     if (provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
       value = _orderEntries(value);
     }
     if (_valueSerializer != null) {
       serializeFieldsUsing(value, jgen, provider, _valueSerializer);
     } else {
       serializeFields(value, jgen, provider);
     }
   }
   jgen.writeEndObject();
 }
示例#3
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);
        }
      }
    }
  }
示例#4
0
 @Override
 public void serializeWithType(
     Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)
     throws IOException, JsonGenerationException {
   typeSer.writeTypePrefixForObject(value, jgen);
   if (!value.isEmpty()) {
     if (provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
       value = _orderEntries(value);
     }
     if (_valueSerializer != null) {
       serializeFieldsUsing(value, jgen, provider, _valueSerializer);
     } else {
       serializeFields(value, jgen, provider);
     }
   }
   typeSer.writeTypeSuffixForObject(value, jgen);
 }
  @Override
  public void serialize(List<String> value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    final int len = value.size();
    // [JACKSON-805]
    if ((len == 1) && provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)) {
      _serializeUnwrapped(value, jgen, provider);
      return;
    }

    jgen.writeStartArray();
    if (_serializer == null) {
      serializeContents(value, jgen, provider, len);
    } else {
      serializeUsingCustom(value, jgen, provider, len);
    }
    jgen.writeEndArray();
  }
 @Override
 public void serialize(Map<?, ?> value, JsonGenerator gen, SerializerProvider provider)
     throws IOException {
   gen.writeStartObject(value);
   if (!value.isEmpty()) {
     if (_sortKeys || provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
       value = _orderEntries(value, gen, provider);
     }
     PropertyFilter pf;
     if ((_filterId != null) && (pf = findPropertyFilter(provider, _filterId, value)) != null) {
       serializeFilteredFields(value, gen, provider, pf, _suppressableValue);
     } else if ((_suppressableValue != null) || _suppressNulls) {
       serializeOptionalFields(value, gen, provider, _suppressableValue);
     } else if (_valueSerializer != null) {
       serializeFieldsUsing(value, gen, provider, _valueSerializer);
     } else {
       serializeFields(value, gen, provider);
     }
   }
   gen.writeEndObject();
 }
示例#7
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);
        }
      }
    }
  }
 @Override
 public void serializeWithType(
     Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer)
     throws IOException {
   typeSer.writeTypePrefixForObject(value, gen);
   // [databind#631]: Assign current value, to be accessible by custom serializers
   gen.setCurrentValue(value);
   if (!value.isEmpty()) {
     if (_sortKeys || provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
       value = _orderEntries(value, gen, provider);
     }
     PropertyFilter pf;
     if ((_filterId != null) && (pf = findPropertyFilter(provider, _filterId, value)) != null) {
       serializeFilteredFields(value, gen, provider, pf, _suppressableValue);
     } else if ((_suppressableValue != null) || _suppressNulls) {
       serializeOptionalFields(value, gen, provider, _suppressableValue);
     } else if (_valueSerializer != null) {
       serializeFieldsUsing(value, gen, provider, _valueSerializer);
     } else {
       serializeFields(value, gen, provider);
     }
   }
   typeSer.writeTypeSuffixForObject(value, gen);
 }