View Raw SPL
/*****************************************************************************
*                                                                            *
*   OBJJSONSTR.SPL   Copyright (C) 2020 DSP Development Corporation          *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Converts a JSON string into an object                       *
*                                                                            *
*   Revisions:   20 Aug 2020  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_OBJJSONSTR

    OBJJSONSTR

    Purpose: Converts a JSON string into an object.

    Syntax:  OBJJSONSTR("jstr")

              "jstr" - A string, the JSON object string.

    Returns: An object, the object represented by the input string.

    Example:
             jobj = objjsonstr('{name:"test", value:10, data:[10, 20, 30]}')

             Returns an object with three properties:

                 name  : "test"
                 value : 10
                 data  : {10, 20, 30}

    Example:
             jstr = geturl("http://dummy.restapiexample.com/api/v1/employees", strescape("Content-Type: application/json\r\n"));
             jobj = objjsonstr(jstr);

             Returns an object where the DATA property contains 24
             mocked employee records.

    Remarks:
             OBJJSONSTR converts a standard JSON object string to an
             actual object.

             Object strings are commonly returned by HTML based JSON servers.

             See WEBREADJSON to return a JSON object from a URL.

    See Also:
             Geturl
             Webreadhtml
             Webreadjson
             Webreadimage
#endif


static ce = -1;

/* JSON string to object */
objjsonstr(str = "")
{
        local obj;
        
        if (strlen(str) > 0)
        {
                /* strip newlines */
                str = strrep(str, strescape("\r"), "");
                str = strrep(str, strescape("\n"), "");

                /* json strings can be escaped */
                ce = getconfig("spl_convert_escapes");

                /* escape quotes and double quotes */
                setconfig("spl_convert_escapes", 0x01);

                /* evaluate at lowest scope to create an object */                
                obj = objjsonstr_eval(str);

                setconfig("spl_convert_escapes", ce);
                ce = -1;
        }
        else
        {
                error(sprintf("%s - json string required", __FUNC__));
        }

        return(obj);
}


/* error handler */
objjsonstr_error(errnum, errmes)
{
        local errstr;

        if (ce >= 0)
        {
                setconfig("spl_convert_escapes", ce);
                ce = -1;
        }

        errstr = strextract(errmes, 15, -1);
        errstr = "objjsonstr" + errstr;

        error(errstr);
}



/* on the fly function to evaluate json expression */
objjsonstr_eval(str)
{
        local jobj, body;

        body = sprintf('
                {
                        local true = "true", false = "false", null = "null";

                        return(%s);

                }', str);

        deffunnomes("__$j_objjson__()", body);

        jobj = __$j_objjson__();

        delfunction("__$j_objjson__");

        return(jobj);
}