Search in sources :

Example 26 with SerializedString

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

the class NextXxxParsingTest method testNextFieldAndText.

public void testNextFieldAndText() throws Exception {
    ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_STRINGS);
    final ObjectWriter w = MAPPER.writerFor(Strings.class).with(schema);
    Strings input = new Strings("Dogs", "like", "Baco\u00F1");
    byte[] bytes = w.writeValueAsBytes(input);
    JsonParser p = MAPPER.reader().with(schema).createParser(bytes);
    assertFalse(p.nextName(new SerializedString("values")));
    assertToken(JsonToken.START_OBJECT, p.currentToken());
    assertTrue(p.nextName(new SerializedString("values")));
    assertEquals("values", p.currentName());
    // 23-May-2016, tatu: Not working properly yet:
    // assertEquals("{values}", p.getParsingContext().toString());
    assertNull(p.nextName());
    assertToken(JsonToken.START_ARRAY, p.currentToken());
    assertEquals(input.values[0], p.nextTextValue());
    assertEquals(input.values[0], p.getText());
    assertEquals(-1, p.nextIntValue(-1));
    assertToken(JsonToken.VALUE_STRING, p.currentToken());
    assertEquals(input.values[1], p.getText());
    assertEquals(input.values[2], p.nextTextValue());
    assertEquals(input.values[2], p.getText());
    assertNull(p.nextTextValue());
    assertToken(JsonToken.END_ARRAY, p.currentToken());
    assertToken(JsonToken.END_OBJECT, p.nextToken());
    p.close();
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) ProtobufSchema(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 27 with SerializedString

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

the class POJOSimpleReadTest method _testSimplePojoViaParser.

private void _testSimplePojoViaParser(Employee empl, byte[] avro, boolean smallReads) throws Exception {
    // System.out.println("Bytes -> "+avro.length);
    InputStream in = new ByteArrayInputStream(avro);
    JsonParser p = MAPPER.reader().with(getEmployeeSchema()).createParser(smallReads ? ThrottledInputStream.wrap(in, 9) : in);
    assertToken(JsonToken.START_OBJECT, p.nextToken());
    if (!smallReads) {
        assertSame(in, p.streamReadInputSource());
    }
    assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
    assertFalse(p.hasTextCharacters());
    assertEquals("name", p.currentName());
    assertEquals("name", p.getText());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(empl.name, p.getText());
    StringWriter sw = new StringWriter();
    assertEquals(empl.name.length(), p.getText(sw));
    assertEquals(empl.name, sw.toString());
    assertTrue(p.hasTextCharacters());
    assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
    assertEquals("age", p.currentName());
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(NumberType.INT, p.getNumberType());
    assertEquals(Integer.valueOf(empl.age), p.getNumberValue());
    assertEquals(empl.age, p.getIntValue());
    assertFalse(p.isNaN());
    assertEquals((double) empl.age, p.getDoubleValue());
    assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
    assertEquals("emails", p.currentName());
    sw = new StringWriter();
    assertEquals(6, p.getText(sw));
    assertEquals("emails", sw.toString());
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(empl.emails[0], p.getText());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(empl.emails[1], p.getText());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
    assertEquals("boss", p.currentName());
    assertToken(JsonToken.START_OBJECT, p.nextToken());
    assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
    assertEquals("name", p.currentName());
    // and then skip various bits and pieces
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertTrue(p.nextName(new SerializedString("age")));
    assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    String n = p.nextName();
    assertEquals("emails", n);
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertNull(p.nextName());
    assertToken(JsonToken.VALUE_STRING, p.currentToken());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    assertFalse(p.nextName(new SerializedString("bossy")));
    assertToken(JsonToken.PROPERTY_NAME, p.currentToken());
    assertEquals("boss", p.currentName());
    assertToken(JsonToken.VALUE_NULL, p.nextToken());
    assertToken(JsonToken.END_OBJECT, p.nextToken());
    assertToken(JsonToken.END_OBJECT, p.nextToken());
    // should have consumed it all, but let's verify
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    assertEquals(0, p.releaseBuffered(b));
    b.close();
    // also...
    sw = new StringWriter();
    assertEquals(-1, p.releaseBuffered(sw));
    p.close();
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ThrottledInputStream(com.fasterxml.jackson.dataformat.avro.testsupport.ThrottledInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SerializedString(com.fasterxml.jackson.core.io.SerializedString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 28 with SerializedString

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

the class TestGenerator method _verifyName.

private void _verifyName(JsonParser p, int mode, String exp) throws Exception {
    switch(mode) {
        case 0:
            assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
            break;
        case 1:
            String name = p.nextName();
            assertEquals(exp, name);
            break;
        case 2:
            assertFalse(p.nextName(new SerializedString(exp + "1")));
            break;
        default:
            assertTrue(p.nextName(new SerializedString(exp)));
    }
    assertToken(JsonToken.PROPERTY_NAME, p.currentToken());
    assertEquals(exp, p.currentName());
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializedString(com.fasterxml.jackson.core.io.SerializedString)

Example 29 with SerializedString

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

the class SequenceWriterTest method testSimpleNonArray.

public void testSimpleNonArray() throws Exception {
    StringWriter strw = new StringWriter();
    SequenceWriter w = WRITER.forType(Bean.class).writeValues(strw);
    w.write(new Bean(13)).write(new Bean(-6)).writeAll(new Bean[] { new Bean(3), new Bean(1) }).writeAll(Arrays.asList(new Bean(5), new Bean(7)));
    w.close();
    assertEquals(aposToQuotes("{'a':13}\n{'a':-6}\n{'a':3}\n{'a':1}\n{'a':5}\n{'a':7}"), strw.toString());
    strw = new StringWriter();
    JsonGenerator gen = WRITER.getFactory().createGenerator(strw);
    w = WRITER.withRootValueSeparator(new SerializedString("/")).writeValues(gen);
    w.write(new Bean(1)).write(new Bean(2));
    w.close();
    gen.close();
    assertEquals(aposToQuotes("{'a':1}/{'a':2}"), strw.toString());
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 30 with SerializedString

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

the class ObjectIdWriter method construct.

/**
     * Factory method called by {@link com.fasterxml.jackson.databind.ser.std.BeanSerializerBase}
     * with the initial information based on standard settings for the type
     * for which serializer is being built.
     * 
     * @since 2.3
     */
public static ObjectIdWriter construct(JavaType idType, PropertyName propName, ObjectIdGenerator<?> generator, boolean alwaysAsId) {
    String simpleName = (propName == null) ? null : propName.getSimpleName();
    SerializableString serName = (simpleName == null) ? null : new SerializedString(simpleName);
    return new ObjectIdWriter(idType, serName, generator, null, alwaysAsId);
}
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)57 JsonParser (com.fasterxml.jackson.core.JsonParser)6 Test (org.junit.Test)5 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 SerializableString (com.fasterxml.jackson.core.SerializableString)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 StringWriter (java.io.StringWriter)3 Random (java.util.Random)3 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)2 JsonpCharacterEscapes (com.fasterxml.jackson.core.util.JsonpCharacterEscapes)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ProtobufSchema (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 IndexTemplate (org.apache.skywalking.library.elasticsearch.response.IndexTemplate)2 IndexTemplates (org.apache.skywalking.library.elasticsearch.response.IndexTemplates)2 Test (org.junit.jupiter.api.Test)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 CharacterEscapes (com.fasterxml.jackson.core.io.CharacterEscapes)1