function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc13sfdc13 

how to cover below line in test class

when I run the test class I was getting this error::System.JSONException: Unexpected character ('s' (code 115)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

public  static String save(string data)
 InnerWrapper req =(InnerWrapper)JSON.deserialize(data,InnerWrapper.class);

Inner class
=======
public class InnerWrapper{
        List<String> ls;
        List<String> types;
        String parentId;
        Boolean ismpty;
        Boolean pSave;
        Map<String,Rule__c> type;
        String no;
    }
Best Answer chosen by sfdc13
Tad Aalgaard 3Tad Aalgaard 3
Your data needs to be in a JSON parseable format that matches the structure of your InnerWrapper class.

Here is an example of a class that you can run in the developer console to get you started.  
 
public class DoStuff{

    public static void parseStuff(){
        String data = '{"ls" : ["blah","blah"],"types" : ["blah","blah"],"parentId":"123","no":"42"}';
        InnerWrapper req =(InnerWrapper)JSON.deserialize(data,InnerWrapper.class);
        System.debug(req);
    }

   public class InnerWrapper{
        List<String> ls;
        List<String> types;
        String parentId;
        String no;
   }
}

To run in developer console.
 
DoStuff.parseStuff();

Here is a good page on parsing.

https://www.lopau.com/demystifying-json-parsing-in-apex/
 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
Please include a sample of the "data" that you are trying to deserialize as you more than likely have an issue with the data you are using.
sfdc13sfdc13
I have given a string as data
string data="test data";

can u please give code snippets the type of data i need to pass
Tad Aalgaard 3Tad Aalgaard 3
Your data needs to be in a JSON parseable format that matches the structure of your InnerWrapper class.

Here is an example of a class that you can run in the developer console to get you started.  
 
public class DoStuff{

    public static void parseStuff(){
        String data = '{"ls" : ["blah","blah"],"types" : ["blah","blah"],"parentId":"123","no":"42"}';
        InnerWrapper req =(InnerWrapper)JSON.deserialize(data,InnerWrapper.class);
        System.debug(req);
    }

   public class InnerWrapper{
        List<String> ls;
        List<String> types;
        String parentId;
        String no;
   }
}

To run in developer console.
 
DoStuff.parseStuff();

Here is a good page on parsing.

https://www.lopau.com/demystifying-json-parsing-in-apex/
 
This was selected as the best answer
sfdc13sfdc13
thanks, Tad Aalgaard3 my error resolved
 
Tad Aalgaard 3Tad Aalgaard 3
No problem.  Please mark your post as "Answered" and select my post from above as the solution so that it helps with filtering of posts.