0

I'm running into a problem with my JSON2Apex code where I'm unable to define objects of another class in the current class I'm working on and I'm not sure where I'm running afoul. I tried changing the 'public class' to a 'global with sharing class ... extends ...' as referenced here, but I'm now getting the below error message:

Line: 1, Column: 27 Dependent class is invalid and needs recompilation: Class restCallout : Non-virtual and non-abstract type cannot be extended: JSON2Apex

I'm not sure how to tie one class (ie make one an ancestor class) into another so I can perform the following call:

JSON2Apex data = (JSON2Apex)JSON.deserialize(res.getBody(), JSON2Apex.class);

I've also tried making my JSON2Apex class a 'public virtual class' so I can 'extend' my "restCallout" class, however that yields a different error message as well:

Line: x, Column: 1 System.JSONException: Expected JSON2Apex.Value but found [line: x, column: x]

Class 1:

public class restCallout extends JSON2Apex{  
    public static void restGroups() { 
    HTTPRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setHeader('Accept', 'application/json');
    req.setEndpoint('callout:rest_endpoint1/');
    HTTPResponse res = new Http().send(req);
    JSON2Apex data = (JSON2Apex)JSON.deserialize(res.getBody(), JSON2Apex.class);
    }
...
}

Class 2:

public virtual class JSON2Apex {
   public class...{
   }
}
  • can you post more code? it is really hard to figure out what is your code base status based on your messages so far. Just structure should be enough – kurunve Aug 16 '18 at 14:43
  • @kurunve let me know if the above code snippets are what you're looking for – xorLogic Aug 16 '18 at 14:47
  • Try removing the inner class from the JSON2Apex generated class and put all the properties right at the top level class. – Daniel Hoechst Aug 16 '18 at 15:27
  • Doesn't work. You get invalid types for the JSON objects you're trying to pull. – xorLogic Aug 16 '18 at 15:34
1

Generally composition makes more sense than inheritance: build one class by incorporating other classes.

In your example, leave the generated JSON2Apex class unchanged (or better rename it to reflect the service it calls) and then just reference it in your class:

public class restCallout {  
    public static void restGroups() { 
        HTTPRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setHeader('Accept', 'application/json');
        req.setEndpoint('callout:rest_endpoint1/');
        HTTPResponse res = new Http().send(req);
        JSON2Apex data = (JSON2Apex)JSON.deserialize(res.getBody(), JSON2Apex.class);
        ...
    }
}
  • Hi Keith, appreciate the feedback and the article. Inheritance may be my only option because I'm not proficient enough to make heads/tails of the JSON2Apex generator output to compose it in my existing .axpc file. There's also the issue of the generator using nested classes and I realized you can only go two levels deep before hitting errors if that makes any sense. What's the procedure for doing class calls between two .axpc files in the Apex Developer Console? – xorLogic Aug 17 '18 at 16:22
  • @xorLogic From the console you can't define classes so one of them e.g. the JSON2Apex one will have to be already creaed in the org. – Keith C Aug 17 '18 at 17:51
  • Okay, I think I got it. Just had to use the extends and virtual top class defs – xorLogic Aug 21 '18 at 16:35

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.