use of jakarta.json.stream.JsonParsingException in project dash-licenses by eclipse.
the class ClearlyDefinedSupport method doQueryClearlyDefined.
private void doQueryClearlyDefined(List<IContentId> ids, int start, int end, Consumer<IContentData> consumer) {
// If there's nothing to do, bail out.
if (start == end)
return;
int code = httpClientService.post(settings.getClearlyDefinedDefinitionsUrl(), "application/json", JsonUtils.toJson(ids.subList(start, end)), response -> {
// FIXME Seems like overkill.
AtomicInteger counter = new AtomicInteger();
try {
JsonUtils.readJson(new StringReader(response)).forEach((key, each) -> {
ClearlyDefinedContentData data = new ClearlyDefinedContentData(key, each.asJsonObject());
data.setStatus(isAccepted(data) ? Status.Approved : Status.Restricted);
consumer.accept(data);
counter.incrementAndGet();
logger.debug("ClearlyDefined {} score: {} {} {}", data.getId(), data.getScore(), data.getLicense(), data.getStatus() == Status.Approved ? "approved" : "restricted");
});
logger.info("Found {} items.", counter.get());
} catch (JsonParsingException e) {
logger.error("Could not parse the response from ClearlyDefined: {}.", response);
logger.debug(e.getMessage(), e);
throw new ClearlyDefinedResponseException(e);
}
});
if (code != 200) {
logger.error("Error response from ClearlyDefined {}", code);
throw new RuntimeException("Received an error response from ClearlyDefined.");
}
}
use of jakarta.json.stream.JsonParsingException in project opensearch-java by opensearch-project.
the class UnionDeserializer method deserialize.
@Override
public Union deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
EventHandler<Union, Kind, Member> member = otherMembers.get(event);
if (member == null && event == Event.START_OBJECT && !objectMembers.isEmpty()) {
// Parse as an object to find matching field names
JsonObject object = parser.getObject();
for (String field : object.keySet()) {
member = objectMembers.get(field);
if (member != null) {
break;
}
}
if (member == null) {
member = fallbackObjectMember;
}
if (member != null) {
// Traverse the object we have inspected
parser = JsonpUtils.objectParser(object, mapper);
event = parser.next();
}
}
if (member == null) {
throw new JsonParsingException("Cannot determine what union member to deserialize", parser.getLocation());
}
return member.deserialize(parser, mapper, event, buildFn);
}
use of jakarta.json.stream.JsonParsingException in project opensearch-java by opensearch-project.
the class JsonpUtils method lookAheadFieldValue.
/**
* Looks ahead a field value in the Json object from the upcoming object in a parser, which should be on the
* START_OBJECT event.
*
* Returns a pair containing that value and a parser that should be used to actually parse the object
* (the object has been consumed from the original one).
*/
public static Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String defaultValue, JsonParser parser, JsonpMapper mapper) {
// FIXME: need a buffering parser wrapper so that we don't roundtrip through a JsonObject and a String
// FIXME: resulting parser should return locations that are offset with the original parser's location
JsonObject object = parser.getObject();
String result = object.getString(name, null);
if (result == null) {
result = defaultValue;
}
if (result == null) {
throw new JsonParsingException("Property '" + name + "' not found", parser.getLocation());
}
return new AbstractMap.SimpleImmutableEntry<>(result, objectParser(object, mapper));
}
use of jakarta.json.stream.JsonParsingException in project opensearch-java by opensearch-project.
the class JacksonJsonpParser method next.
@Override
public Event next() {
JsonToken token;
if (hasNextWasCalled) {
token = parser.getCurrentToken();
hasNextWasCalled = false;
} else {
token = fetchNextToken();
}
if (token == null) {
throw new NoSuchElementException();
}
Event result = tokenToEvent.get(token);
if (result == null) {
throw new JsonParsingException("Unsupported Jackson event type '" + token + "'", getLocation());
}
return result;
}
use of jakarta.json.stream.JsonParsingException in project opensearch-java by opensearch-project.
the class JsonValueParser method parseObject.
public JsonObject parseObject(JsonParser parser) throws IOException {
JsonObjectBuilder ob = provider.createObjectBuilder();
JsonToken token;
while ((token = parser.nextToken()) != JsonToken.END_OBJECT) {
if (token != JsonToken.FIELD_NAME) {
throw new JsonParsingException("Expected a property name", new JacksonJsonpLocation(parser));
}
String name = parser.getCurrentName();
parser.nextToken();
ob.add(name, parseValue(parser));
}
return ob.build();
}
Aggregations