Search in sources :

Example 21 with JsonFactory

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

the class AsyncSimpleObjectTest method testNumbers.

public void testNumbers() throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(100);
    JsonFactory f = JSON_F;
    JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), bytes);
    g.writeStartObject();
    g.writeNumberField("i1", NUMBER_EXP_I);
    g.writeNumberField("doubley", NUMBER_EXP_D);
    g.writeFieldName("biggieDecimal");
    g.writeNumber(NUMBER_EXP_BD.toString());
    g.writeEndObject();
    g.close();
    byte[] data = bytes.toByteArray();
    // first, no offsets
    _testNumbers(f, data, 0, 100);
    _testNumbers(f, data, 0, 5);
    _testNumbers(f, data, 0, 3);
    _testNumbers(f, data, 0, 2);
    _testNumbers(f, data, 0, 1);
    // then with some
    _testNumbers(f, data, 1, 100);
    _testNumbers(f, data, 1, 3);
    _testNumbers(f, data, 1, 1);
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with JsonFactory

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

the class TestGeneratorArray method testEmptyArrayWrite.

public void testEmptyArrayWrite() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    TokenStreamContext ctxt = gen.getOutputContext();
    assertTrue(ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(0, ctxt.getEntryCount());
    assertEquals(0, ctxt.getCurrentIndex());
    gen.writeStartArray();
    ctxt = gen.getOutputContext();
    assertFalse(ctxt.inRoot());
    assertTrue(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(0, ctxt.getEntryCount());
    assertEquals(0, ctxt.getCurrentIndex());
    gen.writeEndArray();
    ctxt = gen.getOutputContext();
    assertTrue("Should be in root, was " + ctxt.typeDesc(), ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(1, ctxt.getEntryCount());
    // Index won't yet move
    assertEquals(0, ctxt.getCurrentIndex());
    gen.close();
    String docStr = sw.toString();
    JsonParser jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    jp.close();
    // Ok, then array with nested empty array
    sw = new StringWriter();
    gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    gen.writeStartArray();
    gen.writeStartArray();
    gen.writeEndArray();
    gen.writeEndArray();
    gen.close();
    docStr = sw.toString();
    jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    assertEquals(null, jp.nextToken());
    jp.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 23 with JsonFactory

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

the class TestGeneratorArray method testInvalidArrayWrite.

public void testInvalidArrayWrite() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    gen.writeStartArray();
    // Mismatch:
    try {
        gen.writeEndObject();
        fail("Expected an exception for mismatched array/object write");
    } catch (JsonGenerationException e) {
        verifyException(e, "Current context not Object");
    }
    gen.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 24 with JsonFactory

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

the class TestGeneratorObject method testSimpleObjectWrite.

public void testSimpleObjectWrite() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    gen.writeStartObject();
    gen.writeFieldName("first");
    gen.writeNumber(-901);
    gen.writeFieldName("sec");
    gen.writeBoolean(false);
    // JSON field names are just strings, not ids with restrictions
    gen.writeFieldName("3rd!");
    gen.writeString("yee-haw");
    gen.writeEndObject();
    gen.close();
    String docStr = sw.toString();
    JsonParser jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_OBJECT, jp.nextToken());
    assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
    assertEquals("first", jp.getText());
    assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertEquals(-901, jp.getIntValue());
    assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
    assertEquals("sec", jp.getText());
    assertEquals(JsonToken.VALUE_FALSE, jp.nextToken());
    assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
    assertEquals("3rd!", jp.getText());
    assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
    assertEquals("yee-haw", jp.getText());
    assertEquals(JsonToken.END_OBJECT, jp.nextToken());
    assertEquals(null, jp.nextToken());
    jp.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 25 with JsonFactory

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

the class TestGeneratorObject method testEmptyObjectWrite.

public void testEmptyObjectWrite() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    TokenStreamContext ctxt = gen.getOutputContext();
    assertTrue(ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(0, ctxt.getEntryCount());
    assertEquals(0, ctxt.getCurrentIndex());
    gen.writeStartObject();
    ctxt = gen.getOutputContext();
    assertFalse(ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertTrue(ctxt.inObject());
    assertEquals(0, ctxt.getEntryCount());
    assertEquals(0, ctxt.getCurrentIndex());
    gen.writeEndObject();
    ctxt = gen.getOutputContext();
    assertTrue(ctxt.inRoot());
    assertFalse(ctxt.inArray());
    assertFalse(ctxt.inObject());
    assertEquals(1, ctxt.getEntryCount());
    // Index won't yet move
    assertEquals(0, ctxt.getCurrentIndex());
    gen.close();
    String docStr = sw.toString();
    JsonParser jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_OBJECT, jp.nextToken());
    assertEquals(JsonToken.END_OBJECT, jp.nextToken());
    assertEquals(null, jp.nextToken());
    jp.close();
}
Also used : 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