/**
   * Main serialization method needs to be overridden to allow XML-specific extra handling, such as
   * indication of whether to write attributes or elements.
   */
  @Override
  protected void serializeFields(Object bean, JsonGenerator jgen0, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    // 19-Aug-2013, tatu: During 'convertValue()', need to skip
    if (!(jgen0 instanceof ToXmlGenerator)) {
      super.serializeFields(bean, jgen0, provider);
      return;
    }

    final ToXmlGenerator xgen = (ToXmlGenerator) jgen0;
    final BeanPropertyWriter[] props;
    if (_filteredProps != null && provider.getActiveView() != null) {
      props = _filteredProps;
    } else {
      props = _props;
    }

    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
          prop.serializeAsField(bean, xgen, provider);
        }
        // Reset to avoid next value being written as unwrapped,
        // for example when property is suppressed
        if (i == textIndex) {
          xgen.setNextIsUnwrapped(false);
        }
      }
      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) { // Bit tricky, can't do more calls as stack is full; so:
      JsonMappingException mapE =
          new JsonMappingException("Infinite recursion (StackOverflowError)");
      String name = (i == props.length) ? "[anySetter]" : props[i].getName();
      mapE.prependPath(new JsonMappingException.Reference(bean, name));
      throw mapE;
    }
  }
 @Override
 protected void _serializeObjectId(
     Object bean,
     JsonGenerator jgen,
     SerializerProvider provider,
     TypeSerializer typeSer,
     WritableObjectId objectId)
     throws IOException, JsonProcessingException, JsonGenerationException {
   /* Ok: let's serialize type id as attribute, but if (and only if!)
    * we are using AS_PROPERTY
    */
   if (typeSer.getTypeInclusion() == JsonTypeInfo.As.PROPERTY) {
     ToXmlGenerator xgen = (ToXmlGenerator) jgen;
     xgen.setNextIsAttribute(true);
     super._serializeObjectId(bean, jgen, provider, typeSer, objectId);
     if (_attributeCount == 0) { // if no attributes, need to reset
       xgen.setNextIsAttribute(false);
     }
   } else {
     super._serializeObjectId(bean, jgen, provider, typeSer, objectId);
   }
 }
  @Override
  public void serializeWithType(
      Object bean, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)
      throws IOException, JsonGenerationException {
    if (_objectIdWriter != null) {
      _serializeWithObjectId(bean, jgen, provider, typeSer);
      return;
    }

    /* Ok: let's serialize type id as attribute, but if (and only if!)
     * we are using AS_PROPERTY
     */
    if (typeSer.getTypeInclusion() == JsonTypeInfo.As.PROPERTY) {
      ToXmlGenerator xgen = (ToXmlGenerator) jgen;
      xgen.setNextIsAttribute(true);
      super.serializeWithType(bean, jgen, provider, typeSer);
      if (_attributeCount == 0) { // if no attributes, need to reset
        xgen.setNextIsAttribute(false);
      }
    } else {
      super.serializeWithType(bean, jgen, provider, typeSer);
    }
  }