Article
· Mar 4 2m read

Customize how messages are displayed in Message Viewer

I would like to share with you a little trick to customize how messages are displayed in the Message Viewer. In particular, how you can display messages as JSON (instead of the default XML representation).

image

Messages are objects that are used to communicate interoperability productions components. In my example I have defined a message that later I serialize as JSON to send to an external API. This message is defined as a regular message and also as a %JSON.Adaptor so I can export / import directly to or from JSON.

Class interop.msg.DeviceOrderReq Extends (Ens.Request, %JSON.Adaptor)
{

Parameter %JSONNULL As BOOLEAN = 1;

Property externalOrderId As %String(MAXLEN = "");

Property orderStatus As %String;

Property requestedServiceId As %String(MAXLEN = "");

Property patientData As interop.msg.elements.PatientData;

}

This message is working fine when I run different tests in my interoperability production. However in the Message Viewer it is displayed as an XML (this is the default representation).

image

This representation is fine, but in this case it would be much more intuitive for me being able to see the contents as JSON. Luckily, you can override the %ShowContents method in the message.

In my case, I've created a class called JSONMessage (so I can later re-use that). This class overrides the %ShowContents method to display the message as a formatted JSON.

Class interop.msg.JSONMessage Extends (Ens.Request, %JSON.Adaptor)
{

/// This method is called by the Management Portal to determine the content type that will be returned by the <method>%ShowContents</method> method.
/// The return value is a string containing an HTTP content type.
Method %GetContentType() As %String
{
    Quit "text/html"
}

/// This method is called by the Management Portal to display a message-specific content viewer.<br>
/// This method displays its content by writing out to the current device.
/// The content should match the type returned by the <method>%GetContentType</method> method.<br>
Method %ShowContents(pZenOutput As %Boolean = 0)
{
   do ..%JSONExportToString(.jsonExport)
    set formatter = ##class(%JSON.Formatter).%New()
    do formatter.FormatToString(jsonExport, .json)
    &html<<pre>#(json)#</pre>>
}

}

Finally, you only need to change the original message so it extends from JSONMessage:

Class interop.msg.DeviceOrderReq Extends (JSONMessage, Ens.Request)
{

Parameter %JSONNULL As BOOLEAN = 1;

Property externalOrderId As %String(MAXLEN = "");

Property orderStatus As %String;

Property requestedServiceId As %String(MAXLEN = "");

Property patientData As interop.msg.elements.PatientData;

}
Discussion (2)3
Log in or sign up to continue

Thanks @Alberto Fuentes 
I suggest to keep information regarding the classname of the message and its ID :

Method %ShowContents(pZenOutput As %Boolean = 0)
{
   do ..%JSONExportToString(.jsonExport)
    set formatter = ##class(%JSON.Formatter).%New()
    do formatter.FormatToString(jsonExport, .json)
    &html<
        <i style="color:green;">type: #($classname())#  id: #(..%Id())# </i>
        <pre>#(json)#</pre>
    >
}

 in order to get :