Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Syntax

Code Block
Result = SRP_JSONJson(Handle, "Parse", JsonString)

Returns

Returns "" if successful or an error message if not.

Parameters

ParameterDescription
Handle [OUT]Variable to receive new handle to a JSON Entity. Required.
JsonStringThe JSON to parse. Required.

Remarks

The PARSE service parses a string containing standardized JSON and returns a single JSON Entity Handle. If the JSON is not properly formatted, then this service returns an error message indicating how the parsing failed. If it succeeds, then "" is returns. The new JSON Entity Handle is placed into the variable you pass into the Handle parameter. It is perfectly safe to set Handle to an unassigned variable, but you should check the return value to see if the parsing was successfully, like so:

Code Block
Result = SRP_JSONJson(ObjectHandle, "PARSEParse", SampleJSON)
If Result NE "" then
   Msg(@Window, Result:@FM:@FM:@FM:"!")
end

The new JSON entity's type depends upon the nature of the JSON. Here is some sample code testing each entity's type after parsing some JSON:

Code Block
If SRP_JSONJson(ObjectHandle, "PARSEParse", '{"id": "Open"}') EQ "" then
   Type = SRP_JSONJson(ObjectHandle, "TYPE") ; // Type is "Object"
   SRP_JSONJson(ObjectHandle, "RELEASE")
end

If SRP_JSONJson(ObjectHandle, "PARSEParse", '["1", "2", "3"]') EQ "" then
   Type = SRP_JSONJson(ObjectHandle, "TYPE") ; // Type is "Array"
   SRP_JSONJson(ObjectHandle, "RELEASE")
end

If SRP_JSONJson(ObjectHandle, "PARSEParse", '"Hello, World!"') EQ "" then
   Type = SRP_JSONJson(ObjectHandle, "TYPE") ; // Type is "String"
   SRP_JSONJson(ObjectHandle, "RELEASE")
end

If SRP_JSONJson(ObjectHandle, "PARSEParse", '1') EQ "" then
   Type = SRP_JSONJson(ObjectHandle, "TYPE") ; // Type is "Number"
   SRP_JSONJson(ObjectHandle, "RELEASE")
end

If SRP_JSONJson(ObjectHandle, "PARSEParse", 'false') EQ "" then
   Type = SRP_JSONJson(ObjectHandle, "TYPE") ; // Type is "Boolean"
   SRP_JSONJson(ObjectHandle, "RELEASE")
end

IMPORTANT: Any JSON entity created by this service must be deallocated from memory when no longer needed using the RELEASERelease service. Forgetting to do this on occasion will not cause a fatal error since SRP Utilities will clean up all its memory when OpenInsight closes, but frequently failing to release entities causes memory to get used up, which can become a problem over long periods of time.

Example

Code Block
// Test parsing an existing JSON string and then drilling down into specific values
SampleJSON  = '{"menu": {'
SampleJSON := '    "header": "SVG Viewer",'
SampleJSON := '    "items": ['
SampleJSON := '        {"id": "Open"},'
SampleJSON := '        {"id": "OpenNew", "label": "Open New"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "ZoomIn", "label": "Zoom In"},'
SampleJSON := '        {"id": "ZoomOut", "label": "Zoom Out"},'
SampleJSON := '        {"id": "OriginalView", "label": "Original View"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "Quality"},'
SampleJSON := '        {"id": "Pause"},'
SampleJSON := '        {"id": "Mute"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "Find", "label": "Find..."},'
SampleJSON := '        {"id": "FindAgain", "label": "Find Again"},'
SampleJSON := '        {"id": "Copy"},'
SampleJSON := '        {"id": "CopyAgain", "label": "Copy Again"},'
SampleJSON := '        {"id": "CopySVG", "label": "Copy SVG"},'
SampleJSON := '        {"id": "ViewSVG", "label": "View SVG"},'
SampleJSON := '        {"id": "ViewSource", "label": "View Source"},'
SampleJSON := '        {"id": "SaveAs", "label": "Save As"},'
SampleJSON := '        null,'
SampleJSON := '        {"id": "Help"},'
SampleJSON := '        {"id": "About", "label": "About Adobe CVG Viewer..."}'
SampleJSON := '    ],'
SampleJSON := '    "testbool": "true",'
SampleJSON := '    "testint": "1234567890",'
SampleJSON := '    "testreal": "1234567890.987654321"'
SampleJSON := '}}'

// Parse it
If SRP_JSONJson(ObjectHandle, "PARSEParse", SampleJSON) EQ "" then
   
   // Grab some values
   TestValue = SRP_JSONJson(ObjectHandle, "GETVALUEGetValue", "menu.items[1].id")
   TestValue = SRP_JSONJson(ObjectHandle, "GETVALUEGetValue", "menu.items[12].label")
   TestValue = SRP_JSONJson(ObjectHandle, "GETVALUEGetValue", "menu.items[3]")
   TestValue = SRP_JSONJson(ObjectHandle, "GETVALUEGetValue", "menu.testbool")
   TestValue = SRP_JSONJson(ObjectHandle, "GETVALUEGetValue", "menu.testint")
   TestValue = SRP_JSONJson(ObjectHandle, "GETVALUEGetValue", "menu.testreal")
   
   // Release the object because we're all done
   SRP_JSONJson(ObjectHandle, "RELEASERelease")

end

See Also

NEWNewRELEASERelease