Publishing a JSON subject from a DataSource

The page provides an overview of how to publish a subject of type JSON from a DataSource.

The instructions on this page assume you already know how to create a DataSource project. For more information on creating a Java DataSource, see the Caplin Platform Developer Tutorial 1.

Requirements

To use the JSON data type, your Caplin components and libraries must be at least as recent as the versions listed in the table below:

Product versions supporting the JSON data type
Product Minimum Version

C DataSource API

7.1.9

Java DataSource API

7.1.4

.NET DataSource API

7.1.9

Liberator

7.1.8

Transformer

7.1.5

StreamLink iOS

7.1.0

StreamLink Android

7.1.1

StreamLink Java

7.1.1

StreamLink JS

7.1.1

StreamLink .NET

7.1.1

Overview

JSON messages in the DataSource API are objects of type JsonMessage, which you create using the factory methods below:

  • Image messages: MessageFactory.createJsonImageMessage

  • Update messages: MessageFactory.createJsonPatchMessage

For JSON documents below 500KB in size, Liberator automatically chooses the optimal message type (image or update) to stream JSON data, and may override your choice of message type.

For JSON documents over 500KB in size, Liberator does not override your choice of message type, and we recommend that you determine the most efficient message type (image or update) before publishing JSON data from a DataSource. In most cases, update is the most efficient message type, but when the changes are extensive, a JSON image message can be smaller than a JSON update message.

Publishing a JSON image message

To publish a JSON image message, follow the steps below:

  1. Build the JSON document to send as the image. The example below uses the FasterXML Jackson library to build the JSON:

    com.fasterxml.jackson.databind.ObjectMapper mapper =
      new com.fasterxml.jackson.databind.ObjectMapper();
    com.fasterxml.jackson.databind.node.ObjectNode objectNode =
      mapper.createObjectNode();
    objectNode.put("priceId", "a2945fcde624fe84935e1b97002ac5a6");
    objectNode.put("bid", "1.30");
    objectNode.put("ask", "1.32");
    objectNode.put("currencyPair", "GBPUSD");
    
    String json = objectNode.toString();
  2. Create a JsonMessage object:

    com.caplin.datasource.messaging.json.JsonMessage message =
      publisher.getMessageFactory().createJsonImageMessage(subject, json);
  3. Publish the JsonMessage object:

    publisher.publishToSubscribedPeers(message);

Publishing a JSON update message

To publish a JSON update message, follow the steps below:

  1. Build the IETF RFC 6902 JSON patch to send as the update. The example below uses the FasterXML Jackson library to build the JSON:

    com.fasterxml.jackson.databind.ObjectMapper mapper =
      new com.fasterxml.jackson.databind.ObjectMapper();
    com.fasterxml.jackson.databind.node.ArrayNode arrayNode =
      mapper.createArrayNode();
    com.fasterxml.jackson.databind.node.ObjectNode objectNode =
      mapper.createObjectNode();
    objectNode.put("op", "replace");
    objectNode.put("path", "/ask");
    objectNode.put("value", "1.33");
    arrayNode.add(objectNode);
    
    String json = arrayNode.toString();
  2. Create a JsonMessage object:

    com.caplin.datasource.messaging.json.JsonMessage message =
      publisher.getMessageFactory().createJsonUpdateMessage(subject, json);
  3. Publish the JsonMessage object:

    publisher.publishToSubscribedPeers(message);

See also: