Skip to content

Commit

Permalink
Include boolean support in existing JsonEnum.Deserializer
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Schlindwein <patrick.schlindwein@bolindalabs.com>
  • Loading branch information
patschl authored and Patrick Schlindwein committed May 17, 2023
1 parent f630192 commit b372d24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
63 changes: 23 additions & 40 deletions java-client/src/main/java/org/opensearch/client/json/JsonEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

/**
* Base interface for enumerations in API types. Members have a JSON representation and also accept
* aliases when parsed from a string value.
* aliases when parsed from a string value. For some enumerations primitive boolean values are also supported.
*/
public interface JsonEnum extends JsonpSerializable {
String jsonValue();
Expand All @@ -62,14 +62,18 @@ default void serialize(JsonGenerator generator, JsonpMapper params) {
class Deserializer<T extends JsonEnum> extends JsonpDeserializerBase<T> {
private final Map<String, T> lookupTable;

public Deserializer(T[] values) {
this(values, EnumSet.of(JsonParser.Event.VALUE_STRING, JsonParser.Event.KEY_NAME));
}
private static final EnumSet<JsonParser.Event> ACCEPTED_EVENTS = EnumSet.of(
JsonParser.Event.VALUE_STRING,
JsonParser.Event.KEY_NAME,
JsonParser.Event.VALUE_TRUE,
JsonParser.Event.VALUE_FALSE
);

private static final EnumSet<JsonParser.Event> NATIVE_EVENTS = EnumSet.of(JsonParser.Event.VALUE_STRING);

private Deserializer(T[] values, EnumSet<JsonParser.Event> acceptedEvents) {
super(acceptedEvents, EnumSet.of(JsonParser.Event.VALUE_STRING));
public Deserializer(T[] values) {
super(ACCEPTED_EVENTS, NATIVE_EVENTS);

// Use the same size calculation as in java.lang.Enum.enumConstantDirectory
this.lookupTable = new HashMap<>((int) (values.length / 0.75f) + 1);
for (T member : values) {
this.lookupTable.put(member.jsonValue(), member);
Expand All @@ -84,7 +88,18 @@ private Deserializer(T[] values, EnumSet<JsonParser.Event> acceptedEvents) {

@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
String value = parser.getString();
String value;
switch (event) {
case VALUE_TRUE:
value = "true";
break;
case VALUE_FALSE:
value = "false";
break;
default:
value = parser.getString();
}

return deserialize(value, parser);
}

Expand Down Expand Up @@ -119,36 +134,4 @@ public T parse(String value) {
return result;
}
}

class DeserializerSupportingBooleans<T extends JsonEnum> extends Deserializer<T> {

private static final EnumSet<JsonParser.Event> ACCEPTED_EVENTS = EnumSet.of(
JsonParser.Event.VALUE_STRING,
JsonParser.Event.KEY_NAME,
JsonParser.Event.VALUE_TRUE,
JsonParser.Event.VALUE_FALSE
);

public DeserializerSupportingBooleans(T[] values) {
super(values, ACCEPTED_EVENTS);
}

@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
String value;

switch (event) {
case VALUE_TRUE:
value = "true";
break;
case VALUE_FALSE:
value = "false";
break;
default:
value = parser.getString();
}

return deserialize(value, parser);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ public String jsonValue() {
return this.jsonValue;
}

public static final JsonEnum.Deserializer<Refresh> _DESERIALIZER = new JsonEnum.DeserializerSupportingBooleans<>(Refresh.values());
public static final JsonEnum.Deserializer<Refresh> _DESERIALIZER = new JsonEnum.Deserializer<>(Refresh.values());
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public String jsonValue() {
return this.jsonValue;
}

public static final JsonEnum.Deserializer<DynamicMapping> _DESERIALIZER = new JsonEnum.DeserializerSupportingBooleans<>(
public static final JsonEnum.Deserializer<DynamicMapping> _DESERIALIZER = new JsonEnum.Deserializer<>(
DynamicMapping.values());
}

0 comments on commit b372d24

Please sign in to comment.