0 votes
11 views
by

I have built this class, in the attempt to pass some JSON from JavaScript to Apex:

public with sharing class QueryDefinition {
    public Integer lim;
    public Integer offset;
    public String searchTerm;
    public Boolean getCount;
    public List<String> queries;

    public static void testSerialization() {
        Decimal n = (Decimal)JSON.deserialize('100.1', Decimal.class);
        System.assertEquals(n, 100.1);
        System.debug(n);
        String json = '{ "limit": 50, "offset": 0, "searchTerm": "NAP", "getCount": "true", "queries": [ "Status__c = \'NAP\' AND NAP_Confirmed__c = FALSE", "STATUS__C IN (\'OFF\', \'RET\') AND Source__c = \'Loan Officer\'" ] }';
        QueryDefinition query = (QueryDefinition)JSON.deserialize(json, QueryDefinition.class);
        System.debug(query);
    }
}

When I use anonymous Apex deploy this class, I get the following error:

Method does not exist or incorrect signature: void deserialize(String, System.Type)
from the type String (13:55)

Why does the compiler think I'* expecting void from the function? I'* attempting to cast it to QueryDefinition. How can I create a custom class that can be deserialized from JSON?

1 Answer

0 votes
by
 
Best answer

Apex in general is not case-sensitive. Because of that, when you defined your String json variable, it caused you to "shadow" the JSON class.

The hint that this is what's happening is in the error message. "Method does not exist ... from the type String"

You can fix this by using the fully qualified name for the JSON class, System.JSON.deserialize(), or by renaming your String json variable to something else like jsonInput.

Renaming the variable is probably the better approach, in my opinion.

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...