All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.jackson.map.deser.CollectionDeserializer Maven / Gradle / Ivy

Go to download

Data Mapper package is a high-performance data binding package built on Jackson JSON processor

There is a newer version: 1.9.13
Show newest version
package org.codehaus.jackson.map.deser;

import java.io.IOException;
import java.util.*;

import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.DeserializationContext;

/**
 * Basic serializer that can take Json "Array" structure and
 * construct a {@link java.util.Collection} instance, with typed contents.
 *

* Note: for untyped content (one indicated by passing Object.class * as the type), {@link UntypedObjectDeserializer} is used instead. * It can also construct {@link java.util.List}s, but not with specific * POJO types, only other containers and primitives/wrappers. */ public class CollectionDeserializer extends StdDeserializer> { // // Configuration final Class> _collectionClass; /** * Value deserializer. */ final JsonDeserializer _valueDeserializer; @SuppressWarnings("unchecked") public CollectionDeserializer(Class collectionClass, JsonDeserializer valueDeser) { super(Collection.class); _collectionClass = (Class>) collectionClass; _valueDeserializer = valueDeser; } @Override public Collection deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Collection result; try { result = _collectionClass.newInstance(); } catch (Exception e) { throw ctxt.instantiationException(_collectionClass, e); } return deserialize(jp, ctxt, result); } @Override public Collection deserialize(JsonParser jp, DeserializationContext ctxt, Collection result) throws IOException, JsonProcessingException { // Ok: must point to START_ARRAY if (jp.getCurrentToken() != JsonToken.START_ARRAY) { throw ctxt.mappingException(_collectionClass); } JsonDeserializer valueDes = _valueDeserializer; JsonToken t; while ((t = jp.nextToken()) != JsonToken.END_ARRAY) { Object value = (t == JsonToken.VALUE_NULL) ? null : valueDes.deserialize(jp, ctxt); result.add(value); } return result; } }