View Raw SPL
/*****************************************************************************
*                                                                            *
*   OBJJSON.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_OBJJSON

    OBJJSON

    Purpose: Converts a JSON string into an object.

    Syntax:  OBJJSON("jstr")

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

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

    Example:
             jobj = objjson('{"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 = objjson(jstr);

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

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

             Object strings are commonly returned by HTML based JSON servers.

             See VIEWJSON to view a JSON string in a file.

             See WEBREADJSON to return a JSON object from a URL.

    See Also:
             Geturl
             Viewjson
             Webreadhtml
             Webreadjson
             Webreadimage
#endif


static ce = -1;

/* JSON string to object */
objjson(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 everything */
                setconfig("spl_convert_escapes", 0x07);

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

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

        return(obj);
}


/* on the fly function to evaluate json expression */
objjson_eval(str)
{
        local jobj, body, errmes = "";

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

                                return(%s);

                        }', str);

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

                jobj = __$j_objjson__();
        }
        catch
        {
                errmes = sprintf("invalid JSON string: '%s'", str);
        }

        delfunction("__$j_objjson__");

        if (strlen(errmes) > 0)
        {
                error(errmes);
        }

        return(jobj);
}


/* error handler */
objjson_error(errnum, errmes)
{
        if (ce >= 0)
        {
                setconfig("spl_convert_escapes", ce);
                ce = -1;
        }

        error(errmes);
}