Example usage for com.fasterxml.jackson.core JsonParser close

List of usage examples for com.fasterxml.jackson.core JsonParser close

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser close.

Prototype

@Override
public abstract void close() throws IOException;

Source Link

Document

Closes the parser so that no further iteration or data access can be made; will also close the underlying input source if parser either owns the input source, or feature Feature#AUTO_CLOSE_SOURCE is enabled.

Usage

From source file:Service.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();
    try {/*from  w  w  w  .ja  v a 2  s  .co  m*/
        JsonGenerator g = factory.createGenerator(sw);
        g.writeStartObject();
        g.writeNumberField("code", 200);
        g.writeArrayFieldStart("languages");
        for (Language l : Languages.get()) {
            g.writeStartObject();
            g.writeStringField("name", l.getName());
            g.writeStringField("locale", l.getLocaleWithCountryAndVariant().toString());
            g.writeEndObject();
        }
        g.writeEndArray();
        g.writeEndObject();
        g.flush();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    String languagesResponse = sw.toString();

    String errorResponse = codeResponse(500);
    String okResponse = codeResponse(200);

    Scanner sc = new Scanner(System.in);
    while (sc.hasNextLine()) {
        try {
            String line = sc.nextLine();
            JsonParser p = factory.createParser(line);
            String cmd = "";
            String text = "";
            String language = "";
            while (p.nextToken() != JsonToken.END_OBJECT) {
                String name = p.getCurrentName();
                if ("command".equals(name)) {
                    p.nextToken();
                    cmd = p.getText();
                }
                if ("text".equals(name)) {
                    p.nextToken();
                    text = p.getText();
                }
                if ("language".equals(name)) {
                    p.nextToken();
                    language = p.getText();
                }
            }
            p.close();

            if ("check".equals(cmd)) {
                sw = new StringWriter();
                JsonGenerator g = factory.createGenerator(sw);
                g.writeStartObject();
                g.writeNumberField("code", 200);
                g.writeArrayFieldStart("matches");
                for (RuleMatch match : new JLanguageTool(Languages.getLanguageForShortName(language))
                        .check(text)) {
                    g.writeStartObject();

                    g.writeNumberField("offset", match.getFromPos());

                    g.writeNumberField("length", match.getToPos() - match.getFromPos());

                    g.writeStringField("message", substituteSuggestion(match.getMessage()));

                    if (match.getShortMessage() != null) {
                        g.writeStringField("shortMessage", substituteSuggestion(match.getShortMessage()));
                    }

                    g.writeArrayFieldStart("replacements");
                    for (String replacement : match.getSuggestedReplacements()) {
                        g.writeString(replacement);
                    }
                    g.writeEndArray();

                    Rule rule = match.getRule();

                    g.writeStringField("ruleId", rule.getId());

                    if (rule instanceof AbstractPatternRule) {
                        String subId = ((AbstractPatternRule) rule).getSubId();
                        if (subId != null) {
                            g.writeStringField("ruleSubId", subId);
                        }
                    }

                    g.writeStringField("ruleDescription", rule.getDescription());

                    g.writeStringField("ruleIssueType", rule.getLocQualityIssueType().toString());

                    if (rule.getUrl() != null) {
                        g.writeArrayFieldStart("ruleUrls");
                        g.writeString(rule.getUrl().toString());
                        g.writeEndArray();
                    }

                    Category category = rule.getCategory();
                    CategoryId catId = category.getId();
                    if (catId != null) {
                        g.writeStringField("ruleCategoryId", catId.toString());

                        g.writeStringField("ruleCategoryName", category.getName());
                    }

                    g.writeEndObject();
                }
                g.writeEndArray();
                g.writeEndObject();
                g.flush();
                System.out.println(sw.toString());
            } else if ("languages".equals(cmd)) {
                System.out.println(languagesResponse);
            } else if ("quit".equals(cmd)) {
                System.out.println(okResponse);
                return;
            } else {
                System.out.println(errorResponse);
            }
        } catch (Exception e) {
            System.out.println(errorResponse);
        }
    }
}

From source file:org.lansir.beautifulgirls.utils.JsonMapper.java

public static Map<?, ?> node2map(JsonNode json) throws JsonProcessingException, IOException {
    if (json == null) {
        return null;
    }//from w ww. j a  va2 s.com
    JsonParser jp = null;
    try {
        jp = json.traverse();
        return m.readValue(jp, Map.class);
    } finally {
        if (jp != null) {
            try {
                jp.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:org.lansir.beautifulgirls.utils.JsonMapper.java

public static <T> T node2pojo(JsonNode json, Class<T> pojoClass) throws JsonProcessingException, IOException {
    if (json == null) {
        return null;
    }/*from ww w  .j  ava 2 s. co m*/
    JsonParser jp = null;
    try {
        jp = json.traverse();
        return m.readValue(jp, pojoClass);
    } finally {
        if (jp != null) {
            try {
                jp.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.microsoft.azure.storage.core.EncryptionData.java

public static EncryptionData deserialize(String inputData) throws JsonProcessingException, IOException {
    JsonParser parser = Utility.getJsonParser(inputData);
    try {// w  w  w.j ava2s.  c  om
        if (!parser.hasCurrentToken()) {
            parser.nextToken();
        }

        return EncryptionData.deserialize(parser);
    } finally {
        parser.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Merges the {@code message} from the {@link Reader} using the given {@code schema}.
 *//* w  ww .  j a va2  s.  co m*/
public static <T> void mergeFrom(Reader reader, T message, Schema<T> schema, boolean numeric)
        throws IOException {
    final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(reader);
    try {
        mergeFrom(parser, message, schema, numeric);
    } finally {
        parser.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Parses the {@code messages} from the reader using the given {@code schema}.
 *///www .ja  v a2s . c o  m
public static <T> List<T> parseListFrom(Reader reader, Schema<T> schema, boolean numeric) throws IOException {
    final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(reader);
    try {
        return parseListFrom(parser, schema, numeric);
    } finally {
        parser.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Merges the {@code message} from the {@link InputStream} using the given {@code schema}.
 * <p>//from  w ww  .j  a va2s .co m
 * The {@link LinkedBuffer}'s internal byte array will be used when reading the message.
 */
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema, boolean numeric,
        LinkedBuffer buffer) throws IOException {
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), in, false);
    final JsonParser parser = newJsonParser(in, buffer.buffer, 0, 0, false, context);
    try {
        mergeFrom(parser, message, schema, numeric);
    } finally {
        parser.close();
    }
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Parses the {@code messages} from the stream using the given {@code schema}.
 * <p>/*from  w ww.java 2  s  .c o m*/
 * The {@link LinkedBuffer}'s internal byte array will be used when reading the message.
 */
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema, boolean numeric, LinkedBuffer buffer)
        throws IOException {
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), in, false);
    final JsonParser parser = newJsonParser(in, buffer.buffer, 0, 0, false, context);
    try {
        return parseListFrom(parser, schema, numeric);
    } finally {
        parser.close();
    }
}

From source file:org.metis.utils.Utils.java

public static List<Map<String, String>> parseJson(String json) throws Exception {

    if (jsonFactory == null) {
        LOG.error("parseJson: ERROR, jsonFactory is null");
        throw new Exception("parseJson: ERROR, jsonFactory is null");
    }//ww  w.  jav  a2 s  .c om

    LOG.trace("parseJson: jsonString = " + json);
    JsonParser jp = jsonFactory.createParser(json);

    List<Map<String, String>> rows = new ArrayList<Map<String, String>>();
    try {
        parseJson(jp, rows);
    } finally {
        jp.close();
    }
    return rows;
}

From source file:io.protostuff.JsonIOUtil.java

/**
 * Merges the {@code message} from the {@link InputStream} using the given {@code schema}.
 *//* w ww.ja  v a2  s  . c  o m*/
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema, boolean numeric)
        throws IOException {
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), in, false);
    final JsonParser parser = newJsonParser(in, context.allocReadIOBuffer(), 0, 0, true, context);
    // final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(in);
    try {
        mergeFrom(parser, message, schema, numeric);
    } finally {
        parser.close();
    }
}