Versions Compared

Key

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

...

When complete control at the lowest level is required, you'll want to use SRP_JSON to create your resource. As noted above, the handle returned by the GetObject service is compatible with the SRP_JSON function and vice-versa. Therefore, a developer can choose to start with either method and continue to use SRP_JSON member services and the higher level GetObject companion services (e.g., AddProperty, AddSubProperty, etc.) at will. One unique feature of SRP_JSON is its ability to interrogate the resource object using services like GetValue. This is useful when a resource object is generated elsewhere. Examples of how this can work will be documented in another article. For now, here is an example of how our sample simple resource could can be created primarily from using SRP_JSON:

Code Block
languagebp
API contacts.ID.GET

    KeyID           = EndpointSegment

    // Create a JSON object in memory.
    If SRP_JSON(objResource, 'New') then
        ContactRow  = Database_Services('ReadDataRow', 'CONTACTS', KeyID)
        If Error_Services('NoError') then
            SRP_JSON(objResource, 'SetValue', 'firstName', ContactRow<CONTACTS_FIRST_NAME$>, 'String')
            SRP_JSON(objResource, 'SetValue', 'lastName', ContactRow<CONTACTS_LAST_NAME$>, 'String')
            SRP_JSON(objResource, 'SetValue', 'address', ContactRow<CONTACTS_ADDRESS$>, 'String')
            SRP_JSON(objResource, 'SetValue', 'city', ContactRow<CONTACTS_CITY$>, 'String')
            SRP_JSON(objResource, 'SetValue', 'state', ContactRow<CONTACTS_STATE$>, 'String')
            SRP_JSON(objResource, 'SetValue', 'zipCode', ContactRow<CONTACTS_ZIP$>, 'String')
            // Serialize the JSON object and release the object from memory.
            jsonResource    = SRP_JSON(objResource, 'Stringify', 'Fast')
            SRP_JSON(objResource, 'Release')
            // Set the response body with the serialized JSON object and set the Content-Type response header.
            HTTP_Services('SetResponseBody', jsonResource, False$, 'application/hal+json')
        end else
            // There is an error reading the CONTACTS row. Probably a non-existent row.
            HTTP_Services('SetResponseError', '', '', 404, 'Contact ' : KeyID : ' does not exist.', FullEndpointURL)
        end
    end else
        // There is an error condition so call the SetResponseError service.
        HTTP_Services('SetResponseError', '', '', 500, 'Unable to create JSON object', FullEndpointURL)
    end

end api

...