Search in sources :

Example 1 with JsonParsingException

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.");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringReader(java.io.StringReader) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 2 with JsonParsingException

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);
}
Also used : JsonObject(jakarta.json.JsonObject) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 3 with JsonParsingException

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));
}
Also used : JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 4 with JsonParsingException

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;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) NoSuchElementException(java.util.NoSuchElementException) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 5 with JsonParsingException

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();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonParsingException(jakarta.json.stream.JsonParsingException)

Aggregations

JsonParsingException (jakarta.json.stream.JsonParsingException)8 StringReader (java.io.StringReader)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 JsonObject (jakarta.json.JsonObject)2 JsonProvider (jakarta.json.spi.JsonProvider)2 JsonParser (jakarta.json.stream.JsonParser)2 Test (org.junit.Test)2 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)2 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)1 JsonParseException (com.linkedin.avroutil1.parser.exceptions.JsonParseException)1 ParseException (com.linkedin.avroutil1.parser.exceptions.ParseException)1 JsonObjectExt (com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt)1 JsonReaderExt (com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt)1 JsonReaderWithLocations (com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations)1 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)1 JsonString (jakarta.json.JsonString)1 JsonGenerator (jakarta.json.stream.JsonGenerator)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1