use of com.fasterxml.jackson.annotation.JsonCreator in project presto by prestodb.
the class NullableValue method fromSerializable.
// Jackson deserialization only
@JsonCreator
public static NullableValue fromSerializable(@JsonProperty("serializable") Serializable serializable) {
Type type = serializable.getType();
Block block = serializable.getBlock();
return new NullableValue(type, block == null ? null : Utils.blockToNativeValue(type, block));
}
use of com.fasterxml.jackson.annotation.JsonCreator in project presto by prestodb.
the class WrappedRange method fromBytes.
@JsonCreator
public static WrappedRange fromBytes(byte[] bytes) throws IOException {
DataInput in = ByteStreams.newDataInput(bytes);
Range range = new Range();
range.readFields(in);
return new WrappedRange(range);
}
use of com.fasterxml.jackson.annotation.JsonCreator in project Gaffer by gchq.
the class JsonSerialisationUtil method getSerialisedFieldClasses.
/**
* Gets all the fields and their classes for a given class.
*
* @param className the class name to find the fields for.
* @return a map of field name to class name
*/
public static Map<String, String> getSerialisedFieldClasses(final String className) {
final Map<String, String> cachedResult = cache.get(className);
if (null != cachedResult) {
return cachedResult;
}
final Class<?> clazz;
try {
clazz = Class.forName(SimpleClassNameIdResolver.getClassName(className));
} catch (final Exception e) {
throw new IllegalArgumentException("Class name was not recognised: " + className, e);
}
final ObjectMapper mapper = new ObjectMapper();
final JavaType type = mapper.getTypeFactory().constructType(clazz);
final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
final Class<?> builder = introspection.findPOJOBuilder();
String buildMethodPrefix = "with";
if (null != builder) {
JsonPOJOBuilder anno = findAnnotation(builder, JsonPOJOBuilder.class);
if (null != anno) {
buildMethodPrefix = anno.withPrefix();
}
}
Constructor<?> creator = null;
for (final Constructor<?> constructor : type.getRawClass().getDeclaredConstructors()) {
final JsonCreator anno = constructor.getAnnotation(JsonCreator.class);
if (null != anno) {
creator = constructor;
break;
}
}
final List<BeanPropertyDefinition> properties = introspection.findProperties();
final Map<String, String> fieldMap = new HashMap<>();
for (final BeanPropertyDefinition property : properties) {
final String propName = property.getName();
final String propClass;
if ("class".equals(propName)) {
propClass = Class.class.getName();
} else {
Type genericType = null;
if (null != builder) {
final String methodName = buildMethodPrefix + propName;
Method matchedMethod = null;
for (final Method method : builder.getMethods()) {
if (methodName.equalsIgnoreCase(method.getName())) {
final Type[] params = method.getGenericParameterTypes();
if (null != params && 1 == params.length) {
final JsonSetter jsonSetter = method.getAnnotation(JsonSetter.class);
if (null != jsonSetter && propName.equals(jsonSetter.value())) {
matchedMethod = method;
break;
}
final JsonProperty jsonProperty = method.getAnnotation(JsonProperty.class);
if (null != jsonProperty && propName.equals(jsonProperty.value())) {
matchedMethod = method;
break;
}
if (null == matchedMethod) {
matchedMethod = method;
} else if (builder.equals(method.getReturnType())) {
// Checks for overridden methods
matchedMethod = method;
}
}
}
}
if (null != matchedMethod) {
genericType = matchedMethod.getGenericParameterTypes()[0];
}
}
if (null == genericType && null != creator) {
for (final Parameter parameter : creator.getParameters()) {
final JsonProperty anno = parameter.getAnnotation(JsonProperty.class);
if (null != anno && propName.equals(anno.value())) {
if (null != parameter.getParameterizedType()) {
genericType = parameter.getParameterizedType();
} else {
genericType = parameter.getType();
}
break;
}
}
}
if (null == genericType && null != property.getSetter() && null != property.getSetter().getGenericParameterTypes() && 1 == property.getSetter().getGenericParameterTypes().length) {
genericType = property.getSetter().getGenericParameterTypes()[0];
}
if (null != genericType && genericType instanceof Class && ((Class) genericType).isEnum()) {
genericType = String.class;
}
if (null == genericType) {
propClass = Object.class.getName();
} else {
propClass = getFieldTypeString(clazz, genericType);
}
}
fieldMap.put(propName, propClass);
}
final Map<String, Map<String, String>> newCache = new HashMap<>(cache);
newCache.put(className, Collections.unmodifiableMap(fieldMap));
cache = Collections.unmodifiableMap(newCache);
return fieldMap;
}
use of com.fasterxml.jackson.annotation.JsonCreator in project presto by prestodb.
the class HostAddress method fromString.
/**
* Split a freeform string into a host and port, without strict validation.
* <p>
* Note that the host-only formats will leave the port field undefined. You
* can use {@link #withDefaultPort(int)} to patch in a default value.
*
* @param hostPortString the input string to parse.
* @return if parsing was successful, a populated HostAddress object.
* @throws IllegalArgumentException if nothing meaningful could be parsed.
*/
@JsonCreator
public static HostAddress fromString(String hostPortString) {
requireNonNull(hostPortString, "hostPortString is null");
String host;
String portString = null;
if (hostPortString.startsWith("[")) {
// Parse a bracketed host, typically an IPv6 literal.
Matcher matcher = BRACKET_PATTERN.matcher(hostPortString);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid bracketed host/port: " + hostPortString);
}
host = matcher.group(1);
// could be null
portString = matcher.group(2);
} else {
int colonPos = hostPortString.indexOf(':');
if (colonPos >= 0 && hostPortString.indexOf(':', colonPos + 1) == -1) {
// Exactly 1 colon. Split into host:port.
host = hostPortString.substring(0, colonPos);
portString = hostPortString.substring(colonPos + 1);
} else {
// 0 or 2+ colons. Bare hostname or IPv6 literal.
host = hostPortString;
}
}
int port = NO_PORT;
if (portString != null && portString.length() != 0) {
// JDK7 accepts leading plus signs. We don't want to.
if (portString.startsWith("+")) {
throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
}
try {
port = Integer.parseInt(portString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
}
if (!isValidPort(port)) {
throw new IllegalArgumentException("Port number out of range: " + hostPortString);
}
}
return new HostAddress(host, port);
}
use of com.fasterxml.jackson.annotation.JsonCreator in project dropwizard by dropwizard.
the class Size method parse.
@JsonCreator
public static Size parse(String size) {
final Matcher matcher = SIZE_PATTERN.matcher(size);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid size: " + size);
}
final long count = Long.parseLong(matcher.group(1));
final SizeUnit unit = SUFFIXES.get(matcher.group(2));
if (unit == null) {
throw new IllegalArgumentException("Invalid size: " + size + ". Wrong size unit");
}
return new Size(count, unit);
}
Aggregations