0

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'm expecting void from the function? I'm attempting to cast it to QueryDefinition. How can I create a custom class that can be deserialized from JSON?

5

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.

1
  • 2
    OK, I think I officially hate case-insensitive languages. :) I'm so used to JSON, json, etc., being unique identifiers in Python and most languages. – Chuck Apr 18 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.