Skip to content

Commit

Permalink
588 - [ENHANCEMENT] New interface PlainJsonSerializable and optimizin…
Browse files Browse the repository at this point in the history
…g imports

Signed-off-by: Jai2305 <[email protected]>
  • Loading branch information
Jai2305 committed Jul 9, 2024
1 parent ce514fc commit 5ff4a6f
Show file tree
Hide file tree
Showing 602 changed files with 1,262 additions and 1,225 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 a serializer method for classes implementing JsonpSerializable.([#1064](https://github.com/opensearch-project/opensearch-java/pull/1064))
- 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 @@ -43,9 +43,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 @@ -54,7 +54,7 @@

@JsonpDeserializable
@Generated("org.opensearch.client.codegen.CodeGenerator")
public class OpenSearchVersionInfo implements JsonpSerializable {
public class OpenSearchVersionInfo implements PlainJsonSerializable {

private final String buildDate;

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.opensearch._types.OpenSearchVersionInfo;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
Expand All @@ -54,7 +54,7 @@

@JsonpDeserializable
@Generated("org.opensearch.client.codegen.CodeGenerator")
public class InfoResponse implements JsonpSerializable {
public class InfoResponse implements PlainJsonSerializable {

private final String clusterName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,11 @@
package org.opensearch.client.json;

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

/**
* An object that is its own JsonP serializer
*/
public interface JsonpSerializable {

void serialize(JsonGenerator generator, JsonpMapper mapper);

/**
* 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 JsonpSerializable{..}
* 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 @@ -82,14 +82,14 @@ public <T> void serialize(T value, JsonGenerator generator) {
}

throw new JsonException(
"Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper"
"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"
"Cannot find a default deserializer for type " + clazz.getName() + ". Consider using a full-featured JsonpMapper."
);
}
};
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
Loading

0 comments on commit 5ff4a6f

Please sign in to comment.