From 3a8c7fdd1c08dc0b985ff2b51f52dcdeec9037b3 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Tue, 24 May 2022 23:14:52 +0900 Subject: [PATCH] ApiClient: update result json parser --- .../omegat/textra/TextraApiClient.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java b/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java index eecad3e..32da313 100644 --- a/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java +++ b/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java @@ -177,18 +177,14 @@ public String executeTranslation(final TextraOptions options, final String text) throw new Exception(String.format("Get response: %d", respStatus)); } - String result; + ObjectMapper mapper = new ObjectMapper(); try (BufferedInputStream bis = new BufferedInputStream(respBodyStream)) { - String rsp = IOUtils.toString(bis, StandardCharsets.UTF_8); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jobj = mapper.readTree(rsp); - JsonNode resultset = jobj.get("resultset"); - result = resultset.get("result").get("text").asText(); + ResultSet resultSet = mapper.readValue(bis, ResultSet.class); + return resultSet.result.text; } catch (IOException ex) { Log.log(ex); return null; } - return result; } private static String getAccessUrl(final TextraOptions options) { @@ -211,4 +207,12 @@ private static String getAccessUrl(final TextraOptions options) { } return apiUrl; } + + static class ResultSet { + public Result result; + } + + static class Result { + public String text; + } }