Search in sources :

Example 6 with JsonCreator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator in project metacat by Netflix.

the class QualifiedName method fromJson.

/**
 * Creates the name from the json.
 *
 * @param node json node
 * @return qualified name
 */
@JsonCreator
public static QualifiedName fromJson(@NonNull final JsonNode node) {
    final JsonNode catalogNameNode = node.path("catalogName");
    if (catalogNameNode.isMissingNode() || catalogNameNode.isNull() || !catalogNameNode.isTextual()) {
        // If catalogName is not present try to load from the qualifiedName node instead
        final JsonNode nameNode = node.path("qualifiedName");
        if (!nameNode.isNull() && nameNode.isTextual()) {
            return fromString(nameNode.asText(), false);
        } else {
            // if neither are available throw an exception
            throw new IllegalStateException("Node '" + node + "' is missing catalogName");
        }
    }
    final String catalogName = catalogNameNode.asText();
    final JsonNode databaseNameNode = node.path("databaseName");
    String databaseName = null;
    if (databaseNameNode != null) {
        databaseName = databaseNameNode.asText();
    }
    final JsonNode tableNameNode = node.path("tableName");
    String tableName = null;
    if (tableNameNode != null) {
        tableName = tableNameNode.asText();
    }
    final JsonNode partitionNameNode = node.path("partitionName");
    String partitionName = null;
    if (partitionNameNode != null) {
        partitionName = partitionNameNode.asText();
    }
    final JsonNode viewNameNode = node.path("viewName");
    String viewName = null;
    if (viewNameNode != null) {
        viewName = viewNameNode.asText();
    }
    return new QualifiedName(catalogName, databaseName, tableName, partitionName, viewName);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator)

Example 7 with JsonCreator

use of org.apache.flink.shaded.jackson2.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));
}
Also used : Type(com.facebook.presto.common.type.Type) Block(com.facebook.presto.common.block.Block) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator)

Example 8 with JsonCreator

use of org.apache.flink.shaded.jackson2.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);
}
Also used : DataInput(java.io.DataInput) Range(org.apache.accumulo.core.data.Range) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator)

Example 9 with JsonCreator

use of org.apache.flink.shaded.jackson2.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;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) HashMap(java.util.HashMap) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Method(java.lang.reflect.Method) JavaType(com.fasterxml.jackson.databind.JavaType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) JsonPOJOBuilder(com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder) Parameter(java.lang.reflect.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with JsonCreator

use of org.apache.flink.shaded.jackson2.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);
}
Also used : Matcher(java.util.regex.Matcher) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator)

Aggregations

JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)15 Matcher (java.util.regex.Matcher)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Block (com.facebook.presto.common.block.Block)1 Type (com.facebook.presto.common.type.Type)1 Block (com.facebook.presto.spi.block.Block)1 Type (com.facebook.presto.spi.type.Type)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonSetter (com.fasterxml.jackson.annotation.JsonSetter)1 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonPOJOBuilder (com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1 DataInput (java.io.DataInput)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1