Search in sources :

Example 1 with SerializedString

use of org.codehaus.jackson.io.SerializedString in project stanbol by apache.

the class AnalyzedTextParser method parse.

/**
     * Parses {@link AnalysedText} {@link Span}s including annotations from the 
     * {@link InputStream}. The {@link AnalysedText} instance that is going to
     * be enrichted with the parsed data needs to be parsed. In the simplest case
     * the caller can create an empty instance by using a 
     * {@link AnalysedTextFactory}.
     * @param in The stream to read the data from
     * @param charset the {@link Charset} used by the stream
     * @param at The {@link AnalysedText} instance used to add the data to
     * @return the parsed {@link AnalysedText} instance enrichted with the
     * information parsed from the Stream
     * @throws IOException on any Error while reading or parsing the data
     * from the Stream
     */
public AnalysedText parse(InputStream in, Charset charset, final AnalysedText at) throws IOException {
    if (in == null) {
        throw new IllegalArgumentException("The parsed InputStream MUST NOT be NULL!");
    }
    if (charset == null) {
        charset = UTF8;
    }
    JsonParser parser = mapper.getJsonFactory().createJsonParser(new InputStreamReader(in, charset));
    if (parser.nextToken() != JsonToken.START_OBJECT) {
        //start object
        throw new IOException("JSON serialized AnalyzedTexts MUST use a JSON Object as Root!");
    }
    if (!parser.nextFieldName(new SerializedString("spans"))) {
        throw new IOException("JSON serialized AnalyzedText MUST define the 'spans' field as first entry " + "in the root JSON object!");
    }
    if (parser.nextValue() != JsonToken.START_ARRAY) {
        throw new IOException("The value of the 'span' field MUST BE an Json Array!");
    }
    boolean first = true;
    while (parser.nextValue() == JsonToken.START_OBJECT) {
        if (first) {
            parseAnalyzedTextSpan(parser.readValueAsTree(), at);
            first = false;
        } else {
            parseSpan(at, parser.readValueAsTree());
        }
    }
    return at;
}
Also used : SerializedString(org.codehaus.jackson.io.SerializedString) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) JsonParser(org.codehaus.jackson.JsonParser)

Aggregations

IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 JsonParser (org.codehaus.jackson.JsonParser)1 SerializedString (org.codehaus.jackson.io.SerializedString)1