Skip to content

Commit 979edf7

Browse files
committed
cleaner code
1 parent 661c2ed commit 979edf7

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,28 @@ public static Map.Entry<String, JsonParser> lookAheadFieldValue(
243243
} else {
244244
// Unbuffered path: parse the object into a JsonObject, then extract the value and parse it again
245245
JsonObject object = parser.getObject();
246-
String result = object.getString(name, null);
247246

248-
if (result == null) {
249-
// checking if instead of a string it's a boolean
250-
try{
251-
result = String.valueOf(object.getBoolean(name));
252-
} catch (NullPointerException e) {
253-
// suppressed in favor of JsonpMappingException below
247+
String result = null;
248+
JsonValue value = object.get(name);
249+
// Handle enums and booleans promoted to enums
250+
if (value != null) {
251+
if (value.getValueType() == JsonValue.ValueType.STRING) {
252+
result = ((JsonString) value).getString();
253+
} else if (value.getValueType() == JsonValue.ValueType.TRUE) {
254+
result = "true";
255+
} else if (value.getValueType() == JsonValue.ValueType.FALSE) {
256+
result = "false";
254257
}
255258
}
256259

257260
if (result == null) {
258261
result = defaultValue;
259262
}
260263

264+
if (result == null) {
265+
result = defaultValue;
266+
}
267+
261268
if (result == null) {
262269
throw new JsonpMappingException("Property '" + name + "' not found", location);
263270
}

java-client/src/main/java/co/elastic/clients/json/jackson/JacksonJsonpParser.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -336,27 +336,27 @@ public Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String def
336336
if (fieldName.equals(name)) {
337337
// Found
338338
tb.copyCurrentEvent(parser);
339-
try {
340-
expectNextEvent(JsonToken.VALUE_STRING);
341-
}
342-
catch (UnexpectedJsonEventException e) {
343-
// checking if instead of a string it's a boolean
344-
String details = e.getMessage();
345-
if (details.contains("VALUE_TRUE")){
346-
expectEvent(JsonToken.VALUE_TRUE);
347-
}
348-
else if (details.contains("VALUE_FALSE")){
349-
expectEvent(JsonToken.VALUE_FALSE);
350-
}
351-
// not a boolean either, can throw exception
352-
else{
353-
throw e;
354-
}
339+
340+
String result = null;
341+
switch (parser.nextToken()) {
342+
case VALUE_STRING:
343+
result = parser.getText();
344+
break;
345+
// Handle booleans promoted to enums
346+
case VALUE_TRUE:
347+
result = "true";
348+
break;
349+
case VALUE_FALSE:
350+
result = "false";
351+
break;
352+
default:
353+
expectEvent(JsonToken.VALUE_STRING);
355354
}
355+
356356
tb.copyCurrentEvent(parser);
357357

358358
return new AbstractMap.SimpleImmutableEntry<>(
359-
parser.getText(),
359+
result,
360360
new JacksonJsonpParser(
361361
JsonParserSequence.createFlattened(false, tb.asParser(), parser),
362362
mapper

0 commit comments

Comments
 (0)