Try OpenEdge Now
skip to main content
Working with JSON
Reading and Serializing JSON to/from ProDataSets and Temp-tables : Using JSON with WebSpeed
 

Using JSON with WebSpeed

Because of JSON's popularity in AJAX applications, WebSpeed applications are an obvious place to use these features. To support using JSON with WebSpeed, the WEB-CONTEXT system handle has the IS-JSON attribute. The IS-JSON attribute indicates whether or not a JSON string was posted to the WebSpeed Transaction Server. The AVM determines this by looking for the following content-type HTTP headers:
*"application/json"
*"text/json"
The exact procedure for communicating with a rich Internet application vary depending on the application. As a general example, you might have something like the following procedure fragment to handle JSON requests in a WebSpeed application:
{src/web2/wrap-cgi.i}        /* standard WebSpeed include */
{pi-json-parameterVarDefs.i} /* parameter variable definitions */
{pi-write-json-tt.i}         /* ttCust definition */

PROCEDURE process-web-request:
  DEFINE VARIABLE httCust AS HANDLE   NO-UNDO.
  DEFINE VARIABLE lRetOK  AS LOGICAL  NO-UNDO.
  DEFINE VARIABLE lcData AS LONGCHAR NO-UNDO.

  httCust = TEMP-TABLE ttCust:HANDLE.

  /* 2 */ IF NOT WEB-CONTEXT:IS-JSON THEN
  DO ON ERROR UNDO, LEAVE:
    /* 3 */ RUN populateTT.
    httCust:WRITE-JSON("LONGCHAR", lcData).

/* Build and return your initial HTML response with the JSON of the initial temp-table values */
    /* 4 */ output-content-type ("text/html":U).
    RUN writeHTML(lcData).
    . . .
  /* 6 */ ELSE DO:
    lRetOK = httCust:READ-JSON("HANDLE", WEB-CONTEXT, "MERGE").
    /* 7 */ IF NOT lRetOK THEN
    DO:
      output-content-type ("text/html":U).
      {&OUT}
        "<html>":U SKIP
        "<head>":U SKIP
        "<title> {&FILE-NAME} </title>":U SKIP
        "</head>":U SKIP
        "<body>":U SKIP
        "<p>Failed to load JSON into data object:</p>":U SKIP
        "<p>" + ERROR-STATUS:GET-MESSAGE(1) + "</p>":U SKIP
        "</BODY>":U SKIP
        "</HTML>":U SKIP.
    END.
    /* 8 */ ELSE DO:
      output-content-type ("application/json":U).
      httCust:WRITE-JSON("STREAM", "WebStream").
    END.
  END.
END PROCEDURE.
Using the process-web-request procedure, a complete Web request might run as follows:
1. You log in, creating a request for the initial page that starts the process-web-request procedure on the WebSpeed Transaction Server.
2. The procedure first checks whether or not the request is a JSON string.
3. The initial request is not a JSON string, so the procedure populates the temp-table with the initial records and serializes the temp-table using WRITE-JSON( ).
4. The procedure then builds a HTML page around the JSON and sends it back to the Web browser.
5. You update some of the records and posts the changes back to the server.
6. Since the new Web request is a JSON string, the procedure tries to merge the updates into the temp-table using READ-JSON( ).
7. If merging the updates into the temp-table fails, the procedure builds and returns a HTML page reporting the error.
8. If merging the updates into the temp-table succeeds, the procedure serializes the updated temp-table using WRITE-JSON( ) and posts the updates back to the Web browser.