Java Code Examples for com.fasterxml.jackson.core.JsonParser#setCurrentValue()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#setCurrentValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeanAsArrayDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt)
    throws IOException
{
    // Let's delegate just in case we got a JSON Object (could error out, alternatively?)
    if (!p.isExpectedStartArrayToken()) {
        return _deserializeFromNonArray(p, ctxt);
    }
    if (!_vanillaProcessing) {
        return _deserializeNonVanilla(p, ctxt);
    }
    final Object bean = _valueInstantiator.createUsingDefault(ctxt);
    // [databind#631]: Assign current value, to be accessible by custom serializers
    p.setCurrentValue(bean);

    final SettableBeanProperty[] props = _orderedProperties;
    int i = 0;
    final int propCount = props.length;
    while (true) {
        if (p.nextToken() == JsonToken.END_ARRAY) {
            return bean;
        }
        if (i == propCount) {
            break;
        }
        SettableBeanProperty prop = props[i];
        if (prop != null) { // normal case
            try {
                prop.deserializeAndSet(p, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, prop.getName(), ctxt);
            }
        } else { // just skip?
            p.skipChildren();
        }
        ++i;
    }
    // Ok; extra fields? Let's fail, unless ignoring extra props is fine
    if (!_ignoreAllUnknown && ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
        ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
                "Unexpected JSON values; expected at most %d properties (in JSON Array)",
                propCount);
        // never gets here
    }
    // otherwise, skip until end
    do {
        p.skipChildren();
    } while (p.nextToken() != JsonToken.END_ARRAY);
    return bean;
}
 
Example 2
Source File: BeanAsArrayDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt, Object bean)
    throws IOException
{
    // [databind#631]: Assign current value, to be accessible by custom serializers
    p.setCurrentValue(bean);

    if (!p.isExpectedStartArrayToken()) {
        return _deserializeFromNonArray(p, ctxt);
    }
    
    /* No good way to verify that we have an array... although could I guess
     * check via JsonParser. So let's assume everything is working fine, for now.
     */
    if (_injectables != null) {
        injectValues(ctxt, bean);
    }
    final SettableBeanProperty[] props = _orderedProperties;
    int i = 0;
    final int propCount = props.length;
    while (true) {
        if (p.nextToken() == JsonToken.END_ARRAY) {
            return bean;
        }
        if (i == propCount) {
            break;
        }
        SettableBeanProperty prop = props[i];
        if (prop != null) { // normal case
            try {
                prop.deserializeAndSet(p, ctxt, bean);
            } catch (Exception e) {
                wrapAndThrow(e, bean, prop.getName(), ctxt);
            }
        } else { // just skip?
            p.skipChildren();
        }
        ++i;
    }
    
    // Ok; extra fields? Let's fail, unless ignoring extra props is fine
    if (!_ignoreAllUnknown && ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
        ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
                "Unexpected JSON values; expected at most %d properties (in JSON Array)",
                propCount);
        // never gets here
    }
    // otherwise, skip until end
    do {
        p.skipChildren();
    } while (p.nextToken() != JsonToken.END_ARRAY);
    return bean;
}
 
Example 3
Source File: BeanAsArrayDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Alternate deserialization method that has to check many more configuration
 * aspects than the "vanilla" processing.
 */
protected Object _deserializeNonVanilla(JsonParser p, DeserializationContext ctxt)
    throws IOException
{
    if (_nonStandardCreation) {
        return deserializeFromObjectUsingNonDefault(p, ctxt);
    }
    final Object bean = _valueInstantiator.createUsingDefault(ctxt);
    // [databind#631]: Assign current value, to be accessible by custom serializers
    p.setCurrentValue(bean);
    if (_injectables != null) {
        injectValues(ctxt, bean);
    }
    Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
    final SettableBeanProperty[] props = _orderedProperties;
    int i = 0;
    final int propCount = props.length;

    while (true) {
        if (p.nextToken() == JsonToken.END_ARRAY) {
            return bean;
        }
        if (i == propCount) {
            break;
        }
        SettableBeanProperty prop = props[i];
        ++i;
        if (prop != null) { // normal case
            if (activeView == null || prop.visibleInView(activeView)) {
                try {
                    prop.deserializeAndSet(p, ctxt, bean);
                } catch (Exception e) {
                    wrapAndThrow(e, bean, prop.getName(), ctxt);
                }
                continue;
            }
        }
        // otherwise, skip it (view-filtered, no prop etc)
        p.skipChildren();
    }
    // Ok; extra fields? Let's fail, unless ignoring extra props is fine
    if (!_ignoreAllUnknown) {
        ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
                "Unexpected JSON values; expected at most %d properties (in JSON Array)",
                propCount);
        // will never reach here as exception has been thrown
    }
    // otherwise, skip until end
    do {
        p.skipChildren();
    } while (p.nextToken() != JsonToken.END_ARRAY);
    return bean;
}
 
Example 4
Source File: CustomCollectionDeserializer.java    From caravan with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt,
    Object result)
    throws IOException {
  // Ok: must point to START_ARRAY (or equivalent)
  if (!p.isExpectedStartArrayToken()) {
    return handleNonArray(p, ctxt, (Collection<Object>) result);
  }
  // [databind#631]: Assign current value, to be accessible by custom serializers
  p.setCurrentValue(result);

  JsonDeserializer<Object> valueDes = _valueDeserializer;
  final TypeDeserializer typeDeser = _valueTypeDeserializer;
  CollectionReferringAccumulator referringAccumulator =
      (valueDes.getObjectIdReader() == null) ? null :
          new CollectionReferringAccumulator(_containerType.getContentType().getRawClass(), (Collection<Object>) result);

  JsonToken t;
  while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
    try {
      Object value;
      if (t == JsonToken.VALUE_NULL) {
        if (_skipNullValues) {
          continue;
        }
        value = _nullProvider.getNullValue(ctxt);
      } else if (typeDeser == null) {
        value = valueDes.deserialize(p, ctxt);
      } else {
        value = valueDes.deserializeWithType(p, ctxt, typeDeser);
      }
      if (referringAccumulator != null) {
        referringAccumulator.add(value);
      } else {
        ((Collection<Object>) result).add(value);
      }
    } catch (UnresolvedForwardReference reference) {
      if (referringAccumulator == null) {
        throw JsonMappingException
            .from(p, "Unresolved forward reference but no identity info", reference);
      }
      Referring ref = referringAccumulator.handleUnresolvedReference(reference);
      reference.getRoid().appendReferring(ref);
    } catch (Exception e) {
      boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
      if (!wrap) {
        ClassUtil.throwIfRTE(e);
      }
      throw JsonMappingException.wrapWithPath(e, result, ((Collection<Object>) result).size());
    }
  }
  return result;
}
 
Example 5
Source File: ListDeserializer.java    From allure2 with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Object> deserialize(JsonParser p, DeserializationContext ctxt,
                                      Collection<Object> result)
        throws IOException {
    // Ok: must point to START_ARRAY (or equivalent)
    if (!p.isExpectedStartArrayToken()) {
        return result;
    }
    // [databind#631]: Assign current value, to be accessible by custom serializers
    p.setCurrentValue(result);

    JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    CollectionReferringAccumulator referringAccumulator =
            (valueDes.getObjectIdReader() == null) ? null :
                    new CollectionReferringAccumulator(_containerType.getContentType().getRawClass(), result);

    JsonToken t;
    while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
        try {
            Object value;
            if (t == JsonToken.VALUE_NULL) {
                if (_skipNullValues) {
                    continue;
                }
                value = _nullProvider.getNullValue(ctxt);
            } else if (typeDeser == null) {
                value = valueDes.deserialize(p, ctxt);
            } else {
                value = valueDes.deserializeWithType(p, ctxt, typeDeser);
            }
            if (referringAccumulator != null) {
                referringAccumulator.add(value);
            } else {
                result.add(value);
            }
        } catch (UnresolvedForwardReference reference) {
            if (referringAccumulator == null) {
                throw JsonMappingException
                        .from(p, "Unresolved forward reference but no identity info", reference);
            }
            ReadableObjectId.Referring ref = referringAccumulator.handleUnresolvedReference(reference);
            reference.getRoid().appendReferring(ref);
        } catch (Exception e) {
            boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
            if (!wrap) {
                ClassUtil.throwIfRTE(e);
            }
            throw JsonMappingException.wrapWithPath(e, result, result.size());
        }
    }
    return result;
}
 
Example 6
Source File: EntityBeanDeserializer.java    From requery with Apache License 2.0 4 votes vote down vote up
@Override
public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (_nonStandardCreation || _needViewProcesing) {
        return super.deserializeFromObject(p, ctxt);
    }

    Object bean = null;

    if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
        String propertyName = p.getCurrentName();
        do {
            p.nextToken();
            SettableBeanProperty property = _beanProperties.find(propertyName);

            if (property == null) {
                handleUnknownVanilla(p, ctxt, bean, propertyName);
                continue;
            }

            // lazily create the bean, the id property must be the first property
            if (bean == null) {
                if (propertyName.equals(_objectIdReader.propertyName.getSimpleName())) {

                    // deserialize id
                    Object id = property.deserialize(p, ctxt);

                    ReadableObjectId objectId = ctxt.findObjectId(id,
                            _objectIdReader.generator, _objectIdReader.resolver);

                    bean = objectId == null ? null : objectId.resolve();
                    if (bean == null) {
                        bean = _valueInstantiator.createUsingDefault(ctxt);
                        property.set(bean, id);
                    }
                } else {
                    bean = _valueInstantiator.createUsingDefault(ctxt);
                }
                p.setCurrentValue(bean);
            }
            property.deserializeAndSet(p, ctxt, bean);

        } while ((propertyName = p.nextFieldName()) != null);
    }

    return bean;
}