/**
   * 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));
        }
      }
    }
  }
Example #2
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);
        }
      }
    }
  }
Example #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);
        }
      }
    }
  }
 private final void serializeContents(
     List<String> value, JsonGenerator jgen, SerializerProvider provider, int len)
     throws IOException, JsonGenerationException {
   int i = 0;
   try {
     for (; i < len; ++i) {
       String str = value.get(i);
       if (str == null) {
         provider.defaultSerializeNull(jgen);
       } else {
         jgen.writeString(str);
       }
     }
   } catch (Exception e) {
     wrapAndThrow(provider, e, value, i);
   }
 }
 private final void serializeUsingCustom(
     List<String> value, JsonGenerator jgen, SerializerProvider provider, int len)
     throws IOException, JsonGenerationException {
   int i = 0;
   try {
     final JsonSerializer<String> ser = _serializer;
     for (i = 0; i < len; ++i) {
       String str = value.get(i);
       if (str == null) {
         provider.defaultSerializeNull(jgen);
       } else {
         ser.serialize(str, jgen, provider);
       }
     }
   } catch (Exception e) {
     wrapAndThrow(provider, e, value, i);
   }
 }
  /**
   * General-purpose serialization for contents, where we do not necessarily know the value
   * serialization, but we do know that no value suppression is needed (which simplifies processing
   * a bit)
   */
  public void serializeFields(Map<?, ?> value, JsonGenerator gen, SerializerProvider provider)
      throws IOException {
    // If value type needs polymorphic type handling, some more work needed:
    if (_valueTypeSerializer != null) {
      serializeTypedFields(value, gen, provider, null);
      return;
    }
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final Set<String> ignored = _ignoredEntries;
    Object keyElem = null;

    try {
      for (Map.Entry<?, ?> entry : value.entrySet()) {
        Object valueElem = entry.getValue();
        // First, serialize key
        keyElem = entry.getKey();
        if (keyElem == null) {
          provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
        } else {
          // One twist: is entry ignorable? If so, skip
          if ((ignored != null) && ignored.contains(keyElem)) {
            continue;
          }
          keySerializer.serialize(keyElem, gen, provider);
        }
        // And then value
        if (valueElem == null) {
          provider.defaultSerializeNull(gen);
          continue;
        }
        JsonSerializer<Object> serializer = _valueSerializer;
        if (serializer == null) {
          serializer = _findSerializer(provider, valueElem);
        }
        serializer.serialize(valueElem, gen, provider);
      }
    } catch (Exception e) { // Add reference information
      wrapAndThrow(provider, e, value, String.valueOf(keyElem));
    }
  }
Example #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);
        }
      }
    }
  }