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

test class to serialize and deserialize json

I need to update the test class to serialize/deserialize json based data.
Can someone pls let me know how to go abt with??
global class ApexCallout {
    global static String doPost(String value1, String value2) {
          String passVal = 'TEST';
        try {
            if(value1 == 'Account') {
                Account a = (Account) JSON.deserialize(value2, Account.class);
                if(a.Id == null)
                    insert a;
        return passVal;
    public static String dmlCallout(String value1, String value2) {
        Http rh = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('content-type', 'application/json');
        req.setHeader('Host', '');

        Map<String, String> body = new Map<String, String>();
        body.put('value1', value1);
        body.put('value2', value2);
        String sbody = JSON.serialize(body);

        req.setHeader('Content-Length', String.valueOf(sbody.length()));

        HTTPResponse res = rh.send(req);
        if(res.getStatusCode() == 500) {
            String jsonString = res.getBody();
            jsonString = jsonString.substring(1, jsonString.length() - 1);
            Map<String, String> error = (Map<String, String>)JSON.deserialize(jsonString, Map<String, String>.class);
            String errorMsg = error.get('message');
            errorMsg = (errorMsg == null ? '' : errorMsg);
            throw new Exception('' + eMsg + '');
        String passVal = res.getBody();
        return passVal;
    public static String update(Account a) {
        return dmlCallout('Account', JSON.serialize(a));

Can you please expand on what you are trying to do and what you have already done?  If you are trying to do a mock result for your HTTP callout you can either generate the object you want to return and JSON.serialize it or you can simply create the expected JSON as a string.