I thought it was clever too. That’s why we like hiring the young’ns...

1. Create a form named JSON_Viewer that has:
  - a system web area with On Load enabled
  - a button labeled “Save as JSON” with On Clicked enabled

2. Create two project methods:
  - JSON_Display
  - JSON_Display_Initialize

3. Download the four required files from my Dropbox and put them into whatever 
resource distribution system you use (Resources folder, templates table, etc.):
  - jquery.js
  - json-viewer.css 
  - json-viewer.js
  - JSON_Viewer_Template.html

https://www.dropbox.com/sh/bcmzoq0n8l8m84a/AABIrBuuvVGNYJworNFvZWVMa?dl=0

4. Replace File_Create and File_Cleanup in the code below with your own file 
management wrappers.

5. Usage:

   JSON_Display ($my4DObjectVar)



———— Code for the web area On Load event:

Case of 
        : (Form event=On Load)

                WA OPEN URL(*;OBJECT Get name;tJSON_Display_Path)

End case 


———— Code for the Save as JSON button:

Case of 
        : (Form event=On Clicked)

                $hDoc:=Create document("";".json")
                If (OK=1)
                        SEND PACKET($hDoc;tJSON)
                        CLOSE DOCUMENT($hDoc)
                End if 
End case 


———— Code for JSON_Display_Initialize:

  // JSON_Display_Initialize
  //
  // Backend method for JSON_Display to work. This creates the prerequisite .js 
  // files in the temp folder.
  //
  // No Parameters.

C_TEXT($tJQuery;$tJsonViewerCSS;$tJsonViewerJS)

  // Copy the required files into text variables and write
  // them into the temp directory. These must be saved into the
  // same directory.

$tJQuery:=Global_TextForKey ("jquery.js")
$tJsonViewerCSS:=Global_TextForKey ("json-viewer.css")
$tJsonViewerJS:=Global_TextForKey ("json-viewer.js")

File_Create ("jquery.js";$tJQuery;"Temp_Folder")
File_Create ("json-viewer.css";$tJsonViewerCSS;"Temp_Folder")
File_Create ("json-viewer.js";$tJsonViewerJS;"Temp_Folder”)


———— Code for JSON_Display:

  // JSON_Display ( $oObject )
  //
  // Displays an object as JSON in a web area.
  //
  // $1 - OBJECT - the JSON object to display

C_OBJECT($1;$o)
$o:=$1

  // Initialize components.
JSON_Display_Initialize 

C_TEXT($tHTML;$tName;tJSON_Display_Path;tJSON)

tJSON:=JSON Stringify($o)

  // Create HTML file using text substitution on the
  // file JSON_Viewer_Template.html
$tHTML:=Replace string(Global_TextForKey 
("JSON_Viewer_Template");"[json_goes_here]";tJSON)
$tName:="json-"+String(Milliseconds)+".html"

tJSON_Display_Path:=File_Create ($tName;$tHTML;"Temp_Folder")

  // Open window and display object.
C_LONGINT($lWin)
$lWin:=Open form window("JSON_Viewer";Plain form window)
DIALOG("JSON_Viewer")
CLOSE WINDOW

  // Clean up stuff left around.
File_Cleanup (tJSON_Display_Path)
CLEAR VARIABLE(tJSON_Display_Path)
CLEAR VARIABLE(tJSON)


**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to