public GensonJsonUtils() { super(); this.genson = new GensonBuilder().withBundle(new BaseGensonBundle()).create(); }
return create(createConverterFactory(), withClassAliases);
public GensonMessageConverter() { this(new GensonBuilder().setHtmlSafe(true).setSkipNull(true).useBeanViews(true).create()); }
return create(createConverterFactory(), withClassAliases);
public GensonMessageConverter() { this(new GensonBuilder().setHtmlSafe(true).setSkipNull(true).useBeanViews(true).create()); }
@Override public Genson getContext(Class<?> type) { return new GensonBuilder() // copy from com.owlike.genson.ext.jaxrs.GensonJaxRSFeature._defaultGenson .withBundle(new JAXBBundle()) .useConstructorWithArguments(true) .useDateAsTimestamp(false) // java8 time support .withContextualFactory(new Java8TimeContextualFactory()) .create(); } }
.create(); private final Jsonb yasson = new JsonBindingProvider().create() .withProvider(new org.glassfish.json.JsonProviderImpl())
@Test public void parseJson() { Genson genson = new GensonBuilder().useMethods(true).create(); try(InputStream is = loader.getResourceAsStream("sample-response.json")) { List<Translation> list = genson.deserialize(is, new GenericType<List<Translation>>() {}); assertThat(list).hasSize(1); Translation trans = list.get(0); assertThat(trans.isSuccessful()) .isTrue(); assertThat(trans.getText()) .matches("取得"); assertThat(trans.getWords()).hasSize(1); List<Word> words = trans.getWords(); Word word = words.get(0); assertThat(word.isSuccessful()).isTrue(); assertThat(word.getText()).matches("取得"); assertThat(word.getTranslatedText()).matches("acquisition"); assertThat(word.getCandidates()) .contains(new Candidates("acquisition"), new Candidates("get"), new Candidates("acquired"), new Candidates("fetch")); } catch (IOException e) { e.printStackTrace(); } } }
public List<Translation> translate(String text) { URL url = new HttpUrl.Builder() .scheme("https") .host("api.codic.jp") .addPathSegment("v1") .addPathSegment("engine") .addPathSegment("translate.json") .addQueryParameter("text", text) .build() .url(); Request request = new Builder() .url(url) .addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken) .get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() {}); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } } }