JSON Support
Starting with release 8.5.201.04, Platform SDK for Java has been extended with functionality for serialization and deserialization of protocol messages to JSON string representation. (For older 8.5.201.x releases, refer to the Legacy Content section at the bottom of this article.)
Design
The serializer supports two different types of JSON representations for Platform SDK protocol messages: with MessageName attribute, and without it.
Adding the MessageName attribute helps users to deserialize a protocol message when its type is not known from context. Deserialization without the inner MessageName attribute can be used to support existing solutions, or to avoid duplicating some request context data in order to optimize network traffic, CPU, and memory usage.
Examples
Using the Platform SDK JSON Message Serializer
Example 1: Message Deserialization (with messageName in JSON)
PsdkJsonSerializer ser = PsdkJsonSerializer.createContactServerSerializer();
String json = "{ \"messageName\":\"RequestRefresh\", "
+ "\"query\":\"test-Query-4\", \"file\":\"test-File-3\",\"indexName\":\"Contact\", \"persistents\":\"test-Persistents-2\"}";
Message message = ser.deserialize(json);
Example 2: Message Deserialization (Without messageName in JSON)
PsdkJsonSerializer ser = PsdkJsonSerializer.createContactServerSerializer();
String json = "{ \"query\":\"test-Query-4\", \"file\":\"test-File-3\",\"indexName\":\"Contact\", \"persistents\":\"test-Persistents-2\"}";
Message message = ser.deserialize(json, "RequestRefresh");
// RequestRefresh message = ser.deserialize(json, RequestRefresh.class);
Example 3: Message Serialization (With messageName in JSON)
PsdkJsonSerializer ser = PsdkJsonSerializer.createContactServerSerializer();
RequestRefresh request = RequestRefresh.create();
request.setQuery("test-Query-4");
request.setFile("test-File-3");
request.setIndexName(IndexNameType.Contact);
request.setPersistents("test-Persistents-2");
String json = ser.serialize(request);
Using the Jackson Framework
Example 1: Message Deserialization (with messageName in JSON)
ObjectMapper m = new ObjectMapper();
m.registerModule(new ContactServerModule(true));
String json = "{ \"messageName\":\"RequestRefresh\", "
+ "\"query\":\"test-Query-4\", \"file\":\"test-File-3\",\"indexName\":\"Contact\", \"persistents\":\"test-Persistents-2\"}";
Message message = (Message)m.readValue(json, ContactServerMessage.class);
Example 2: Message Deserialization (Without messageName in JSON)
ObjectMapper m = new ObjectMapper();
PSDKCommonModule mod = new ContactServerModule();
m.registerModule(mod);
String json = "{ \"query\":\"test-Query-4\", \"file\":\"test-File-3\",\"indexName\":\"Contact\", \"persistents\":\"test-Persistents-2\"}";
Message message = m.readValue(json, mod.getMessageClass("RequestRefresh"));
//RequestRefresh message = m.readValue(json, RequestRefresh.class);
Example 3: Message Serialization (Without messageName in JSON)
ObjectMapper m = new ObjectMapper();
m.registerModule(new ContactServerModule());
RequestRefresh request = RequestRefresh.create();
request.setQuery("test-Query-4");
request.setFile("test-File-3");
request.setIndexName(IndexNameType.Contact);
request.setPersistents("test-Persistents-2");
String json = m.writeValueAsString(request);
// We expect to get JSON like the following:
// "{ "\"query\":\"test-Query-4\", \"file\":\"test-File-3\",\"indexName\":\"Contact\", \"persistents\":\"test-Persistents-2\"}";
Example 4: Message Serialization (With messageName in JSON)
ObjectMapper m = new ObjectMapper();
m.registerModule(new ContactServerModule(true));
RequestRefresh request = RequestRefresh.create();
request.setQuery("test-Query-4");
request.setFile("test-File-3");
request.setIndexName(IndexNameType.Contact);
request.setPersistents("test-Persistents-2");
String json = m.writeValueAsString(request);
// We expect to get JSON like the following:
// "{ \"messageName\":\"RequestRefresh\", "
// + "\"query\":\"test-Query-4\", \"file\":\"test-File-3\",\"indexName\":\"Contact\", \"persistents\":\"test-Persistents-2\"}";
Legacy Content
Prior to release 8.5.201.04, Platform SDK for Java did not offer native support for JSON - only XML serialization was supported. This section describes how provide JSON support within your Platform SDK for Java applications for legacy applications that use JSON format for data.
[+] Display Legacy ContentStarting with release 8.5.201.04, Platform SDK for .NET has been extended with functionality for serialization and deserialization of protocol messages to JSON string representation.
Design
The serializer supports two different types of JSON representations for Platform SDK protocol messages: with MessageName attribute, and without it.
Adding the MessageName attribute helps users to deserialize a protocol message when its type is not known from context. Deserialization without the inner MessageName attribute can be used to support existing solutions, or to avoid duplicating some request context data in order to optimize network traffic, CPU, and memory usage.
Examples
Example 1: Bi-directional JSON Serialization
public void TestRequestGetContacts()
{
var req = RequestGetContacts.Create();
req.ReferenceId = 1;
req.SearchCriteria = new SearchCriteriaCollection();
req.SortCriteria = new SortCriteriaCollection();
var json = PsdkJsonSerializer.Serialize(req);
Console.WriteLine(json);
var newRq = PsdkJsonSerializer.Deserialize(req.GetType(), json);
var json2 = PsdkJsonSerializer.Serialize(newRq);
Console.WriteLine(json2);
Assert.IsTrue(json.Equals(json2));
}
Example 2: Using Platform SDK JSON Serializer for UCS Protocol
public void TestRequestGetVersion()
{
var request = RequestGetVersion.Create();
request.ReferenceId = 1;
Console.WriteLine(request);
var serializer = JsonSerializationFactory.GetSerializer<UniversalContactServerProtocol>();
var json = serializer.Serialize(request);
Console.WriteLine(json);
var deserializedMessage = serializer.Deserialize(json);
Console.WriteLine(deserializedMessage);
Assert.IsTrue(request.Equals(deserializedMessage));
}
Example 3: Sample JSON Object
Message:
'RequestRefresh' ('60') message attributes: Query = test-Query-4 File = test-File-3 IndexName = Contact Persistents = test-Persistents-2
JSON Representation:
{"messageName":"RequestRefresh","query":"test-Query-4","file":"test-File-3","indexName":"Contact","persistents":"test-Persistents-2"}
Java and .NET Compatibility Note
The internal implementation of messages from the Configuration Server protocol differs for .NET and Java platforms. Data objects in .NET messages are based on the XDocument class, while Java message use data classes for mapping of data objects. This means that the result of serialization for the same messages from Java and .NET platforms will be different for the Configuration Server protocol (although bi-directional serialization is supported for both platforms).
All other Platform SDK protocols support cross-platform serialization.