Example usage for com.fasterxml.jackson.databind ObjectMapper getSubtypeResolver

List of usage examples for com.fasterxml.jackson.databind ObjectMapper getSubtypeResolver

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper getSubtypeResolver.

Prototype

public SubtypeResolver getSubtypeResolver() 

Source Link

Document

Method for accessing subtype resolver in use.

Usage

From source file:com.flipkart.foxtrot.core.TestUtils.java

public static void registerActions(AnalyticsLoader analyticsLoader, ObjectMapper mapper) throws Exception {
    Reflections reflections = new Reflections("com.flipkart.foxtrot", new SubTypesScanner());
    Set<Class<? extends Action>> actions = reflections.getSubTypesOf(Action.class);
    if (actions.isEmpty()) {
        throw new Exception("No analytics actions found!!");
    }//from   w w w . j  a v a2s.c  om
    List<NamedType> types = new Vector<NamedType>();
    for (Class<? extends Action> action : actions) {
        AnalyticsProvider analyticsProvider = action.getAnnotation(AnalyticsProvider.class);
        if (null == analyticsProvider.request() || null == analyticsProvider.opcode()
                || analyticsProvider.opcode().isEmpty() || null == analyticsProvider.response()) {
            throw new Exception("Invalid annotation on " + action.getCanonicalName());
        }
        if (analyticsProvider.opcode().equalsIgnoreCase("default")) {
            logger.warn("Action " + action.getCanonicalName() + " does not specify cache token. "
                    + "Using default cache.");
        }
        analyticsLoader.register(new ActionMetadata(analyticsProvider.request(), action,
                analyticsProvider.cacheable(), analyticsProvider.opcode()));
        types.add(new NamedType(analyticsProvider.request(), analyticsProvider.opcode()));
        types.add(new NamedType(analyticsProvider.response(), analyticsProvider.opcode()));
        logger.info("Registered action: " + action.getCanonicalName());
    }
    mapper.getSubtypeResolver().registerSubtypes(types.toArray(new NamedType[types.size()]));
}

From source file:org.deeplearning4j.nn.conf.ComputationGraphConfiguration.java

/**
 * Create a computation graph configuration from json
 *
 * @param json the neural net configuration from json
 * @return {@link org.deeplearning4j.nn.conf.ComputationGraphConfiguration}
 *///  ww w.  j  av  a2  s .c  om
public static ComputationGraphConfiguration fromJson(String json) {
    //As per MultiLayerConfiguration.fromJson()
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    try {
        return mapper.readValue(json, ComputationGraphConfiguration.class);
    } catch (IOException e) {
        //No op - try again after adding new subtypes
    }

    //Try: programmatically registering JSON subtypes for GraphVertex classes. This allows users to to add custom GraphVertex
    // implementations without needing to manually register subtypes
    //First: get all registered subtypes
    AnnotatedClass ac = AnnotatedClass.construct(GraphVertex.class,
            mapper.getSerializationConfig().getAnnotationIntrospector(), null);
    Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac,
            mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector());
    Set<Class<?>> registeredSubtypes = new HashSet<>();
    for (NamedType nt : types) {
        registeredSubtypes.add(nt.getType());
    }

    //Second: get all subtypes of GraphVertex using reflection
    Reflections reflections = new Reflections();
    Set<Class<? extends GraphVertex>> subTypes = reflections.getSubTypesOf(GraphVertex.class);

    //Third: register all subtypes that are not already registered
    List<NamedType> toRegister = new ArrayList<>();
    for (Class<? extends GraphVertex> c : subTypes) {
        if (!registeredSubtypes.contains(c)) {
            String name;
            if (ClassUtils.isInnerClass(c)) {
                Class<?> c2 = c.getDeclaringClass();
                name = c2.getSimpleName() + "$" + c.getSimpleName();
            } else {
                name = c.getSimpleName();
            }
            toRegister.add(new NamedType(c, name));
        }
    }
    mapper = NeuralNetConfiguration.reinitMapperWithSubtypes(toRegister);

    try {
        return mapper.readValue(json, ComputationGraphConfiguration.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}