Jump to: navigation, search

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.

Important
Each serializer instance should work with one protocol type. If you need to process different types (such as UCS and StatServer) then you must create one serializer per protocol.

Examples

Using the Platform SDK JSON Message Serializer

Tip
This serializer is designed to work with Platform SDK messages only. Serialization of other custom classes is not supported.

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 Content

Starting 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.

Important
Each serializer instance should work with one protocol type. If you need to process different types (such as UCS and StatServer) then you must create one serializer per protocol.

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.

This page was last edited on April 22, 2016, at 22:00.
Comments or questions about this documentation? Contact us for support!