Search in sources :

Example 1 with ObjectBuffer

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

the class ObjectArrayDeserializer method deserialize.

/*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
@Override
public Object[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    // Ok: must point to START_ARRAY (or equivalent)
    if (!p.isExpectedStartArrayToken()) {
        return handleNonArray(p, ctxt);
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    Object[] chunk = buffer.resetAndStart();
    int ix = 0;
    JsonToken t;
    final TypeDeserializer typeDeser = _elementTypeDeserializer;
    try {
        while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
            // Note: must handle null explicitly here; value deserializers won't
            Object value;
            if (t == JsonToken.VALUE_NULL) {
                if (_skipNullValues) {
                    continue;
                }
                value = _nullProvider.getNullValue(ctxt);
            } else if (typeDeser == null) {
                value = _elementDeserializer.deserialize(p, ctxt);
            } else {
                value = _elementDeserializer.deserializeWithType(p, ctxt, typeDeser);
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
    } catch (Exception e) {
        throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
    }
    Object[] result;
    if (_untyped) {
        result = buffer.completeAndClearBuffer(chunk, ix);
    } else {
        result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
    }
    ctxt.returnObjectBuffer(buffer);
    return result;
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 2 with ObjectBuffer

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

the class ObjectArrayDeserializer method deserialize.

// since 2.9
@Override
public Object[] deserialize(JsonParser p, DeserializationContext ctxt, Object[] intoValue) throws IOException {
    if (!p.isExpectedStartArrayToken()) {
        Object[] arr = handleNonArray(p, ctxt);
        if (arr == null) {
            return intoValue;
        }
        final int offset = intoValue.length;
        Object[] result = new Object[offset + arr.length];
        System.arraycopy(intoValue, 0, result, 0, offset);
        System.arraycopy(arr, 0, result, offset, arr.length);
        return result;
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    int ix = intoValue.length;
    Object[] chunk = buffer.resetAndStart(intoValue, ix);
    JsonToken t;
    final TypeDeserializer typeDeser = _elementTypeDeserializer;
    try {
        while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
            Object value;
            if (t == JsonToken.VALUE_NULL) {
                if (_skipNullValues) {
                    continue;
                }
                value = _nullProvider.getNullValue(ctxt);
            } else if (typeDeser == null) {
                value = _elementDeserializer.deserialize(p, ctxt);
            } else {
                value = _elementDeserializer.deserializeWithType(p, ctxt, typeDeser);
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
    } catch (Exception e) {
        throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
    }
    Object[] result;
    if (_untyped) {
        result = buffer.completeAndClearBuffer(chunk, ix);
    } else {
        result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
    }
    ctxt.returnObjectBuffer(buffer);
    return result;
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 3 with ObjectBuffer

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

the class StringArrayDeserializer method _deserializeCustom.

/**
     * Offlined version used when we do not use the default deserialization method.
     */
protected final String[] _deserializeCustom(JsonParser p, DeserializationContext ctxt, String[] old) throws IOException {
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    int ix;
    Object[] chunk;
    if (old == null) {
        ix = 0;
        chunk = buffer.resetAndStart();
    } else {
        ix = old.length;
        chunk = buffer.resetAndStart(old, ix);
    }
    final JsonDeserializer<String> deser = _elementDeserializer;
    try {
        while (true) {
            /* 30-Dec-2014, tatu: This may look odd, but let's actually call method
                 *   that suggest we are expecting a String; this helps with some formats,
                 *   notably XML. Note, however, that while we can get String, we can't
                 *   assume that's what we use due to custom deserializer
                 */
            String value;
            if (p.nextTextValue() == null) {
                JsonToken t = p.getCurrentToken();
                if (t == JsonToken.END_ARRAY) {
                    break;
                }
                // Ok: no need to convert Strings, but must recognize nulls
                if (t == JsonToken.VALUE_NULL) {
                    if (_skipNullValues) {
                        continue;
                    }
                    value = (String) _nullProvider.getNullValue(ctxt);
                } else {
                    value = deser.deserialize(p, ctxt);
                }
            } else {
                value = deser.deserialize(p, ctxt);
            }
            if (ix >= chunk.length) {
                chunk = buffer.appendCompletedChunk(chunk);
                ix = 0;
            }
            chunk[ix++] = value;
        }
    } catch (Exception e) {
        // note: pass String.class, not String[].class, as we need element type for error info
        throw JsonMappingException.wrapWithPath(e, String.class, 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 4 with ObjectBuffer

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

the class UntypedObjectDeserializer method mapArrayToArray.

/**
     * Method called to map a JSON Array into a Java Object array (Object[]).
     */
protected Object[] mapArrayToArray(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 NO_OBJECTS;
    }
    ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    Object[] values = buffer.resetAndStart();
    int ptr = 0;
    do {
        Object value = deserialize(p, ctxt);
        if (ptr >= values.length) {
            values = buffer.appendCompletedChunk(values);
            ptr = 0;
        }
        values[ptr++] = value;
    } while (p.nextToken() != JsonToken.END_ARRAY);
    return buffer.completeAndClearBuffer(values, ptr);
}
Also used : ObjectBuffer(com.fasterxml.jackson.databind.util.ObjectBuffer)

Example 5 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, String[] intoValue) throws IOException {
    // Ok: must point to START_ARRAY (or equivalent)
    if (!p.isExpectedStartArrayToken()) {
        String[] arr = handleNonArray(p, ctxt);
        if (arr == null) {
            return intoValue;
        }
        final int offset = intoValue.length;
        String[] result = new String[offset + arr.length];
        System.arraycopy(intoValue, 0, result, 0, offset);
        System.arraycopy(arr, 0, result, offset, arr.length);
        return result;
    }
    if (_elementDeserializer != null) {
        return _deserializeCustom(p, ctxt, intoValue);
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    int ix = intoValue.length;
    Object[] chunk = buffer.resetAndStart(intoValue, ix);
    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) {
                    // 03-Feb-2017, tatu: Should we skip null here or not?
                    if (_skipNullValues) {
                        return NO_STRINGS;
                    }
                    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)

Aggregations

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