Search in sources :

Example 81 with JsonFactory

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

the class AsyncStringObjectTest method testBasicFieldsNames.

public void testBasicFieldsNames() throws IOException {
    final String json = aposToQuotes(String.format("{'%s':'%s','%s':'%s','%s':'%s'}", UNICODE_SHORT_NAME, UNICODE_LONG_NAME, UNICODE_LONG_NAME, UNICODE_SHORT_NAME, ASCII_SHORT_NAME, ASCII_SHORT_NAME));
    final JsonFactory f = JSON_F;
    byte[] data = _jsonDoc(json);
    _testBasicFieldsNames(f, data, 0, 100);
    _testBasicFieldsNames(f, data, 0, 3);
    _testBasicFieldsNames(f, data, 0, 1);
    _testBasicFieldsNames(f, data, 1, 100);
    _testBasicFieldsNames(f, data, 1, 3);
    _testBasicFieldsNames(f, data, 1, 1);
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 82 with JsonFactory

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

the class TestGeneratorArray method testSimpleArrayWrite.

public void testSimpleArrayWrite() throws Exception {
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
    gen.writeStartArray();
    gen.writeNumber(13);
    gen.writeBoolean(true);
    gen.writeString("foobar");
    gen.writeEndArray();
    gen.close();
    String docStr = sw.toString();
    JsonParser jp = createParserUsingReader(docStr);
    assertEquals(JsonToken.START_ARRAY, jp.nextToken());
    assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertEquals(13, jp.getIntValue());
    assertEquals(JsonToken.VALUE_TRUE, jp.nextToken());
    assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
    assertEquals("foobar", jp.getText());
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    assertEquals(null, jp.nextToken());
    jp.close();
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 83 with JsonFactory

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

the class TestGeneratorCopy method testCopyObjectTokens.

public void testCopyObjectTokens() throws IOException {
    JsonFactory jf = new JsonFactory();
    final String DOC = "{ \"a\":1, \"b\":[{ \"c\" : null }] }";
    JsonParser jp = jf.createParser(ObjectReadContext.empty(), new StringReader(DOC));
    StringWriter sw = new StringWriter();
    JsonGenerator gen = jf.createGenerator(ObjectWriteContext.empty(), sw);
    assertToken(JsonToken.START_OBJECT, jp.nextToken());
    gen.copyCurrentStructure(jp);
    // which will advance parser to matching end Object
    assertToken(JsonToken.END_OBJECT, jp.currentToken());
    jp.close();
    gen.close();
    assertEquals("{\"a\":1,\"b\":[{\"c\":null}]}", sw.toString());
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 84 with JsonFactory

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

the class TestGeneratorCopy method testCopyArrayTokens.

public void testCopyArrayTokens() throws IOException {
    JsonFactory jf = new JsonFactory();
    final String DOC = "123 [ 1, null, [ false ] ]";
    JsonParser jp = jf.createParser(ObjectReadContext.empty(), new StringReader(DOC));
    StringWriter sw = new StringWriter();
    JsonGenerator gen = jf.createGenerator(ObjectWriteContext.empty(), sw);
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    gen.copyCurrentEvent(jp);
    // should not change parser state:
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.currentToken());
    assertEquals(123, jp.getIntValue());
    // And then let's copy the array
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    gen.copyCurrentStructure(jp);
    // which will advance parser to matching close Array
    assertToken(JsonToken.END_ARRAY, jp.currentToken());
    jp.close();
    gen.close();
    assertEquals("123 [1,null,[false]]", sw.toString());
}
Also used : JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Example 85 with JsonFactory

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

the class TestGeneratorCopy method testCopyRootTokens.

public void testCopyRootTokens() throws IOException {
    JsonFactory jf = new JsonFactory();
    final String DOC = "\"text\\non two lines\" true false 2.0";
    JsonParser jp = jf.createParser(ObjectReadContext.empty(), new StringReader(DOC));
    StringWriter sw = new StringWriter();
    JsonGenerator gen = jf.createGenerator(ObjectWriteContext.empty(), sw);
    JsonToken t;
    while ((t = jp.nextToken()) != null) {
        gen.copyCurrentEvent(jp);
        // should not change parser state:
        assertToken(t, jp.currentToken());
    }
    jp.close();
    gen.close();
    assertEquals("\"text\\non two lines\" true false 2.0", sw.toString());
}
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