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

com.fitbur.fasterxml.jackson.databind.deser.SettableAnyProperty Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.fasterxml.jackson.databind.com.fitburser;

import java.io.IOException;
import java.lang.reflect.*;

import com.fitbur.fasterxml.jackson.core.*;
import com.fitbur.fasterxml.jackson.databind.BeanProperty;
import com.fitbur.fasterxml.jackson.databind.DeserializationContext;
import com.fitbur.fasterxml.jackson.databind.JavaType;
import com.fitbur.fasterxml.jackson.databind.JsonDeserializer;
import com.fitbur.fasterxml.jackson.databind.JsonMappingException;
import com.fitbur.fasterxml.jackson.databind.introspect.AnnotatedMethod;

/**
 * Class that represents a "wildcard" set method which can be used
 * to generically set values of otherwise unmapped (aka "unknown")
 * properties read from Json content.
 *

* !!! Note: might make sense to refactor to share some code * with {@link SettableBeanProperty}? */ public final class SettableAnyProperty { /** * Method used for setting "any" properties, along with annotation * information. Retained to allow contextualization of any properties. */ final protected BeanProperty _property; /** * Physical JDK object used for assigning properties. */ final protected Method _setter; final protected JavaType _type; protected JsonDeserializer _valueDeserializer; /* /********************************************************** /* Life-cycle /********************************************************** */ public SettableAnyProperty(BeanProperty property, AnnotatedMethod setter, JavaType type, JsonDeserializer valueDeser) { this(property, setter.getAnnotated(), type, valueDeser); } public SettableAnyProperty(BeanProperty property, Method rawSetter, JavaType type, JsonDeserializer valueDeser) { _property = property; _type = type; _setter = rawSetter; _valueDeserializer = valueDeser; } public SettableAnyProperty withValueDeserializer(JsonDeserializer com.fitburser) { return new SettableAnyProperty(_property, _setter, _type, com.fitburser); } /* /********************************************************** /* Public API, accessors /********************************************************** */ public BeanProperty getProperty() { return _property; } public boolean hasValueDeserializer() { return (_valueDeserializer != null); } public JavaType getType() { return _type; } /* /********************************************************** /* Public API, com.fitburserialization /********************************************************** */ /** * Method called to com.fitburserialize appropriate value, given parser (and * context), and set it using appropriate method (a setter method). */ public final void com.fitburserializeAndSet(JsonParser jp, DeserializationContext ctxt, Object instance, String propName) throws IOException, JsonProcessingException { set(instance, propName, com.fitburserialize(jp, ctxt)); } public final Object com.fitburserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_NULL) { return null; } return _valueDeserializer.com.fitburserialize(jp, ctxt); } public final void set(Object instance, String propName, Object value) throws IOException { try { _setter.invoke(instance, propName, value); } catch (Exception e) { _throwAsIOE(e, propName, value); } } /* /********************************************************** /* Helper methods /********************************************************** */ /** * @param e Exception to re-throw or wrap * @param propName Name of property (from Json input) to set * @param value Value of the property */ protected void _throwAsIOE(Exception e, String propName, Object value) throws IOException { if (e instanceof IllegalArgumentException) { String actType = (value == null) ? "[NULL]" : value.getClass().getName(); StringBuilder msg = new StringBuilder("Problem com.fitburserializing \"any\" property '").append(propName); msg.append("' of class "+getClassName()+" (expected type: ").append(_type); msg.append("; actual type: ").append(actType).append(")"); String origMsg = e.getMessage(); if (origMsg != null) { msg.append(", problem: ").append(origMsg); } else { msg.append(" (no error message provided)"); } throw new JsonMappingException(msg.toString(), null, e); } if (e instanceof IOException) { throw (IOException) e; } if (e instanceof RuntimeException) { throw (RuntimeException) e; } // let's wrap the innermost problem Throwable t = e; while (t.getCause() != null) { t = t.getCause(); } throw new JsonMappingException(t.getMessage(), null, t); } private String getClassName() { return _setter.getDeclaringClass().getName(); } @Override public String toString() { return "[any property on class "+getClassName()+"]"; } }