Exemplo n.º 1
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));
      }
    }
  }
  @Override
  protected void serializeFieldsFiltered(
      Object bean, JsonGenerator jgen0, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    // 19-Aug-2013, tatu: During 'convertValue()', need to skip
    if (!(jgen0 instanceof ToXmlGenerator)) {
      super.serializeFieldsFiltered(bean, jgen0, provider);
      return;
    }

    final ToXmlGenerator xgen = (ToXmlGenerator) jgen0;

    final BeanPropertyWriter[] props;
    if (_filteredProps != null && provider.getActiveView() != null) {
      props = _filteredProps;
    } else {
      props = _props;
    }
    final PropertyFilter filter = findPropertyFilter(provider, _propertyFilterId, bean);
    // better also allow missing filter actually..
    if (filter == null) {
      serializeFields(bean, jgen0, provider);
      return;
    }

    final int attrCount = _attributeCount;
    if (attrCount > 0) {
      xgen.setNextIsAttribute(true);
    }
    final int textIndex = _textPropertyIndex;
    final QName[] xmlNames = _xmlNames;

    int i = 0;
    try {
      for (final int len = props.length; i < len; ++i) {
        if (i == attrCount) {
          xgen.setNextIsAttribute(false);
        }
        // also: if this is property to write as text ("unwrap"), need to:
        if (i == textIndex) {
          xgen.setNextIsUnwrapped(true);
        }
        xgen.setNextName(xmlNames[i]);
        BeanPropertyWriter prop = props[i];
        if (prop != null) { // can have nulls in filtered list
          filter.serializeAsField(bean, xgen, provider, prop);
        }
      }
      if (_anyGetterWriter != null) {
        _anyGetterWriter.getAndSerialize(bean, xgen, provider);
      }
    } catch (Exception e) {
      String name = (i == props.length) ? "[anySetter]" : props[i].getName();
      wrapAndThrow(provider, e, bean, name);
    } catch (StackOverflowError e) {
      JsonMappingException mapE =
          new JsonMappingException("Infinite recursion (StackOverflowError)", e);
      String name = (i == props.length) ? "[anySetter]" : props[i].getName();
      mapE.prependPath(new JsonMappingException.Reference(bean, name));
      throw mapE;
    }
  }