com.microsoft.bingads.internal.restful.adaptor.EnumDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of microsoft.bingads Show documentation
Show all versions of microsoft.bingads Show documentation
The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.
package com.microsoft.bingads.internal.restful.adaptor;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class EnumDeserializer extends StdDeserializer> implements ContextualDeserializer {
private Class> enumClass;
public EnumDeserializer() {
super(Enum.class);
}
public EnumDeserializer(Class> enumClass) {
super(enumClass);
this.enumClass = enumClass;
}
@Override
public Enum> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonToken token = p.getCurrentToken();
String s = p.getText();
String s2 = AdaptorUtil.convertStringToEnumValue(s);
if (token == JsonToken.VALUE_STRING) {
return Enum.valueOf((Class)enumClass, AdaptorUtil.convertStringToEnumValue(p.getText()));
}
throw new JsonParseException(p, "Invalid token for enum deserialization: " + token);
}
@Override
public JsonDeserializer> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
Class> enumClass = property.getType().getRawClass();
return new EnumDeserializer(enumClass);
}
}