How can I make a method accept a class type variable in Java?

James Larkin

I've got the following method:

public <T> T deserialise(String payload, Class<T> expectedClass) {
    try {
        return mapper.readValue(payload, expectedClass);
    } catch (IOException e) {
        throw new IllegalStateException("JSON is not valid!", e);
    }
} 

that I can call using deserialise("{\"foo\": \"123\"}", Foo.class).

What type shall I use if I want to create a map from String to Class and then iterate over this map to deserialise the strings into objects?

E.g., I want something similar to:

Map<String, Class?> contents = ImmutableMap.of(
   "{\"foo\": \"123\"}", Foo.class,
   "{\"bar\": \"123\", \"bar2\": \"123\"}", Bar.class
);

And then I want to be able to:

for (Map.Entry<String, Class?> e : contents.entrySet) {
   Class? obj = deserialise(e.getKey(), e.getValue());
}

What should I put instead of Class??

Update:

ObjectMapper objectMapper = new ObjectMapper();

Map<String, Class<?>> contents = ImmutableMap.of(
        "{\"foo\": \"123\"}", Foo.class,
        "{ \"color\" : \"Black\", \"type\" : \"BMW\" }", Bar.class
);

for (Map.Entry<String, Class<?>> e : contents.entrySet()) {
    try {
        Object obj = objectMapper.readValue(e.getKey(), e.getValue());
        System.out.println(obj);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Update #2:

ObjectMapper objectMapper = new ObjectMapper();

String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
T typeClass = Foo.class; // TODO: fix syntax error

try {
    Class<?> obj = objectMapper.readValue(json, typeClass); // TODO: fix error and cast obj to Foo.class using typeClass
} catch (IOException e) {
    e.printStackTrace();
}
Sweeper

Your syntax is very close!

You should use Class<?>. The <?> is known as a generic wildcard.

Map<String, Class<?>> contents = ImmutableMap.of(
   "{\"foo\": \"123\"}", Foo.class,
   "{\"bar\": \"123\", \"bar2\": \"123\"}", Bar.class
);


for (Map.Entry<String, Class<?>> e : contents.entrySet) {
   Object obj = deserialise(e.getKey(), e.getValue());
}

Note that obj should not be of type Class<?> because deserialise returns T, not Class<T>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I interrupt a ServerSocket accept() method?

How can I make an interface instance method accept arguments of the same class only?

How can i make a class Immutable if it has a instance variable of java.lang.Object?

How do I restrict method to accept only object as parameter instead of Class Objects as Type Literals?

How can I return a dependent type from templated class method?

How can I make a Rust function accept any floating type as an argument

In python type-hinting, how can I make an argument accept any subclass of a base class?

How can I ask for the type class of a variable?

How can I get output from method of class into global variable

Can I use type annotations of class to make decision about result type of method of the class?

How can I access class variable from method JS

How can I run a class method stored in a variable in Powershell

How can I make an interface instance method accept arguments of the same class only, really?

How can I make a method return a "variable" (not value)?

How can i make this method return to that variable in that loop

When using the 'Class' datatype, how can I specify the type so I only accept subclass of a specific class?

How to use make "using" keyword within a method to accept it as class level

How can I declare a templated class type as a member method, *not* a property?

How can I make this class work based on a on specific variable (ID)?

How can I call this type of extension method from another class

How can I make a class method that takes a function as a parameter?

How can I access a variable that is defined within the method in the class?

How can I make type inference work in a generic method?

Created a Task interface with an execute method. How can make it accept paramenters dynamically - JAVA

How can I get the type of the implementation class stored in the variable declared in the interface type by Java reflection?

How can i make a method available only from within the class

How can I make a Java method accept arrays of different (primitive) variable types?

How can i make textinput a class variable

how can I make an input field only accept numbers when the input type is text?