org.codehaus.jackson.map.JsonSerializer.serialize()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(220)

本文整理了Java中org.codehaus.jackson.map.JsonSerializer.serialize()方法的一些代码示例,展示了JsonSerializer.serialize()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonSerializer.serialize()方法的具体详情如下:
包路径:org.codehaus.jackson.map.JsonSerializer
类名称:JsonSerializer
方法名:serialize

JsonSerializer.serialize介绍

[英]Method that can be called to ask implementation to serialize values of type this serializer handles.
[中]方法,该方法可被调用以要求实现序列化此序列化程序处理的类型的值。

代码示例

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

throws IOException, JsonProcessingException
serialize(value, jgen, provider);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

public void serializeContentsUsing(List<?> value, JsonGenerator jgen, SerializerProvider provider,
    JsonSerializer<Object> ser)
  throws IOException, JsonGenerationException
{
  final int len = value.size();
  if (len == 0) {
    return;
  }
  final TypeSerializer typeSer = _valueTypeSerializer;
  for (int i = 0; i < len; ++i) {
    Object elem = value.get(i);
    try {
      if (elem == null) {
        provider.defaultSerializeNull(jgen);
      } else if (typeSer == null) {
        ser.serialize(elem, jgen, provider);
      } else {
        ser.serializeWithType(elem, jgen, provider, typeSer);
      }
    } catch (Exception e) {
      // [JACKSON-55] Need to add reference information
      wrapAndThrow(provider, e, value, i);
    }
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

public void serializeContentsUsing(Collection<?> value, JsonGenerator jgen, SerializerProvider provider,
      JsonSerializer<Object> ser)
    throws IOException, JsonGenerationException
  {
    Iterator<?> it = value.iterator();
    if (it.hasNext()) {
      TypeSerializer typeSer = _valueTypeSerializer;
      int i = 0;
      do {
        Object elem = it.next();
        try {
          if (elem == null) {
            provider.defaultSerializeNull(jgen);
          } else {
            if (typeSer == null) {
              ser.serialize(elem, jgen, provider);
            } else {
              ser.serializeWithType(elem, jgen, provider, typeSer);
            }
          }
          ++i;
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          wrapAndThrow(provider, e, value, i);
        }
      } while (it.hasNext());
    }
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

/**
 * @since 1.7
 */
public final void defaultSerializeNull(JsonGenerator jgen)
  throws IOException, JsonProcessingException
{
  getNullValueSerializer().serialize(null, jgen, this);
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

currSerializer.serialize(elem, jgen, provider);
} else {
  currSerializer.serializeWithType(elem, jgen, provider, typeSer);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

private void serializeContentsSlow(String[] value, JsonGenerator jgen, SerializerProvider provider,
    JsonSerializer<Object> ser)
  throws IOException, JsonGenerationException
{
  for (int i = 0, len = value.length; i < len; ++i) {
    String str = value[i];
    if (str == null) {
      provider.defaultSerializeNull(jgen);
    } else {
      ser.serialize(value[i], jgen, provider);
    }
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

ser.serialize(elem, jgen, provider);
} else {
  ser.serializeWithType(elem, jgen, provider, typeSer);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

/**
 * Convenience method that will serialize given value (which can be
 * null) using standard serializer locating functionality. It can
 * be called for all values including field and Map values, but usually
 * field values are best handled calling
 * {@link #defaultSerializeField} instead.
 */
public final void defaultSerializeValue(Object value, JsonGenerator jgen)
  throws IOException, JsonProcessingException
{
  if (value == null) {
    getNullValueSerializer().serialize(null, jgen, this);
  } else {
    Class<?> cls = value.getClass();
    findTypedValueSerializer(cls, true, null).serialize(value, jgen, this);
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

currSerializer.serialize(elem, jgen, provider);
} else {
  currSerializer.serializeWithType(elem, jgen, provider, typeSer);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

@Override
  public void serializeContents(EnumSet<? extends Enum<?>> value, JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonGenerationException
  {
    JsonSerializer<Object> enumSer = _elementSerializer;
    /* Need to dynamically find instance serializer; unfortunately
     * that seems to be the only way to figure out type (no accessors
     * to the enum class that set knows)
     */
    for (Enum<?> en : value) {
      if (enumSer == null) {
        /* 12-Jan-2010, tatu: Since enums can not be polymorphic, let's
         *   not bother with typed serializer variant here
         */
        enumSer = provider.findValueSerializer(en.getDeclaringClass(), _property);
      }
      enumSer.serialize(en, jgen, provider);
    }
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

Object keyElem = entry.getKey();
if (keyElem == null) {
  provider.getNullKeySerializer().serialize(null, jgen, provider);
} else {
  keySerializer.serialize(keyElem, jgen, provider);
  try {
    if (typeSer == null) {
      ser.serialize(valueElem, jgen, provider);
    } else {
      ser.serializeWithType(valueElem, jgen, provider, typeSer);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

/**
 * Convenience method that will serialize given field with specified
 * value. Value may be null. Serializer is done using the usual
 * null) using standard serializer locating functionality.
 */
public final void defaultSerializeField(String fieldName, Object value, JsonGenerator jgen)
  throws IOException, JsonProcessingException
{
  jgen.writeFieldName(fieldName);
  if (value == null) {
    /* Note: can't easily check for suppression at this point
     * any more; caller must check it.
     */
    getNullValueSerializer().serialize(null, jgen, this);
  } else {
    Class<?> cls = value.getClass();
    findTypedValueSerializer(cls, true, null).serialize(value, jgen, this);
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

ser.serialize(value, jgen, prov);
} else {
  ser.serializeWithType(value, jgen, prov, _typeSerializer);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

private final void serializeUsingCustom(List<String> value, JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonGenerationException
  {
    int i = 0;
    try {
      final int len = value.size();
      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);
    }
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

ser.serialize(value, jgen, prov);
} else {
  ser.serializeWithType(value, jgen, prov, _typeSerializer);

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

private void serializeUsingCustom(Collection<String> value, JsonGenerator jgen, SerializerProvider provider)
  throws IOException, JsonGenerationException
{
  final JsonSerializer<String> ser = _serializer;
  int i = 0;
  for (String str : value) {
    try {
      if (str == null) {
        provider.defaultSerializeNull(jgen);
      } else {
        ser.serialize(str, jgen, provider);
      }
    } catch (Exception e) {
      wrapAndThrow(provider, e, value, i);
    }
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

@Override
@SuppressWarnings("deprecation")
public void serialize(JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonProcessingException
{
  // First, wrapping:
  if (_prefix != null) jgen.writeRaw(_prefix);
  if (_value == null) {
    provider.defaultSerializeNull(jgen);
  } else if (_serializationType != null) {
    provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, jgen, provider);
  } else {
    Class<?> cls = _value.getClass();
    provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider);
  }
  if (_suffix != null) jgen.writeRaw(_suffix);
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

ser.serialize(value, jgen, prov);
} catch (IOException ioe) {
  throw ioe;

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

@Override
@SuppressWarnings("deprecation")
public void serialize(JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonProcessingException
{
  // First, wrapping:
  jgen.writeRaw(_function);
  jgen.writeRaw('(');
  if (_value == null) {
    provider.defaultSerializeNull(jgen);
  } else if (_serializationType != null) {
    provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, jgen, provider);
  } else {
    Class<?> cls = _value.getClass();
    provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider);
  }
  jgen.writeRaw(')');
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

protected void serializeContentsUsing(EnumMap<? extends Enum<?>,?> value, JsonGenerator jgen, SerializerProvider provider,
    JsonSerializer<Object> valueSer)
  throws IOException, JsonGenerationException
{
  EnumValues keyEnums = _keyEnums;
  for (Map.Entry<? extends Enum<?>,?> entry : value.entrySet()) {
    Enum<?> key = entry.getKey();
    if (keyEnums == null) {
      // clumsy, but has to do for now:
      SerializerBase<?> ser = (SerializerBase<?>) provider.findValueSerializer(key.getDeclaringClass(),
          _property);
      keyEnums = ((EnumSerializer) ser).getEnumValues();
    }
    jgen.writeFieldName(keyEnums.serializedValueFor(key));
    Object valueElem = entry.getValue();
    if (valueElem == null) {
      provider.defaultSerializeNull(jgen);
    } else {
      try {
        valueSer.serialize(valueElem, jgen, provider);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, entry.getKey().name());
      }
    }
  }
}

相关文章