Java cast Object to MyClass

Alexander Kuzmenko

I'm writing a storage manager. I want to read from file to object. Example:

protected Object read(String key) throws Exception{
    Path pathReadFrom = Paths.get(getPath(key));

    if (!Files.isReadable(pathReadFrom)){
        throw new FileNotFoundException();
    }

    Object object = JSON_MAPPER.readValue(Files.readAllBytes(pathReadFrom), Object.class);
    return object;
}

JSON_MAPPER is Jackson's ObjectMapper.

public MyClass get(String id) throws Exception {
    MyClass myClassObject = (MyClass) storeManager.read(id);
    return myClassObject;
}

I get the next exception:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.pckg.project.dto.MyClass

How can I create universal read() method? Maybe should I set a type like second argument of read() method?

Alexander Kuzmenko

I've thought up some solution:

protected <T> T read(String key, Class<T> valueType) throws Exception{
    Path pathReadFrom = Paths.get(getPath(key));
    T object = JSON_MAPPER.readValue(Files.readAllBytes(pathReadFrom), valueType);
    return object;
}

public MyClass get(String id) throws Exception {
    MyClass object = storeManager.read(id, MyClass.class);
    return object;
}

It works fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java cast Object to MyClass

From Dev

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type <MyClass>

From Dev

Java : Cast String Array to an Object

From Dev

is Object instance of MyClass

From Dev

Java SQLData - Cast to user object with a list/array?

From Dev

java.lang.Object cannot be cast

From Dev

Why cant I cast Object[] to StringBuilder[] in Java?

From Dev

Generics cannot cast Object to interface class java

From Dev

Java - array types to Object type cast

From Dev

How to cast a java Object to an arbitrary other type

From Dev

Unchecked cast: 'java.lang.Object[]' to 'T[]'

From Dev

JAVA cast to Object to ArrayList doesn't work

From Dev

Why java creates error to cast a object as callable?

From Dev

java.lang.Object cannot be cast

From Dev

How to cast a java Object to an arbitrary other type

From Dev

How to cast an object to field type in Java?

From Dev

Check if object is cast to its dynamic type in Java

From Dev

Java cast object to an interface that is not implemented but matches signature

From Dev

google gson LinkedTreeMap class cast to myclass

From Dev

cast from List<MyClass> to List<Interface>

From Dev

How to cast List<<MyClass> to List<T>?

From Dev

How to fix 'Unchecked cast from MyClass to T'

From Dev

How to cast List<<MyClass> to List<T>?

From Dev

Java List<MyClass> to TreeMap<Long, MyClass>

From Dev

Java: Array of List<MyClass>

From Dev

cast on object of type superclass returns object of type subclass in java

From Dev

Need to cast the class object with parent class's parent object in java

From Dev

Can not cast java.lang.Object to custom object

From Dev

java.lang.ClassCastException: cannot be cast to java.lang.Object

Related Related

HotTag

Archive