Search in sources :

Example 11 with SerializedString

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

the class NextXxxAccessTest method _testIsNextTokenName1.

/*
    /********************************************************
    /* Actual test code
    /********************************************************
     */
private void _testIsNextTokenName1(int mode) throws Exception {
    final String DOC = "{\"name\":123,\"name2\":14,\"x\":\"name\"}";
    JsonParser p = createParser(mode, DOC);
    final SerializedString NAME = new SerializedString("name");
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.START_OBJECT, p.currentToken());
    assertEquals(JsonTokenId.ID_START_OBJECT, p.currentTokenId());
    assertTrue(p.nextFieldName(NAME));
    assertToken(JsonToken.FIELD_NAME, p.currentToken());
    assertEquals(NAME.getValue(), p.getCurrentName());
    assertEquals(NAME.getValue(), p.getText());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.currentToken());
    assertEquals(123, p.getIntValue());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.FIELD_NAME, p.currentToken());
    assertEquals("name2", p.getCurrentName());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    // do NOT check number value, to enforce skipping
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.FIELD_NAME, p.currentToken());
    assertEquals("x", p.getCurrentName());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.VALUE_STRING, p.currentToken());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.END_OBJECT, p.currentToken());
    if (mode != MODE_DATA_INPUT) {
        assertFalse(p.nextFieldName(NAME));
        assertNull(p.currentToken());
    }
    p.close();
    // Actually, try again with slightly different sequence...
    p = createParser(mode, DOC);
    assertToken(JsonToken.START_OBJECT, p.nextToken());
    assertFalse(p.nextFieldName(new SerializedString("Nam")));
    assertToken(JsonToken.FIELD_NAME, p.currentToken());
    assertEquals(NAME.getValue(), p.getCurrentName());
    assertEquals(NAME.getValue(), p.getText());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.currentToken());
    assertEquals(123, p.getIntValue());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.FIELD_NAME, p.currentToken());
    assertEquals("name2", p.getCurrentName());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.FIELD_NAME, p.currentToken());
    assertEquals("x", p.getCurrentName());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.VALUE_STRING, p.currentToken());
    assertFalse(p.nextFieldName(NAME));
    assertToken(JsonToken.END_OBJECT, p.currentToken());
    if (mode != MODE_DATA_INPUT) {
        assertFalse(p.nextFieldName(NAME));
        assertNull(p.currentToken());
    }
    p.close();
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializedString(com.fasterxml.jackson.core.io.SerializedString)

Example 12 with SerializedString

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

the class NextXxxAccessTest method _testIssue34.

private void _testIssue34(int mode) throws Exception {
    final int TESTROUNDS = 223;
    final String DOC_PART = "{ \"fieldName\": 1 }";
    // build the big document to trigger issue
    StringBuilder sb = new StringBuilder(2000);
    for (int i = 0; i < TESTROUNDS; ++i) {
        sb.append(DOC_PART);
    }
    final String DOC = sb.toString();
    SerializableString fieldName = new SerializedString("fieldName");
    JsonParser parser = createParser(mode, DOC);
    for (int i = 0; i < TESTROUNDS - 1; i++) {
        assertEquals(JsonToken.START_OBJECT, parser.nextToken());
        // These will succeed
        assertTrue(parser.nextFieldName(fieldName));
        parser.nextLongValue(-1);
        assertEquals(JsonToken.END_OBJECT, parser.nextToken());
    }
    assertEquals(JsonToken.START_OBJECT, parser.nextToken());
    // This will fail
    assertTrue(parser.nextFieldName(fieldName));
    parser.close();
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializedString(com.fasterxml.jackson.core.io.SerializedString)

Example 13 with SerializedString

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

the class NextXxxAccessTest method _testNextFieldNameIndent.

private void _testNextFieldNameIndent(int mode) throws Exception {
    final String DOC = "{\n  \"name\" : \n  [\n  ]\n   }";
    JsonParser p = createParser(mode, DOC);
    assertToken(JsonToken.START_OBJECT, p.nextToken());
    assertTrue(p.nextFieldName(new SerializedString("name")));
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertToken(JsonToken.END_OBJECT, p.nextToken());
    if (mode != MODE_DATA_INPUT) {
        assertNull(p.nextToken());
    }
    p.close();
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializedString(com.fasterxml.jackson.core.io.SerializedString)

Example 14 with SerializedString

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

the class UTF8NamesParseTest method _testNextFieldName.

private void _testNextFieldName(int mode, byte[] doc) throws IOException {
    SerializedString id = new SerializedString("id");
    JsonParser parser = createParser(mode, doc);
    assertEquals(parser.nextToken(), JsonToken.START_OBJECT);
    assertTrue(parser.nextFieldName(id));
    assertEquals(parser.nextToken(), JsonToken.VALUE_NUMBER_INT);
    assertEquals(parser.nextToken(), JsonToken.END_OBJECT);
    parser.close();
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString)

Example 15 with SerializedString

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

the class TestSerializedString method testAppending.

public void testAppending() throws IOException {
    final String INPUT = "\"quo\\ted\"";
    final String QUOTED = "\\\"quo\\\\ted\\\"";
    SerializableString sstr = new SerializedString(INPUT);
    // sanity checks first:
    assertEquals(sstr.getValue(), INPUT);
    assertEquals(QUOTED, new String(sstr.asQuotedChars()));
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    assertEquals(QUOTED.length(), sstr.writeQuotedUTF8(bytes));
    assertEquals(QUOTED, bytes.toString("UTF-8"));
    bytes.reset();
    assertEquals(INPUT.length(), sstr.writeUnquotedUTF8(bytes));
    assertEquals(INPUT, bytes.toString("UTF-8"));
    byte[] buffer = new byte[100];
    assertEquals(QUOTED.length(), sstr.appendQuotedUTF8(buffer, 3));
    assertEquals(QUOTED, new String(buffer, 3, QUOTED.length()));
    Arrays.fill(buffer, (byte) 0);
    assertEquals(INPUT.length(), sstr.appendUnquotedUTF8(buffer, 5));
    assertEquals(INPUT, new String(buffer, 5, INPUT.length()));
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString)

Aggregations

SerializedString (com.fasterxml.jackson.core.io.SerializedString)18 SerializableString (com.fasterxml.jackson.core.SerializableString)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 RawValue (com.fasterxml.jackson.databind.util.RawValue)1 StringWriter (java.io.StringWriter)1 BigDecimal (java.math.BigDecimal)1 ByteBuffer (java.nio.ByteBuffer)1 Random (java.util.Random)1