Search in sources :

Example 1 with SegmentedStringWriter

use of com.fasterxml.jackson.core.io.SegmentedStringWriter in project curiostack by curioswitch.

the class MessageMarshaller method writeValueAsString.

/**
 * 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());
    JsonGenerator gen = jsonFactory.createGenerator(sw);
    writeValue(message, gen);
    return sw.getAndClear();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SegmentedStringWriter(com.fasterxml.jackson.core.io.SegmentedStringWriter)

Example 2 with SegmentedStringWriter

use of com.fasterxml.jackson.core.io.SegmentedStringWriter in project jackson-core by FasterXML.

the class SegmentedStringWriterTest method testSimple.

public void testSimple() throws Exception {
    BufferRecycler br = new BufferRecycler();
    SegmentedStringWriter w = new SegmentedStringWriter(br);
    StringBuilder exp = new StringBuilder();
    for (int i = 0; exp.length() < 100; ++i) {
        String nr = String.valueOf(i);
        exp.append(' ').append(nr);
        w.append(' ');
        switch(i % 4) {
            case 0:
                w.append(nr);
                break;
            case 1:
                {
                    String str = "  " + nr;
                    w.append(str, 2, str.length());
                }
                break;
            case 2:
                w.write(nr.toCharArray());
                break;
            default:
                {
                    char[] ch = (" " + nr + " ").toCharArray();
                    w.write(ch, 1, nr.length());
                }
                break;
        }
    }
    // flush, close are nops but trigger just for fun
    w.flush();
    w.close();
    String act = w.getAndClear();
    assertEquals(exp.toString(), act);
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) SegmentedStringWriter(com.fasterxml.jackson.core.io.SegmentedStringWriter)

Example 3 with SegmentedStringWriter

use of com.fasterxml.jackson.core.io.SegmentedStringWriter in project rskj by rsksmart.

the class EtherObjectMapper method writeValueAsString.

@Override
public String writeValueAsString(Object value) throws JsonProcessingException {
    // alas, we have to pull the recycler directly here...
    SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
    try {
        JsonGenerator ge = _jsonFactory.createGenerator(sw);
        // set ethereum custom pretty printer
        EtherPrettyPrinter pp = new EtherPrettyPrinter();
        ge.setPrettyPrinter(pp);
        _configAndWriteValue(ge, value);
    } catch (JsonProcessingException e) {
        // to support [JACKSON-758]
        throw e;
    } catch (IOException e) {
        // shouldn't really happen, but is declared as possibility so:
        throw JsonMappingException.fromUnexpectedIOE(e);
    }
    return sw.getAndClear();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SegmentedStringWriter(com.fasterxml.jackson.core.io.SegmentedStringWriter)

Aggregations

SegmentedStringWriter (com.fasterxml.jackson.core.io.SegmentedStringWriter)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)1 IOException (java.io.IOException)1