Using the JSON plug-in
You can use the JSON plug-in to convert EPL objects to JSON strings, and vice versa. When converting from JSON strings, JSON objects are converted to dictionary<any, any> and JSON lists are converted to sequence<any>.
To use the JSON plug-in, add the
JSON Support EPL bundle to your Apama project. For details, see
Adding EPL bundles to projects or
Creating and managing an Apama project from the command line.
The JSON plug-in is provided as a JSONPlugin event in the com.apama.json package. The JSONPlugin event provides the following actions:
To convert a JSON string to an EPL object:
fromJSON(string) returns any
To convert an EPL object (including events, dictionaries and sequences) to a JSON string:
toJSON(any) returns string
For detailed information, see the
API Reference for EPL (ApamaDoc) .
The following is a simple example:
using com.apama.json.JSONPlugin;
event Address {
integer houseNumber;
sequence<string> address;
optional<string> postcode;
}
monitor PrintJSON {
action onload() {
Address a1 := new Address;
Address a2 := new Address;
a1.houseNumber := 107;
a1.address.append("The Rounds");
a1.address.append("Milton");
a1.postcode := "CB1 2AB";
a2.houseNumber := 196;
a2.address.append("Exeter Road");
a2.address.append("Newmarket");
print JSONPlugin.toJSON(a1);
print JSONPlugin.toJSON(a2);
}
}
The above example prints the following:
{"postcode":"CB1 2AB","address":["The Rounds","Milton"],"houseNumber":107}
{"postcode":null,"address":["Exeter Road","Newmarket"],"houseNumber":196}
Equivalent functionality is available with the JSON codec connectivity plug-in. Use the JSON codec if the JSON is coming in as an entire message of a connectivity chain. Using a codec helps to separate the application logic from the connection over which the data is being sent, and allows the use of mapping codecs if needed. See
The JSON codec connectivity plug-in for detailed information.
Use the JSON EPL plug-in if only one field of an event is JSON, or if the events are going to or are coming from a connection that is not using a connectivity chain.