Skip to content

Latest commit

 

History

History
96 lines (75 loc) · 4.01 KB

json.md

File metadata and controls

96 lines (75 loc) · 4.01 KB

Working With JSON

OpenSearch Java client seamlessly integrates with JSON, providing serialization and deserialization capability.

Serialization

For demonstration let's consider an instance of SearchRequest.

SearchRequest searchRequest = SearchRequest.of(
        request -> request.index("index1", "index2")
                .aggregations(Collections.emptyMap())
                .terminateAfter(5L)
                .query(q -> q.match(t -> t.field("name").query(FieldValue.of("OpenSearch"))))
);

Using toJsonString

For classes implementing PlainJsonSerializable, which extends JsonpSerializable, a default toJsonString method is provided. This implementation uses jakarta.json.spi.JsonProvider SPI to discover the available JSON provider instance from the classpath and to create a new mapper. The JsonpUtils utility class streamlines this serialization process. The following code example demonstrates how to use the toJsonString method to serialize objects:

String requestString = searchRequest.toJsonString();

Manual Serialization

For classes implementing the JsonpSerializable interface, a serialize method is provided, which takes a mapper and a generator as arguments and returns the JSON string representation of the instance.

The following sample code demonstrates how to serialize an instance of a Java class:

private String toJson(JsonpSerializable object) {
  try (StringWriter writer = new StringWriter()) {
    JsonbJsonpMapper mapper = new JsonbJsonpMapper();
    try (JsonGenerator generator = mapper.jsonProvider().createGenerator(writer)) {
      serialize(generator, mapper);
    }
    return writer.toString();
  } catch (IOException ex) {
    throw new UncheckedIOException(ex);
  }
}

Deserialization

For demonstration let's consider an IndexTemplateMapping JSON String.

String stringTemplate =
        "{\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"settings\":{\"number_of_shards\":\"2\",\"number_of_replicas\":\"1\"}}";

Using withJson

For classes, Builders of which implements PlainDeserializable interface, a default withJson method is provided. The withJson method returns the Builder enabling you to chain Builder methods for additional configuration. This implementation uses jakarta.json.spi.JsonProvider SPI to discover the available JSON provider instance from the classpath and to create a new mapper. The JsonpUtils utility class streamlines this deserialization process. The following code example demonstrates how to use the withJson method to deserialize objects:

InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8));
IndexTemplateMapping indexTemplateMapping = new IndexTemplateMapping.Builder().withJson(inputStream).build();

Using static _DESERIALIZER

For classes annotated with @JsonpDeserializable, a static field _DESERIALIZER is provided, which takes a mapper and a parser as arguments and returns the instance of the json value passed in the parser. Notice that this way you cannot further customize the instance, the state of which will solely depend on the json value parsed.

The following sample code demonstrates how to serialize an instance of a Java class:

private IndexTemplateMapping getInstance(String templateJsonString) {
  InputStream inputStream = new ByteArrayInputStream(templateJsonString.getBytes(StandardCharsets.UTF_8));
  JsonbJsonpMapper mapper = new JsonbJsonpMapper();
  try (JsonParser parser = mapper.jsonProvider().createParser(inputStream)) {
    IndexTemplateMapping indexTemplateMapping = new IndexTemplateMapping._DESERIALIZER.deserialize(parser, mapper);
    return indexTemplateMapping;
  }
}