public String parse(String jsonLine) { JsonElement jelement = new JsonParser().parse(jsonLine); JsonObject jobject = jelement.getAsJsonObject(); jobject = jobject.getAsJsonObject("data"); JsonArray jarray = jobject.getAsJsonArray("translations"); jobject = jarray.get(0).getAsJsonObject(); String result = jobject.get("translatedText").toString(); return result; }
@CheckForNull private static String tryParseAsJsonError(String responseContent) { try { JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(responseContent).getAsJsonObject(); JsonArray errors = obj.getAsJsonArray("errors"); List<String> errorMessages = new ArrayList<>(); for (JsonElement e : errors) { errorMessages.add(e.getAsJsonObject().get("msg").getAsString()); } return Joiner.on(", ").join(errorMessages); } catch (Exception e) { return null; } }
public String getHealthcheck() { if (!json.getAsJsonObject(CONFIG).has(HEALTHCHECK) || !json.getAsJsonObject(CONFIG).getAsJsonObject(HEALTHCHECK).has(TEST)) { return null; } return Joiner.on(", ").join(json.getAsJsonObject(CONFIG).getAsJsonObject(HEALTHCHECK).getAsJsonArray(TEST)); }
@Override public PersistedDataArray getAsArray(String name) { return new GsonPersistedDataArray(map.getAsJsonArray(name)); }
private void readSoundList(JsonObject element, String field, List<StaticSound> sounds) throws IOException { if (element.has(field) && element.get(field).isJsonArray()) { sounds.clear(); for (JsonElement item : element.getAsJsonArray(field)) { Optional<StaticSound> sound = assetManager.getAsset(item.getAsString(), StaticSound.class); if (sound.isPresent()) { sounds.add(sound.get()); } else { throw new IOException("Unable to resolve sound '" + item.getAsString() + "'"); } } } }
public List<MultiSearchResponse> getResponses() { List<MultiSearchResponse> multiSearchResponses = new ArrayList<MultiSearchResponse>(); if(jsonObject != null && jsonObject.has(RESPONSES_KEY)) { JsonArray responsesArray = jsonObject.getAsJsonArray(RESPONSES_KEY); for(JsonElement responseElement : responsesArray) { multiSearchResponses.add(new MultiSearchResponse(responseElement.getAsJsonObject())); } } return multiSearchResponses; }
public Optional<List<String>> readStringArrayIfPresent(String property) { if (jsonObject.has(property)) { try { Spliterator<JsonElement> iterator = jsonObject.getAsJsonArray(property).spliterator(); return Optional.of( StreamSupport.stream(iterator, false) .map(JsonElement::getAsString) .collect(Collectors.toList())); } catch (Exception e) { throw haltBecausePropertyIsNotAJsonStringArray(property, jsonObject); } } return Optional.empty(); }
private static Set<SourceEntity> getSourceEntities(String response) { Set<SourceEntity> result = Sets.newHashSet(); JsonObject jsonObject = new Gson().fromJson(response, JsonObject.class).getAsJsonObject(); JsonArray array = jsonObject.getAsJsonArray("sobjects"); for (JsonElement element : array) { String sourceEntityName = element.getAsJsonObject().get("name").getAsString(); result.add(SourceEntity.fromSourceEntityName(sourceEntityName)); } return result; }
@Override public Map<String, PortBinding> getPortBindings() { if (json.get(PORTS).isJsonNull()) { return Collections.emptyMap(); } return mapPortBindings(json.getAsJsonArray(PORTS)); }
public String[] getArguments() { if (!json.has(PROCESS_CONFIG)) { return null; } JsonObject processConfig = json.getAsJsonObject(PROCESS_CONFIG); if (!processConfig.has(ARGUMENTS)) { return null; } JsonArray arguments = processConfig.getAsJsonArray(ARGUMENTS); String[] result = new String[arguments.size()]; for (int i = 0; i < arguments.size(); i++) { result[i] = arguments.get(i).getAsString(); } return result; } }
public Optional<JsonArray> optJsonArray(String property) { if (hasJsonObject(property)) { try { return Optional.ofNullable(jsonObject.getAsJsonArray(property)); } catch (Exception e) { throw haltBecausePropertyIsNotAJsonArray(property, jsonObject); } } return Optional.empty(); }
/** * * @return empty list if Bulk action failed on HTTP level, otherwise all individual action items in the response */ public List<BulkResultItem> getItems() { List<BulkResultItem> items = new LinkedList<BulkResultItem>(); if (jsonObject != null && jsonObject.has("items")) { for (JsonElement jsonElement : jsonObject.getAsJsonArray("items")) { for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) { items.add(new BulkResultItem( entry.getKey(), entry.getValue().getAsJsonObject() )); } } } return items; }
public WxMpMassNews deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMassNews wxMpMassNews = new WxMpMassNews(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("media_id") != null && !json.get("media_id").isJsonNull()) { JsonArray articles = json.getAsJsonArray("articles"); for (JsonElement article1 : articles) { JsonObject articleInfo = article1.getAsJsonObject(); WxMpMassNews.WxMpMassNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMassNews.WxMpMassNewsArticle.class); wxMpMassNews.addArticle(article); } } return wxMpMassNews; } }
public WxMpMaterialNews deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMaterialNews wxMpMaterialNews = new WxMpMaterialNews(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("news_item") != null && !json.get("news_item").isJsonNull()) { JsonArray articles = json.getAsJsonArray("news_item"); for (JsonElement article1 : articles) { JsonObject articleInfo = article1.getAsJsonObject(); WxMpMaterialNews.WxMpMaterialNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNews.WxMpMaterialNewsArticle.class); wxMpMaterialNews.addArticle(article); } } return wxMpMaterialNews; } }
@Override public SecurityConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SecurityConfig result = new SecurityConfig(); JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.has(SERVER_PUBLIC_CERTIFICATE)) { result.serverPublicCertificate = context.deserialize(jsonObject.getAsJsonObject(SERVER_PUBLIC_CERTIFICATE), PublicIdentityCertificate.class); } if (jsonObject.has(SERVER_PRIVATE_CERTIFICATE)) { result.serverPrivateCertificate = context.deserialize(jsonObject.getAsJsonObject(SERVER_PRIVATE_CERTIFICATE), PrivateIdentityCertificate.class); } if (jsonObject.has(CLIENT_IDENTITIES)) { JsonArray clientArray = jsonObject.getAsJsonArray(CLIENT_IDENTITIES); for (JsonElement jsonEntry : clientArray) { ClientEntry entry = context.deserialize(jsonEntry, ClientEntry.class); result.addIdentity(entry.server, new ClientIdentity(entry.clientPublic, entry.clientPrivate)); } } return result; }
private Map<String, PortBinding> createPortBindings(JsonObject ports) { Map<String, PortBinding> portBindings = new HashMap<>(); for (Object obj : ports.keySet()) { String port = obj.toString(); if (ports.get(port).isJsonNull()) { addPortMapping(port, (PortBinding) null, portBindings); } else { // use the first entry in the array JsonObject hostConfig = ports.getAsJsonArray(port).get(0).getAsJsonObject(); addPortMapping(port, hostConfig, portBindings); } } return portBindings; }
/** * Perform extended authentication. Use the provided credentials as IAM credentials and * get a temporary ECR token. * * @param localCredentials IAM id/secret * @return ECR base64 encoded username:password * @throws IOException * @throws MojoExecutionException */ public AuthConfig extendedAuth(AuthConfig localCredentials) throws IOException, MojoExecutionException { JsonObject jo = getAuthorizationToken(localCredentials); JsonArray authorizationDatas = jo.getAsJsonArray("authorizationData"); JsonObject authorizationData = authorizationDatas.get(0).getAsJsonObject(); String authorizationToken = authorizationData.get("authorizationToken").getAsString(); return new AuthConfig(authorizationToken, "none"); }
public ProjectStatusResponse.ProjectStatus format() { if (!optionalMeasureData.isPresent()) { return newResponseWithoutQualityGateDetails(); } JsonParser parser = new JsonParser(); JsonObject json = parser.parse(optionalMeasureData.get()).getAsJsonObject(); ProjectStatusResponse.Status qualityGateStatus = measureLevelToQualityGateStatus(json.get("level").getAsString()); projectStatusBuilder.setStatus(qualityGateStatus); formatIgnoredConditions(json); formatConditions(json.getAsJsonArray("conditions")); formatPeriods(); return projectStatusBuilder.build(); }
public WxMpMaterialNewsBatchGetResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMaterialNewsBatchGetResult wxMpMaterialNewsBatchGetResult = new WxMpMaterialNewsBatchGetResult(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("total_count") != null && !json.get("total_count").isJsonNull()) { wxMpMaterialNewsBatchGetResult.setTotalCount(GsonHelper.getAsInteger(json.get("total_count"))); } if (json.get("item_count") != null && !json.get("item_count").isJsonNull()) { wxMpMaterialNewsBatchGetResult.setItemCount(GsonHelper.getAsInteger(json.get("item_count"))); } if (json.get("item") != null && !json.get("item").isJsonNull()) { JsonArray item = json.getAsJsonArray("item"); List<WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem> items = new ArrayList<>(); for (JsonElement anItem : item) { JsonObject articleInfo = anItem.getAsJsonObject(); items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class)); } wxMpMaterialNewsBatchGetResult.setItems(items); } return wxMpMaterialNewsBatchGetResult; } }
public WxMpMaterialFileBatchGetResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMaterialFileBatchGetResult wxMpMaterialFileBatchGetResult = new WxMpMaterialFileBatchGetResult(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("total_count") != null && !json.get("total_count").isJsonNull()) { wxMpMaterialFileBatchGetResult.setTotalCount(GsonHelper.getAsInteger(json.get("total_count"))); } if (json.get("item_count") != null && !json.get("item_count").isJsonNull()) { wxMpMaterialFileBatchGetResult.setItemCount(GsonHelper.getAsInteger(json.get("item_count"))); } if (json.get("item") != null && !json.get("item").isJsonNull()) { JsonArray item = json.getAsJsonArray("item"); List<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> items = new ArrayList<>(); for (JsonElement anItem : item) { JsonObject articleInfo = anItem.getAsJsonObject(); items.add(WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class)); } wxMpMaterialFileBatchGetResult.setItems(items); } return wxMpMaterialFileBatchGetResult; } }