Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serializeToString() method #213

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public <T> void serialize(T value, JsonGenerator generator) {
mapper.serialize(value, generator);
}

@Override
public <T> String serializeToString(T value) {
return mapper.serializeToString(value);
}

@Override
public boolean ignoreUnknownFields() {
return mapper.ignoreUnknownFields();
Expand Down
14 changes: 14 additions & 0 deletions java-client/src/main/java/co/elastic/clients/json/JsonpMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

package co.elastic.clients.json;

import co.elastic.clients.json.jackson.JacksonJsonProvider;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;

import javax.annotation.Nullable;
import java.io.StringWriter;

/**
* A {@code JsonpMapper} combines a JSON-P provider and object serialization/deserialization based on JSON-P events.
Expand Down Expand Up @@ -51,6 +54,17 @@ public interface JsonpMapper {
*/
<T> void serialize(T value, JsonGenerator generator);

/**
* Serialize an object to its JSON String representation
*/
default <T> String serializeToString(T value) {
StringWriter writer = new StringWriter();
try (JsonGenerator generator = jsonProvider().createGenerator(writer)) {
serialize(value, generator);
};
return writer.toString();
}

/**
* Should object parsers in the API client be lenient and silently ignore unknown fields?
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

package co.elastic.clients.json;

import jakarta.json.JsonException;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;

public abstract class JsonpMapperBase implements JsonpMapper {
Expand Down Expand Up @@ -76,6 +80,24 @@ public static <T> JsonpSerializer<T> findSerializer(T value) {
return null;
}

@Override
public <T> String serializeToString(T value) {
JsonpSerializer<T> serializer = findSerializer(value);

if (serializer != null) {
StringWriter writer = new StringWriter();
JsonGenerator generator = jsonProvider().createGenerator(writer);
serializer.serialize(value, generator, this);
generator.close();
return writer.toString();
} else {
throw new JsonException(
"Cannot find a serializer for type " + value.getClass().getName() +
". Consider using a full-featured JsonpMapper"
);
}
}

protected static class JsonpSerializableSerializer<T extends JsonpSerializable> implements JsonpSerializer<T> {
@Override
public void serialize(T value, JsonGenerator generator, JsonpMapper mapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
package co.elastic.clients.elasticsearch.json;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.TotalHitsRelation;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.json.DelegatingDeserializer;
import co.elastic.clients.json.JsonData;
Expand All @@ -40,6 +44,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;

public class JsonDataTest extends Assert {

Expand Down Expand Up @@ -125,4 +130,38 @@ public void testConvert() {
assertEquals(JsonValue.ValueType.STRING, value.getValueType());
assertEquals("foo", ((JsonString)value).getString());
}

@Test
public void testIndexResponse() {
IndexResponse resp = IndexResponse.of(r -> r
.index("foo")
.id("1")
.primaryTerm(1)
.seqNo(1)
.version(1)
.shards(s -> s.successful(1).failed(0).total(1))
.result(Result.Created)
);

String json = new JacksonJsonpMapper().serializeToString(resp);

assertEquals("{\"_id\":\"1\",\"_index\":\"foo\",\"_primary_term\":1,\"result\":\"created\",\"_seq_no\":1,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0},\"_version\":1}", json);
}

@Test
public void testSearchResponse() {
SearchResponse<?> resp = SearchResponse.searchResponseOf(r -> r
.took(10)
.shards(s -> s.successful(1).failed(0).total(1))
.timedOut(false)
.hits(h -> h
.total(t -> t.value(0).relation(TotalHitsRelation.Eq))
.hits(Collections.emptyList())
)
);

String json = new JacksonJsonpMapper().serializeToString(resp);

assertEquals("{\"took\":10,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0},\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}}", json);
}
}