Java Code Examples for com.fasterxml.jackson.core.io.SegmentedStringWriter

The following examples show how to use com.fasterxml.jackson.core.io.SegmentedStringWriter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: kripton   Source File: AbstractContext.java    License: Apache License 2.0 6 votes vote down vote up
@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();

}
 
Example 2
Source Project: curiostack   Source File: MessageMarshaller.java    License: MIT License 5 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source Project: kripton   Source File: AbstractContext.java    License: Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 4
Source Project: Rosetta   Source File: ContextualStoredAsJsonSerializer.java    License: Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 5
Source Project: jackson-jr   Source File: JSONComposer.java    License: Apache License 2.0 5 votes vote down vote up
protected JSONComposer(int features, JsonGenerator gen, SegmentedStringWriter w)
{
    super(gen);
    _features = features;
    _stringWriter = w;
    _byteWriter = null;
    _closeGenerator = true;
}
 
Example 6
Source Project: jackson-jr   Source File: JSONComposer.java    License: Apache License 2.0 4 votes vote down vote up
public static JSONComposer<String> stringComposer(int features,
        JsonGenerator gen, SegmentedStringWriter w) {
    return new JSONComposer<String>(features, gen, w);
}
 
Example 7
Source Project: jackson-jr   Source File: JSON.java    License: Apache License 2.0 4 votes vote down vote up
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);
}