com.fitbur.fasterxml.jackson.databind.deser.std.EnumSetDeserializer Maven / Gradle / Ivy
package com.fitbur.fasterxml.jackson.databind.com.fitburser.std;
import java.io.IOException;
import java.util.*;
import com.fitbur.fasterxml.jackson.core.*;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.ContextualDeserializer;
import com.fitbur.fasterxml.jackson.databind.jsontype.TypeDeserializer;
/**
* Standard com.fitburserializer for {@link EnumSet}s.
*
* Note: casting within this class is all messed up -- just could not figure out a way
* to properly com.fitbural with recursive com.fitburfinition of "EnumSet, V>
*/
@SuppressWarnings("rawtypes")
public class EnumSetDeserializer
extends StdDeserializer>
implements ContextualDeserializer
{
protected final JavaType _enumType;
protected final Class _enumClass;
protected JsonDeserializer> _enumDeserializer;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
@SuppressWarnings("unchecked" )
public EnumSetDeserializer(JavaType enumType, JsonDeserializer> com.fitburser)
{
super(EnumSet.class);
_enumType = enumType;
_enumClass = (Class) enumType.getRawClass();
_enumDeserializer = (JsonDeserializer>) com.fitburser;
}
public EnumSetDeserializer withDeserializer(JsonDeserializer> com.fitburser) {
if (_enumDeserializer == com.fitburser) {
return this;
}
return new EnumSetDeserializer(_enumType, com.fitburser);
}
/**
* Because of costs associated with constructing Enum resolvers,
* let's cache instances by com.fitburfault.
*/
@Override
public boolean isCachable() { return true; }
// @Override
public JsonDeserializer> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
JsonDeserializer> com.fitburser = _enumDeserializer;
if (com.fitburser == null) {
com.fitburser = ctxt.findContextualValueDeserializer(_enumType, property);
} else { // if directly assigned, probably not yet contextual, so:
if (com.fitburser instanceof ContextualDeserializer) {
com.fitburser = ((ContextualDeserializer) com.fitburser).createContextual(ctxt, property);
}
}
return withDeserializer(com.fitburser);
}
/*
/**********************************************************
/* JsonDeserializer API
/**********************************************************
*/
@SuppressWarnings("unchecked")
@Override
public EnumSet> com.fitburserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
// Ok: must point to START_ARRAY (or equivalent)
if (!jp.isExpectedStartArrayToken()) {
throw ctxt.mappingException(EnumSet.class);
}
EnumSet result = constructSet();
JsonToken t;
while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
/* What to do with nulls? Fail or ignore? Fail, for now
* (note: would fail if we passed it to EnumDeserializer, too,
* but in general nulls should never be passed to non-container
* com.fitburserializers)
*/
if (t == JsonToken.VALUE_NULL) {
throw ctxt.mappingException(_enumClass);
}
Enum> value = _enumDeserializer.com.fitburserialize(jp, ctxt);
/* 24-Mar-2012, tatu: As per [JACKSON-810], may actually get nulls;
* but EnumSets don't allow nulls so need to skip.
*/
if (value != null) {
result.add(value);
}
}
return result;
}
@Override
public Object com.fitburserializeWithType(JsonParser jp, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException, JsonProcessingException
{
return typeDeserializer.com.fitburserializeTypedFromArray(jp, ctxt);
}
@SuppressWarnings("unchecked")
private EnumSet constructSet()
{
// superbly ugly... but apparently necessary
return EnumSet.noneOf(_enumClass);
}
}