Skip to content

Commit

Permalink
588 - [FEATURE] serialize requests to JSON (opensearch-project#1064)
Browse files Browse the repository at this point in the history
* 588 - [FEATURE] serialize requests to JSON

Signed-off-by: Jai2305 <[email protected]>

* 588 - [ENHANCEMENT] New interface PlainJsonSerializable and optimizing imports

Signed-off-by: Jai2305 <[email protected]>

---------

Signed-off-by: Jai2305 <[email protected]>
  • Loading branch information
Jai2305 authored Jul 14, 2024
1 parent efe41e5 commit df46d73
Show file tree
Hide file tree
Showing 599 changed files with 1,358 additions and 1,192 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Added
- Document HTTP/2 support ([#330](https://github.com/opensearch-project/opensearch-java/pull/330))
- Add support for phase_took & search_pipeline request params ([#1036](https://github.com/opensearch-project/opensearch-java/pull/1036))
- Add an interface PlainJsonSerializable inherit from JsonpSerializable with a default method streamlining serialization ([#1064](https://github.com/opensearch-project/opensearch-java/pull/1064))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@

package org.opensearch.client.json;

import jakarta.json.JsonException;
import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonLocation;
import jakarta.json.stream.JsonParser;
Expand All @@ -59,6 +61,39 @@ public class JsonpUtils {
* JSON when advancing to next state.
* @throws java.util.NoSuchElementException if there are no more parsing states.
*/

static final JsonProvider DEFAULT_PROVIDER = provider();

static JsonProvider provider() {
return JsonProvider.provider();
}

static final JsonpMapper DEFAULT_JSONP_MAPPER = new JsonpMapperBase() {
@Override
public JsonProvider jsonProvider() {
return DEFAULT_PROVIDER;
}

@Override
public <T> void serialize(T value, JsonGenerator generator) {
if (value instanceof JsonpSerializable) {
((JsonpSerializable) value).serialize(generator, this);
return;
}

throw new JsonException(
"Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper."
);
}

@Override
protected <T> JsonpDeserializer<T> getDefaultDeserializer(Class<T> clazz) {
throw new JsonException(
"Cannot find a default deserializer for type " + clazz.getName() + ". Consider using a full-featured JsonpMapper."
);
}
};

public static JsonParser.Event expectNextEvent(JsonParser parser, JsonParser.Event expected) {
JsonParser.Event event = parser.next();
expectEvent(parser, expected, event);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.json;

import jakarta.json.stream.JsonGenerator;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;

public interface PlainJsonSerializable extends JsonpSerializable {

/**
* A default method which returns string representation for the instances of classes
* implementing JsonpSerializable interface.<br>
* Usage : Eg for SearchRequest.class<pre>{@code SearchRequest implements PlainJsonSerializable{..}
* SearchRequest searchRequest = SearchRequest.of(request -> request...);
* String searchRequestString = searchRequest.writeValueAsString();}</pre> <br>
*
*/
default String toJsonString() {
try (StringWriter writer = new StringWriter()) {
try (JsonGenerator generator = JsonpUtils.DEFAULT_PROVIDER.createGenerator(writer)) {
serialize(generator, JsonpUtils.DEFAULT_JSONP_MAPPER);
}
return writer.toString();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import jakarta.json.stream.JsonGenerator;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.AcknowledgedResponseBase

public abstract class AcknowledgedResponseBase implements JsonpSerializable {
public abstract class AcknowledgedResponseBase implements PlainJsonSerializable {
private final boolean acknowledged;

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _spec_utils.BaseNode

public abstract class BaseNode implements JsonpSerializable {
public abstract class BaseNode implements PlainJsonSerializable {
private final Map<String, String> attributes;

private final String host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.BulkIndexByScrollFailure

@JsonpDeserializable
public class BulkIndexByScrollFailure implements JsonpSerializable {
public class BulkIndexByScrollFailure implements PlainJsonSerializable {
private final ErrorCause cause;

private final String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.BulkStats

@JsonpDeserializable
public class BulkStats implements JsonpSerializable {
public class BulkStats implements PlainJsonSerializable {
private final long totalOperations;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.ClusterStatistics

@JsonpDeserializable
public class ClusterStatistics implements JsonpSerializable {
public class ClusterStatistics implements PlainJsonSerializable {
private final int skipped;

private final int successful;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.CompletionStats

@JsonpDeserializable
public class CompletionStats implements JsonpSerializable {
public class CompletionStats implements PlainJsonSerializable {
private final long sizeInBytes;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.CoordsGeoBounds

@JsonpDeserializable
public class CoordsGeoBounds implements JsonpSerializable {
public class CoordsGeoBounds implements PlainJsonSerializable {
private final double top;

private final double bottom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.DocStats

@JsonpDeserializable
public class DocStats implements JsonpSerializable {
public class DocStats implements PlainJsonSerializable {
private final long count;

private final long deleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ObjectBuilder;

/**
* For empty Class assignments
*
*/
@JsonpDeserializable
public class EmptyObject implements JsonpSerializable {
public class EmptyObject implements PlainJsonSerializable {

public static final class Builder implements ObjectBuilder<EmptyObject> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ObjectBuilder;

@JsonpDeserializable
public class EmptyTransform implements JsonpSerializable {
public class EmptyTransform implements PlainJsonSerializable {

public static final class Builder implements ObjectBuilder<EmptyTransform> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;
Expand All @@ -57,7 +57,7 @@
* on the error type.
*/
@JsonpDeserializable
public class ErrorCause implements JsonpSerializable {
public class ErrorCause implements PlainJsonSerializable {
private final Map<String, JsonData> metadata;

private final String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.json.UnionDeserializer;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
Expand All @@ -53,7 +53,7 @@
*
*/
@JsonpDeserializable
public class ErrorResponse implements JsonpSerializable {
public class ErrorResponse implements PlainJsonSerializable {

private enum Kind {
OBJECT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.FieldMemoryUsage

@JsonpDeserializable
public class FieldMemoryUsage implements JsonpSerializable {
public class FieldMemoryUsage implements PlainJsonSerializable {
@Nullable
private final String memorySize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

// typedef: _types.FieldSizeUsage

@JsonpDeserializable
public class FieldSizeUsage implements JsonpSerializable {
public class FieldSizeUsage implements PlainJsonSerializable {
@Nullable
private final String size;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.opensearch._types.mapping.FieldType;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
Expand All @@ -49,7 +49,7 @@
// typedef: _types.FieldSort

@JsonpDeserializable
public class FieldSort implements JsonpSerializable {
public class FieldSort implements PlainJsonSerializable {
// Single key dictionary
private final String field;

Expand Down
Loading

0 comments on commit df46d73

Please sign in to comment.