Search in sources :

Example 26 with JsonFactory

use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.

the class TestParserClosing method testNoAutoCloseReader.

/**
 * This unit test checks the default behaviour; with no auto-close, no
 * automatic closing should occur, nor explicit one unless specific
 * forcing method is used.
 */
public void testNoAutoCloseReader() throws Exception {
    final String DOC = "[ 1 ]";
    JsonFactory f = new JsonFactory();
    // Check the default settings
    assertTrue(f.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
    // then change
    f = f.rebuild().disable(JsonParser.Feature.AUTO_CLOSE_SOURCE).build();
    assertFalse(f.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
    @SuppressWarnings("resource") MyReader input = new MyReader(DOC);
    JsonParser jp = f.createParser(ObjectReadContext.empty(), input);
    // shouldn't be closed to begin with...
    assertFalse(input.isClosed());
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertToken(JsonToken.END_ARRAY, jp.nextToken());
    assertNull(jp.nextToken());
    // normally would be closed now
    assertFalse(input.isClosed());
    // regular close won't close it either:
    jp.close();
    assertFalse(input.isClosed());
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 27 with JsonFactory

use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.

the class TestParserClosing method testReleaseContentBytes.

// [JACKSON-287]
public void testReleaseContentBytes() throws Exception {
    byte[] input = "[1]foobar".getBytes("UTF-8");
    JsonParser jp = new JsonFactory().createParser(ObjectReadContext.empty(), input);
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertToken(JsonToken.END_ARRAY, jp.nextToken());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // theoretically could have only read subset; but current impl is more greedy
    assertEquals(6, jp.releaseBuffered(out));
    assertArrayEquals("foobar".getBytes("UTF-8"), out.toByteArray());
    jp.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 28 with JsonFactory

use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.

the class TestParserClosing method testReleaseContentChars.

public void testReleaseContentChars() throws Exception {
    JsonParser jp = new JsonFactory().createParser(ObjectReadContext.empty(), "[true]xyz");
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.VALUE_TRUE, jp.nextToken());
    assertToken(JsonToken.END_ARRAY, jp.nextToken());
    StringWriter sw = new StringWriter();
    // theoretically could have only read subset; but current impl is more greedy
    assertEquals(3, jp.releaseBuffered(sw));
    assertEquals("xyz", sw.toString());
    jp.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 29 with JsonFactory

use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.

the class TestPrettyPrinter method testSimpleDocWithDefault.

public void testSimpleDocWithDefault() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    gen.useDefaultPrettyPrinter();
    _verifyPrettyPrinter(gen, sw);
    gen.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 30 with JsonFactory

use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.

the class TestPrettyPrinter method testCustomSeparatorsWithPPWithoutSpaces.

public void testCustomSeparatorsWithPPWithoutSpaces() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    gen.setPrettyPrinter(new DefaultPrettyPrinter().withSeparators(Separators.createDefaultInstance().withObjectFieldValueSeparator('=').withObjectEntrySeparator(';').withArrayValueSeparator('|')).withoutSpacesInObjectEntries());
    _writeTestDocument(gen);
    gen.close();
    assertEquals("[ 3| \"abc\"| [ true ]| {" + DefaultIndenter.SYS_LF + "  \"f\"=null;" + DefaultIndenter.SYS_LF + "  \"f2\"=null" + DefaultIndenter.SYS_LF + "} ]", sw.toString());
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Aggregations

JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)137 AsyncReaderWrapper (com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)5 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 SerializedString (com.fasterxml.jackson.core.io.SerializedString)2 NonBlockingJsonParserBase (com.fasterxml.jackson.core.json.async.NonBlockingJsonParserBase)2 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)2 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigDecimal (java.math.BigDecimal)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 BigInteger (java.math.BigInteger)1 HashSet (java.util.HashSet)1