Skip to content

Commit

Permalink
refactor don't invoke the mapper's serialize method for the Rangequer…
Browse files Browse the repository at this point in the history
…y Jsondata raw value

Signed-off-by: Leo <[email protected]>
  • Loading branch information
Leo committed Nov 19, 2024
1 parent 76aae1f commit fcb398d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public <T> T deserialize(JsonpDeserializer<T> deserializer, JsonpMapper mapper)
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
if (value instanceof JsonValue) {
generator.write((JsonValue) value);
} else if (value instanceof String) {
generator.write((String) value);
} else if (value instanceof Integer) {
generator.write((Integer) value);
} else if (value instanceof Double) {
generator.write((Double) value);
} else {
// Mapper provided at creation time has precedence
(this.mapper != null ? this.mapper : mapper).serialize(value, generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,15 @@ public <T> void serialize(T value, JsonGenerator generator) {
((JsonpSerializable) value).serialize(generator, this);
return;
}

if (value instanceof String) {
generator.write((String) value);
return;
}

if (value instanceof Integer) {
generator.write((Integer) value);
return;
}

if (value instanceof Double) {
generator.write((Double) value);
return;
}

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 Expand Up @@ -183,10 +167,10 @@ public static <T> void serialize(T value, JsonGenerator generator, @Nullable Jso
* (the object has been consumed from the original one).
*/
public static Map.Entry<String, JsonParser> lookAheadFieldValue(
String name,
String defaultValue,
JsonParser parser,
JsonpMapper mapper
String name,
String defaultValue,
JsonParser parser,
JsonpMapper mapper
) {
JsonLocation location = parser.getLocation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,21 @@ public void setChildren(List<SomeClass> children) {
public void testRangeQuery() {

String expectedStringValue =
"{\"aggregations\":{},\"query\":{\"range\":{\"rangeField\":{\"gte\":10.5,\"lte\":30,\"from\":\"2024-01-01T00:00:00Z\",\"format\":\"strict_date_optional_time\"}}},\"terminate_after\":5}";
"{\"aggregations\":{},\"query\":{\"range\":{\"rangeField\":{\"gte\":10.5,\"lte\":30,\"from\":\"2024-01-01T00:00:00Z\",\"format\":\"strict_date_optional_time\"}}},\"terminate_after\":5}";

SearchRequest searchRequest = SearchRequest.of(
request -> request.index("index1", "index2")
.aggregations(Collections.emptyMap())
.terminateAfter(5L)
.query(
q -> q.range(
r -> r.field("rangeField")
.gte(JsonData.of(10.5))
.lte(JsonData.of(30))
.from(JsonData.of("2024-01-01T00:00:00Z"))
.format("strict_date_optional_time")
)
)
request -> request.index("index1", "index2")
.aggregations(Collections.emptyMap())
.terminateAfter(5L)
.query(
q -> q.range(
r -> r.field("rangeField")
.gte(JsonData.of(10.5))
.lte(JsonData.of(30))
.from(JsonData.of("2024-01-01T00:00:00Z"))
.format("strict_date_optional_time")
)
)
);
String searchRequestString = searchRequest.toJsonString();
assertEquals(expectedStringValue, searchRequestString);
Expand Down

0 comments on commit fcb398d

Please sign in to comment.