0

I am trying to deserialize a a serialized string of a child class from a static method.

I have tried referenced the childClass from the static method, but it can't find it because it's a static method, I have tried referencing the child class as I normally would, but the syntax is wrong. I'm unable to find an example in the documentation of doing this directly. I know it's possibly I could create a new class and reference that so I could directly deserialize by Apex type myChildClass, but I would like to do this without creating a new class if possible.

Invoking Class

public class otherClass {
    myClass.myChildClass instanceOfChildClass = new myClass.myChildClass(); 
    instanceOfChildClass.myString = 'Test';
    instanceOfChildClass.myMap = myMap.put('testKey','testValue');
    myClass.myMethod(JSON.serialize(instanceOfChildClass));
}

Apex Type & Deserializing Method

public class myClass {
    
    public class myChildClass {
        String myString {get;set;}
        Map<String,string> myMap {get;set;}
    }
     
    public static String myMethod (String serializedMyChildClass) {
        // I want to deserialze using myChildClass
        myClass deserialization = (myClass)JSON.deserialize(serializedMyChildClass, myClass.myChildClass.class);
       return deserialization.myChildClass.myString;
    }
}

1 Answer 1

2

You're using the wrong type:

    myClass deserialization = (myClass)JSON.deserialize(serializedMyChildClass, myClass.myChildClass.class);

Should be:

    myClass.myChildClass deserialization = (myClass.myChildClass)JSON.deserialize(serializedMyChildClass, myClass.myChildClass.class);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .