-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jai2305 <[email protected]>
- Loading branch information
Showing
8 changed files
with
135 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
java-client/src/main/java/org/opensearch/client/json/PlainDeserializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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.JsonParser; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
|
||
/** Base interface to set JSON properties **/ | ||
|
||
public interface PlainDeserializable<B> { | ||
|
||
B get(); | ||
|
||
/** Updates object with newly provided JSON properties | ||
@param parser the JsonParser parser | ||
@param mapper the JsonpMapper mapper used to deserialize values | ||
@return object | ||
**/ | ||
|
||
default B withJson(JsonParser parser, JsonpMapper mapper) { | ||
JsonpDeserializer<?> deserializer = JsonpMapperBase.findDeserializer(this.getClass().getEnclosingClass()); | ||
@SuppressWarnings("unchecked") | ||
ObjectDeserializer<B> objectDeserializer = (ObjectDeserializer<B>) DelegatingDeserializer.unwrap(deserializer); | ||
assert objectDeserializer != null; | ||
return objectDeserializer.deserialize(get(), parser, mapper, parser.next()); | ||
} | ||
|
||
/** Updates object with newly provided JSON properties | ||
@param inputStream the stream to read from | ||
@return this object | ||
* **/ | ||
default B withJson(InputStream inputStream) { | ||
JsonpMapper defaultMapper = JsonpUtils.DEFAULT_JSONP_MAPPER; | ||
return withJson(defaultMapper.jsonProvider().createParser(inputStream), defaultMapper); | ||
} | ||
|
||
/** Updates object with newly provided JSON properties | ||
@param reader the stream to read from | ||
@return this object | ||
* **/ | ||
default B withJson(Reader reader) { | ||
JsonpMapper defaultMapper = JsonpUtils.DEFAULT_JSONP_MAPPER; | ||
JsonParser parser = defaultMapper.jsonProvider().createParser(reader); | ||
return withJson(parser, defaultMapper); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
java-client/src/test/java/org/opensearch/client/opensearch/json/PlainDeserializableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.opensearch.json; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
public class PlainDeserializableTest { | ||
|
||
@Test | ||
public void testWithJsonPutIndexTemplateRequest() { | ||
|
||
String stringTemplate = | ||
"{\"name\":\"My index\",\"composed_of\":[\"component1\",\"component2\"],\"index_patterns\":[\"index_pattern1\"]}"; | ||
InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8)); | ||
|
||
PutIndexTemplateRequest indexTemplateRequest = new PutIndexTemplateRequest.Builder().name("My index 2") | ||
.indexPatterns("index_pattern3") | ||
.withJson(inputStream) | ||
.build(); | ||
|
||
String expectedName = "My index"; // My index 2 will be replaced by My index from inputStream | ||
List<String> expectedIndexPatterns = List.of("index_pattern3", "index_pattern1"); | ||
List<String> expectedComponents = List.of("component1", "component2"); | ||
|
||
assert expectedName.equals(indexTemplateRequest.name()); | ||
assert expectedIndexPatterns.equals(indexTemplateRequest.indexPatterns()); | ||
assert expectedComponents.equals(indexTemplateRequest.composedOf()); | ||
|
||
} | ||
} |