下面列出了怎么用com.fasterxml.jackson.core.io.SegmentedStringWriter的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
@Override
public <E> String serialize(E object) {
if (object == null)
return null;
SegmentedStringWriter source = new SegmentedStringWriter(buffer.get());
try (SerializerWrapper serializer = createSerializer(source)) {
mapperFor((Class<E>) object.getClass()).serialize(this, serializer, object);
} catch (Exception e) {
e.printStackTrace();
throw new KriptonRuntimeException(e);
}
return source.getAndClear();
}
/**
* Converts a {@link Message} into a JSON {@link String}.
*
* @throws InvalidProtocolBufferException if there are unknown Any types in the message.
*/
public <T extends Message> String writeValueAsString(T message) throws IOException {
checkNotNull(message, "message");
SegmentedStringWriter sw = new SegmentedStringWriter(jsonFactory._getBufferRecycler());
try (JsonGenerator gen = jsonFactory.createGenerator(sw)) {
writeValue(message, gen);
}
return sw.getAndClear();
}
@Override
public <E> String serializeCollection(Collection<E> collection, Class<E> objectClazz) {
if (collection == null)
return null;
SegmentedStringWriter source = new SegmentedStringWriter(buffer.get());
try (SerializerWrapper serializer = createSerializer(source)) {
mapperFor(objectClazz).serializeCollection(this, serializer, collection);
} catch (Exception e) {
e.printStackTrace();
throw new KriptonRuntimeException(e);
}
return source.getAndClear();
}
private String serializeToString(T value, ObjectMapper mapper, SerializerProvider provider) throws IOException {
try (SegmentedStringWriter sw = new SegmentedStringWriter(new BufferRecycler())) {
if (trySerializeToWriter(value, mapper, provider, sw)) {
return sw.getAndClear();
}
}
// fallback on old behavior
JsonNode tree = mapper.valueToTree(value);
if (tree.isNull()) {
return tree.asText();
} else {
return mapper.writeValueAsString(tree);
}
}
protected JSONComposer(int features, JsonGenerator gen, SegmentedStringWriter w)
{
super(gen);
_features = features;
_stringWriter = w;
_byteWriter = null;
_closeGenerator = true;
}
public static JSONComposer<String> stringComposer(int features,
JsonGenerator gen, SegmentedStringWriter w) {
return new JSONComposer<String>(features, gen, w);
}
public JSONComposer<String> composeString() throws IOException, JSONObjectException {
SegmentedStringWriter out = new SegmentedStringWriter(_streamFactory._getBufferRecycler());
JsonGenerator gen = _config(_streamFactory.createGenerator(this, out));
return JSONComposer.stringComposer(_features, gen, out);
}