Search in sources :

Example 6 with ObjectBuffer

use of com.fasterxml.jackson.databind.util.ObjectBuffer in project jackson-databind by FasterXML.

the class StringArrayDeserializer method deserialize.

@Override
public String[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    // Ok: must point to START_ARRAY (or equivalent)
    if (!p.isExpectedStartArrayToken()) {
        return handleNonArray(p, ctxt);
    }
    if (_elementDeserializer != null) {
        return _deserializeCustom(p, ctxt, null);
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    Object[] chunk = buffer.resetAndStart();
    int ix = 0;
    try {
        while (true) {
            String value = p.nextTextValue();
            if (value == null) {
                JsonToken t = p.getCurrentToken();
                if (t == JsonToken.END_ARRAY) {
                    break;
                }
                if (t == JsonToken.VALUE_NULL) {
                    if (_skipNullValues) {
                        continue;
                    }
                    value = (String) _nullProvider.getNullValue(ctxt);
                } else {
                    value = _parseString(p, ctxt);
                }
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
    } catch (Exception e) {
        throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
    }
    String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
    ctxt.returnObjectBuffer(buffer);
    return result;
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer) IOException(java.io.IOException)

Example 7 with ObjectBuffer

use of com.fasterxml.jackson.databind.util.ObjectBuffer in project jackson-databind by FasterXML.

the class UntypedObjectDeserializer method mapArray.

/*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
/**
     * Method called to map a JSON Array into a Java value.
     */
protected Object mapArray(JsonParser p, DeserializationContext ctxt) throws IOException {
    // Minor optimization to handle small lists (default size for ArrayList is 10)
    if (p.nextToken() == JsonToken.END_ARRAY) {
        return new ArrayList<Object>(2);
    }
    Object value = deserialize(p, ctxt);
    if (p.nextToken() == JsonToken.END_ARRAY) {
        ArrayList<Object> l = new ArrayList<Object>(2);
        l.add(value);
        return l;
    }
    Object value2 = deserialize(p, ctxt);
    if (p.nextToken() == JsonToken.END_ARRAY) {
        ArrayList<Object> l = new ArrayList<Object>(2);
        l.add(value);
        l.add(value2);
        return l;
    }
    ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    Object[] values = buffer.resetAndStart();
    int ptr = 0;
    values[ptr++] = value;
    values[ptr++] = value2;
    int totalSize = ptr;
    do {
        value = deserialize(p, ctxt);
        ++totalSize;
        if (ptr >= values.length) {
            values = buffer.appendCompletedChunk(values);
            ptr = 0;
        }
        values[ptr++] = value;
    } while (p.nextToken() != JsonToken.END_ARRAY);
    // let's create full array then
    ArrayList<Object> result = new ArrayList<Object>(totalSize);
    buffer.completeAndClearBuffer(values, ptr, result);
    return result;
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer)

Aggregations

ObjectBuffer (com.fasterxml.jackson.databind.util.ObjectBuffer)7 IOException (java.io.IOException)5 TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)2