Search in sources :

Example 41 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project ratpack by ratpack.

the class DefaultConfigData method getAsConfigObject.

@Override
public <O> ConfigObject<O> getAsConfigObject(String pointer, TypeToken<O> type) {
    JsonNode node = pointer != null ? rootNode.at(pointer) : rootNode;
    if (node.isMissingNode()) {
        node = emptyNode;
    }
    try {
        JavaType javaType = objectMapper.getTypeFactory().constructType(type.getType());
        O value = objectMapper.readValue(new TreeTraversingParser(node, objectMapper), javaType);
        return new DefaultConfigObject<>(pointer, type, value);
    } catch (IOException ex) {
        throw Exceptions.uncheck(ex);
    }
}
Also used : TreeTraversingParser(com.fasterxml.jackson.databind.node.TreeTraversingParser) JavaType(com.fasterxml.jackson.databind.JavaType) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 42 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project Gaffer by gchq.

the class GraphConfigurationService method getSerialisedFields.

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override
public Set<String> getSerialisedFields(final String className) {
    final Class<?> clazz;
    try {
        clazz = Class.forName(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 List<BeanPropertyDefinition> properties = introspection.findProperties();
    final Set<String> fields = new HashSet<>();
    for (final BeanPropertyDefinition property : properties) {
        fields.add(property.getName());
    }
    return fields;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 43 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project alfresco-remote-api by Alfresco.

the class SerializerOfExecutionResult method serialize.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void serialize(ExecutionResult value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
    SerializationConfig config = provider.getConfig();
    Object rootObj = value.getRoot();
    if (rootObj == null) {
        provider.getDefaultNullValueSerializer().serialize(null, jgen, provider);
    } else {
        Class<?> cls = rootObj.getClass();
        // create an untyped map, add the contents of the root + the embeds.
        Map toBeSerialized = new HashMap();
        BeanPropertiesFilter filter = value.getFilter();
        if (filter == null)
            filter = BeanPropertiesFilter.ALLOW_ALL;
        if (Map.class.isAssignableFrom(cls)) {
            // Its a map so
            Map rootAsaMap = (Map) rootObj;
            toBeSerialized.putAll(rootAsaMap);
        } else {
            JavaType classType = config.constructType(cls);
            BeanDescription beanDesc = provider.getConfig().introspect(classType);
            List<BeanPropertyDefinition> props = beanDesc.findProperties();
            for (BeanPropertyDefinition beanProperty : props) {
                if (beanProperty.couldSerialize() && filter.isAllowed(beanProperty.getName())) {
                    Object propertyValue = ResourceInspectorUtil.invokeMethod(beanProperty.getGetter().getAnnotated(), rootObj);
                    if (propertyValue != null) {
                        if ((propertyValue instanceof String)) {
                            if (((String) propertyValue).trim().length() > 0) {
                                toBeSerialized.put(beanProperty.getName(), propertyValue);
                            }
                        } else {
                            toBeSerialized.put(beanProperty.getName(), propertyValue);
                        }
                    }
                }
            }
        }
        // Add embedded
        for (Entry<String, Object> embedded : value.getEmbedded().entrySet()) {
            if (filter == null || filter.isAllowed(embedded.getKey())) {
                toBeSerialized.put(embedded.getKey(), embedded.getValue());
            }
        }
        // if its an embedded entity then render the properties (not as an "entry:")
        if (value.isAnEmbeddedEntity()) {
            jgen.writeObject(toBeSerialized);
        } else {
            jgen.writeStartObject();
            jgen.writeObjectField("entry", toBeSerialized);
            if (value.getRelated() != null && !value.getRelated().isEmpty()) {
                jgen.writeObjectField("relations", value.getRelated());
            }
            jgen.writeEndObject();
        }
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) HashMap(java.util.HashMap) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 44 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project leopard by tanhaichao.

the class UnderlineJson method toEnumListByOnlyKey.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> List<T> toEnumListByOnlyKey(String json, Class<T> clazz) {
    JavaType javaType = getObjectMapper().getTypeFactory().constructParametrizedType(ArrayList.class, List.class, Object.class);
    List<Object> keyList;
    try {
        keyList = getObjectMapper().readValue(json, javaType);
    } catch (Exception e) {
        logger.error("clazz:" + clazz.getName() + " json:" + json);
        throw new JsonException(e.getMessage(), e);
    }
    // System.err.println("keyList:" + keyList);
    List<T> list = new ArrayList<T>();
    for (Object key : keyList) {
        T onum = (T) EnumUtil.toEnum(key, (Class<Enum>) clazz);
        // System.err.println("key:" + key + " onum:" + onum);
        list.add(onum);
    }
    return list;
}
Also used : JsonException(io.leopard.json.JsonException) JavaType(com.fasterxml.jackson.databind.JavaType) ArrayList(java.util.ArrayList) EnumConstantInvalidException(io.leopard.lang.inum.EnumConstantInvalidException) JsonException(io.leopard.json.JsonException)

Example 45 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project rskj by rsksmart.

the class LocalCryptoTest method testAllInCryptoSute.

@Test
public void testAllInCryptoSute() throws ParseException, IOException {
    String json = getJSON("crypto");
    ObjectMapper mapper = new ObjectMapper();
    JavaType type = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, CryptoTestCase.class);
    HashMap<String, CryptoTestCase> testSuite = mapper.readValue(json, type);
    for (String key : testSuite.keySet()) {
        System.out.println("executing: " + key);
        testSuite.get(key).execute();
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) CryptoTestCase(org.ethereum.jsontestsuite.CryptoTestCase) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)264 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)31 Test (org.junit.Test)27 IOException (java.io.IOException)21 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)20 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)17 ArrayList (java.util.ArrayList)17 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)15 Property (io.swagger.models.properties.Property)15 List (java.util.List)15 Map (java.util.Map)13 StringProperty (io.swagger.models.properties.StringProperty)12 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)10 ModelImpl (io.swagger.models.ModelImpl)10 MapProperty (io.swagger.models.properties.MapProperty)10 Method (java.lang.reflect.Method)10 Type (java.lang.reflect.Type)10 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)9 ArrayProperty (io.swagger.models.properties.ArrayProperty)9 IntegerProperty (io.swagger.models.properties.IntegerProperty)9