예제 #1
0
  @Override
  public boolean isEmpty(SerializerProvider prov, Map<?, ?> value) {
    if (value.isEmpty()) {
      return true;
    }

    // 05-Nove-2015, tatu: Simple cases are cheap, but for recursive
    //   emptiness checking we actually need to see if values are empty as well.
    Object supp = _suppressableValue;
    if ((supp == null) && !_suppressNulls) {
      return false;
    }
    JsonSerializer<Object> valueSer = _valueSerializer;
    final boolean checkEmpty = (MARKER_FOR_EMPTY == supp);
    if (valueSer != null) {
      for (Object elemValue : value.values()) {
        if (elemValue == null) {
          if (_suppressNulls) {
            continue;
          }
          return false;
        }
        if (checkEmpty) {
          if (!valueSer.isEmpty(prov, elemValue)) {
            return false;
          }
        } else if ((supp == null) || !supp.equals(value)) {
          return false;
        }
      }
      return true;
    }
    // But if not statically known, try this:
    for (Object elemValue : value.values()) {
      if (elemValue == null) {
        if (_suppressNulls) {
          continue;
        }
        return false;
      }
      try {
        valueSer = _findSerializer(prov, elemValue);
      } catch (JsonMappingException e) { // Ugh... can not just throw as-is, so...
        // 05-Nov-2015, tatu: For now, probably best not to assume empty then
        return false;
      }
      if (checkEmpty) {
        if (!valueSer.isEmpty(prov, elemValue)) {
          return false;
        }
      } else if ((supp == null) || !supp.equals(value)) {
        return false;
      }
    }
    return true;
  }
예제 #2
0
  protected void _writeNullKeyedEntry(JsonGenerator gen, SerializerProvider provider, Object value)
      throws IOException {
    JsonSerializer<Object> keySerializer = provider.findNullKeySerializer(_keyType, _property);
    JsonSerializer<Object> valueSer;
    if (value == null) {
      if (_suppressNulls) {
        return;
      }
      valueSer = provider.getDefaultNullValueSerializer();
    } else {
      valueSer = _valueSerializer;
      if (valueSer == null) {
        valueSer = _findSerializer(provider, value);
      }
      if (_suppressableValue == MARKER_FOR_EMPTY) {
        if (valueSer.isEmpty(provider, value)) {
          return;
        }
      } else if ((_suppressableValue != null) && (_suppressableValue.equals(value))) {
        return;
      }
    }

    try {
      keySerializer.serialize(null, gen, provider);
      valueSer.serialize(value, gen, provider);
    } catch (Exception e) {
      wrapAndThrow(provider, e, value, "");
    }
  }
예제 #3
0
  /**
   * Helper method used when we have a JSON Filter to use for potentially filtering out Map entries.
   *
   * @since 2.5
   */
  public void serializeFilteredFields(
      Map<?, ?> value,
      JsonGenerator gen,
      SerializerProvider provider,
      PropertyFilter filter,
      Object suppressableValue) // since 2.5
      throws IOException {
    final Set<String> ignored = _ignoredEntries;
    final MapProperty prop = new MapProperty(_valueTypeSerializer, _property);
    final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      // First, serialize key; unless ignorable by key
      final Object keyElem = entry.getKey();
      if (ignored != null && ignored.contains(keyElem)) continue;

      JsonSerializer<Object> keySerializer;
      if (keyElem == null) {
        keySerializer = provider.findNullKeySerializer(_keyType, _property);
      } else {
        keySerializer = _keySerializer;
      }
      // or by value; nulls often suppressed
      final Object valueElem = entry.getValue();

      JsonSerializer<Object> valueSer;
      // And then value
      if (valueElem == null) {
        if (_suppressNulls) {
          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;
          }
        }
      }
      // and with that, ask filter to handle it
      prop.reset(keyElem, keySerializer, valueSer);
      try {
        filter.serializeAsField(valueElem, gen, provider, prop);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, String.valueOf(keyElem));
      }
    }
  }
예제 #4
0
  /** Serialization method called when exclusion filtering needs to be applied. */
  public void serializeOptionalFields(
      Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, Object suppressableValue)
      throws IOException {
    // If value type needs polymorphic type handling, some more work needed:
    if (_valueTypeSerializer != null) {
      serializeTypedFields(value, gen, provider, suppressableValue);
      return;
    }
    final Set<String> ignored = _ignoredEntries;
    final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      // First find key serializer
      final Object keyElem = entry.getKey();
      JsonSerializer<Object> keySerializer;
      if (keyElem == null) {
        keySerializer = provider.findNullKeySerializer(_keyType, _property);
      } else {
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer = _keySerializer;
      }

      // Then value serializer
      final Object valueElem = entry.getValue();
      JsonSerializer<Object> valueSer;
      if (valueElem == null) {
        if (_suppressNulls) { // all suppressions 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;
          }
        }
      }
      // and then serialize, if all went well
      try {
        keySerializer.serialize(keyElem, gen, provider);
        valueSer.serialize(valueElem, gen, provider);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, String.valueOf(keyElem));
      }
    }
  }
예제 #5
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));
      }
    }
  }