List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.hybris.datahub.service.impl.DefaultJsonService.java
License:Open Source License
private String convertJsonValue(final JsonElement jsonElement) { if (jsonElement.isJsonPrimitive()) { return jsonElement.getAsString(); } else if (jsonElement.isJsonNull()) { return ""; } else {//from ww w .j av a 2s .co m return jsonElement.toString(); } }
From source file:com.hybris.mobile.lib.http.converter.JsonDataConverter.java
License:Open Source License
/** * @param data String of character to be parsed * @param property Attributes to be parse * @param element JSON element to get data from * @return List of String/* w w w.j a va2s.c om*/ */ protected List<String> getValuesFromPropertyElement(String data, String property, String element, boolean recursive) { List<String> listToReturn = new ArrayList<>(); if (data == null) { throw new IllegalArgumentException(); } JsonParser parser = new JsonParser(); JsonArray jsonArray; JsonElement jsonElement; try { if (StringUtils.isNotBlank(property)) { jsonElement = parser.parse(data).getAsJsonObject().get(property); } else { jsonElement = parser.parse(data); } if (jsonElement != null) { if (jsonElement.isJsonArray()) { jsonArray = jsonElement.getAsJsonArray(); } else { jsonArray = new JsonArray(); jsonArray.add(jsonElement); } if (jsonArray != null) { for (JsonElement currentJsonElement : jsonArray) { if (StringUtils.isNotBlank(element)) { if (recursive) { try { listToReturn.addAll(getValuesFromPropertyElement(currentJsonElement.toString(), property, element, recursive)); } catch (NoSuchElementException e) { Log.d(TAG, "End of getting the recursive property " + property + "."); } } currentJsonElement = currentJsonElement.getAsJsonObject().get(element); } if (currentJsonElement != null) { if (currentJsonElement.isJsonPrimitive()) { listToReturn.add(currentJsonElement.getAsString()); } else { listToReturn.add(currentJsonElement.toString()); } } else { Log.d(TAG, "No data found for element " + element + "."); } } } } else { Log.d(TAG, "No data found on " + data + " for property " + property + "."); } } catch (Exception e) { Log.e(TAG, "Error parsing the data " + data + " for property " + property + " and element " + element + "."); throw new NoSuchElementException("Error parsing the data " + data + " for property " + property + " and element " + element + "."); } return listToReturn; }
From source file:com.hyperaware.conference.android.eventmobi.parser.gson.BooleanDeserializer.java
License:Open Source License
@Override public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return "1".equals(json.getAsString()); }
From source file:com.ibm.g11n.pipeline.resfilter.GlobalizeJsResource.java
License:Open Source License
/** * The override of addBundleStrings is necessary because globalizejs treats arrays of strings * as a single long string, with each piece separated by a space, rather than treating it like * a real JSON array./*from w ww . j av a 2 s. c o m*/ */ @Override protected int addBundleStrings(JsonObject obj, String keyPrefix, Bundle bundle, int sequenceNum) { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { sequenceNum = addBundleStrings(value.getAsJsonObject(), encodeResourceKey(keyPrefix, key, false), bundle, sequenceNum); } else if (value.isJsonArray()) { JsonArray ar = value.getAsJsonArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ar.size(); i++) { JsonElement arrayEntry = ar.get(i); if (arrayEntry.isJsonPrimitive() && arrayEntry.getAsJsonPrimitive().isString()) { if (i > 0) { sb.append(" "); // } sb.append(arrayEntry.getAsString()); } else { throw new IllegalResourceFormatException( "Arrays must contain only strings in a globalizejs resource."); } } sequenceNum++; bundle.addResourceString(encodeResourceKey(keyPrefix, key, true), sb.toString(), sequenceNum); } else if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException("The value of JSON element " + key + " is not a string."); } else { sequenceNum++; bundle.addResourceString(encodeResourceKey(keyPrefix, key, true), value.getAsString(), sequenceNum); } } return sequenceNum; }
From source file:com.ibm.g11n.pipeline.resfilter.impl.GlobalizeJsResource.java
License:Open Source License
/** * The override of addBundleStrings is necessary because globalizejs treats arrays of strings * as a single long string, with each piece separated by a space, rather than treating it like * a real JSON array.// www. j ava 2 s .c o m */ @Override protected int addBundleStrings(JsonObject obj, String keyPrefix, LanguageBundleBuilder bb, int sequenceNum) throws ResourceFilterException { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { sequenceNum = addBundleStrings(value.getAsJsonObject(), encodeResourceKey(keyPrefix, key, false), bb, sequenceNum); } else if (value.isJsonArray()) { JsonArray ar = value.getAsJsonArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ar.size(); i++) { JsonElement arrayEntry = ar.get(i); if (arrayEntry.isJsonPrimitive() && arrayEntry.getAsJsonPrimitive().isString()) { if (i > 0) { sb.append(" "); // } sb.append(arrayEntry.getAsString()); } else { throw new IllegalResourceFormatException( "Arrays must contain only strings in a globalizejs resource."); } } sequenceNum++; bb.addResourceString(encodeResourceKey(keyPrefix, key, true), sb.toString(), sequenceNum); } else if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException("The value of JSON element " + key + " is not a string."); } else { sequenceNum++; bb.addResourceString(encodeResourceKey(keyPrefix, key, true), value.getAsString(), sequenceNum); } } return sequenceNum; }
From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java
License:Open Source License
protected int addBundleStrings(JsonObject obj, String keyPrefix, LanguageBundleBuilder bb, int sequenceNum) throws ResourceFilterException { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { sequenceNum = addBundleStrings(value.getAsJsonObject(), encodeResourceKey(keyPrefix, key, false), bb, sequenceNum);/* w w w . ja va 2s .c om*/ } else if (value.isJsonArray()) { JsonArray ar = value.getAsJsonArray(); for (int i = 0; i < ar.size(); i++) { JsonElement arrayEntry = ar.get(i); String arrayKey = encodeResourceKey(keyPrefix, key, false) + "[" + Integer.toString(i) + "]"; if (arrayEntry.isJsonPrimitive() && arrayEntry.getAsJsonPrimitive().isString()) { sequenceNum++; bb.addResourceString(ResourceString.with(arrayKey, arrayEntry.getAsString()) .sequenceNumber(sequenceNum)); } else { sequenceNum = addBundleStrings(arrayEntry.getAsJsonObject(), arrayKey, bb, sequenceNum); } } } else if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException("The value of JSON element " + key + " is not a string."); } else { sequenceNum++; bb.addResourceString( ResourceString.with(encodeResourceKey(keyPrefix, key, true), value.getAsString()) .sequenceNumber(sequenceNum)); } } return sequenceNum; }
From source file:com.ibm.g11n.pipeline.resfilter.JsonResource.java
License:Open Source License
@Override public Bundle parse(InputStream inStream) throws IOException { Bundle bundle = new Bundle(); try (InputStreamReader reader = new InputStreamReader(new BomInputStream(inStream), StandardCharsets.UTF_8)) { JsonElement root = new JsonParser().parse(reader); if (!root.isJsonObject()) { throw new IllegalResourceFormatException("The root JSON element is not an JSON object."); }/* ww w . j av a 2 s . c om*/ int sequenceNum = 0; for (Map.Entry<String, JsonElement> entry : root.getAsJsonObject().entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException( "The value of JSON element " + key + " is not a string."); } sequenceNum++; bundle.addResourceString(key, value.getAsString(), sequenceNum); } } catch (JsonParseException e) { throw new IllegalResourceFormatException("Failed to parse the specified JSON contents.", e); } return bundle; }
From source file:com.ibm.iotf.devicemgmt.device.resource.DateResource.java
License:Open Source License
/** * Updates the value of this resource with the given Json value *//* w w w . j a va 2 s . c o m*/ @Override public int update(JsonElement json, boolean fireEvent) { DateTime dt = new DateTime(json.getAsString()); super.setValue(new Date(dt.getMillis()), fireEvent); return this.getRC(); }
From source file:com.ibm.iotf.devicemgmt.device.resource.StringResource.java
License:Open Source License
/** * Updates the value of this resource with the given Json value *///from ww w . j a v a 2 s . c o m public int update(JsonElement json, boolean fireEvent) { this.setValue(json.getAsString(), fireEvent); return this.getRC(); }
From source file:com.ibm.iotf.sample.client.application.api.SampleDataManagementAPIOperations.java
License:Open Source License
public static void main(String[] args) { Properties props = new Properties(); try {/* w w w. ja va 2 s.c om*/ props.load(SampleDataManagementAPIOperations.class.getResourceAsStream(APPLICATION_PROPERTIES_FILE)); } catch (IOException e1) { System.err.println("Not able to read the properties file, exiting.."); return; } // SystemObject object = new SystemObject(); APIClient myClient = null; try { //Instantiate the class by passing the properties file myClient = new APIClient(props); } catch (Exception e) { System.out.println("" + e.getMessage()); // Looks like the properties file is not updated, just ignore; return; } try { //Create Schema Resource for Draft Physical Interface JsonObject draftPhysicalSchemaResponse = myClient.addDraftSchemaDefinition(new File(EVENT_SCHEMA1), SCHEMA_NAME1, SCHEMA_DESCRIPTION1, SCHEMA_TYPE1); JsonElement draftSchemaId = draftPhysicalSchemaResponse.get("id"); System.out.println("Creating artifacts"); System.out.println( "1. Schema for Draft Physical Interface created with id = " + draftSchemaId.getAsString()); System.out.println("Schema Object = " + draftPhysicalSchemaResponse.toString()); //Create Event Type JsonObject draftEventTypeRequest = new JsonObject(); draftEventTypeRequest.add("schemaId", draftSchemaId); draftEventTypeRequest.addProperty("name", EVT_TOPIC); JsonObject draftEventTypeResponse = myClient.addDraftEventType(draftEventTypeRequest.toString()); JsonElement draftEventTypeId = draftEventTypeResponse.get("id"); System.out.println("\n2. Event Type created with id = " + draftEventTypeId.getAsString()); System.out.println("Event Type Object = " + draftEventTypeResponse); //Create Draft Physical Interface JsonObject draftPhysicalInterfaceRequest = new JsonObject(); draftPhysicalInterfaceRequest.addProperty("name", "Env sensor physical interface 7"); JsonObject draftPhysicalInterfaceResponse = myClient .addDraftPhysicalInterface(draftPhysicalInterfaceRequest.toString()); System.out.println("\n3. Draft Physical Interface created with id = " + draftPhysicalInterfaceResponse.get("id").getAsString()); System.out.println("Draft Physical Interface Object = " + draftPhysicalInterfaceResponse.toString()); //Add Event to Physical Interface JsonObject eventTypeToPIRequest = new JsonObject(); eventTypeToPIRequest.addProperty("eventId", EVT_TOPIC); eventTypeToPIRequest.addProperty("eventTypeId", draftEventTypeId.getAsString()); JsonObject eventTypeToPIResponse = myClient.addEventToPhysicalInterface( draftPhysicalInterfaceResponse.get("id").getAsString(), eventTypeToPIRequest.toString()); System.out.println("\n4. Event Type to Draft Physical Interface added with eventTypeId = " + eventTypeToPIResponse.get("eventTypeId").getAsString()); System.out.println("Event Type to Physical Interface = " + eventTypeToPIResponse.toString()); //Update Draft Device Type to connect the Draft Physical Interface JsonObject updateDraftDTToPIRequest = new JsonObject(); JsonObject refs = new JsonObject(); refs.addProperty("events", "/api/v0002/draft/physicalinterfaces/" + draftPhysicalInterfaceResponse.get("id").getAsString() + "/events"); Gson gson = new Gson(); JsonElement element = gson.fromJson(refs.toString(), JsonElement.class); updateDraftDTToPIRequest.add("refs", element); updateDraftDTToPIRequest.addProperty("version", "draft"); updateDraftDTToPIRequest.addProperty("name", "Env sensor physical interface 1"); updateDraftDTToPIRequest.addProperty("createdBy", API_KEY); updateDraftDTToPIRequest.addProperty("updatedBy", API_KEY); updateDraftDTToPIRequest.addProperty("id", draftPhysicalInterfaceResponse.get("id").getAsString()); JsonObject updateDraftDTToPIResponse = myClient.associateDraftPhysicalInterfaceWithDeviceType(TYPE_ID, updateDraftDTToPIRequest.toString()); System.out.println("\n5. Draft Device Type added to draft Physical Interface with id = " + updateDraftDTToPIResponse.get("id").getAsString()); System.out.println( "Draft Device Type to draft Physical Interface = " + updateDraftDTToPIResponse.toString()); //Create schema for draft logical interface JsonObject draftLogicalSchemaResponse = myClient.addDraftSchemaDefinition(new File(EVENT_SCHEMA2), SCHEMA_NAME2, SCHEMA_DESCRIPTION2, SCHEMA_TYPE2); JsonElement draftLogicalSchemaId = draftLogicalSchemaResponse.get("id"); System.out.println("\n6. Schema for Draft Logical Interface created with Id = " + draftLogicalSchemaId.getAsString()); System.out.println( "Schema for Draft Logical Interface Object = " + draftLogicalSchemaResponse.toString()); //Create Draft Logical Interface JsonObject draftLogicalInterfaceRequest = new JsonObject(); draftLogicalInterfaceRequest.addProperty("name", "environment sensor interface"); draftLogicalInterfaceRequest.addProperty("schemaId", draftLogicalSchemaId.getAsString()); JsonObject draftLogicalInterfaceResponse = myClient .addDraftLogicalInterface(draftLogicalInterfaceRequest.toString()); System.out.println("\n7. Draft Logical Interface created with Id = " + draftLogicalInterfaceResponse.get("id").getAsString() + " and schemaId = " + draftLogicalInterfaceResponse.get("schemaId").getAsString()); System.out.println("Draft for Logical Interface Object = " + draftLogicalInterfaceResponse.toString()); //Add Draft Logical Interface to Device Type JsonObject draftLIToDTRequest = new JsonObject(); refs = new JsonObject(); refs.addProperty("schema", "/api/v0002/draft/schemas/" + draftLogicalSchemaId.getAsString()); element = gson.fromJson(refs.toString(), JsonElement.class); draftLIToDTRequest.add("refs", element); draftLIToDTRequest.addProperty("version", "draft"); draftLIToDTRequest.addProperty("name", "environment sensor interface"); draftLIToDTRequest.addProperty("createdBy", API_KEY); draftLIToDTRequest.addProperty("updatedBy", API_KEY); draftLIToDTRequest.addProperty("schemaId", draftLogicalSchemaId.getAsString()); draftLIToDTRequest.addProperty("id", draftLogicalInterfaceResponse.get("id").getAsString()); JsonObject draftLIToDTResponse = myClient.associateDraftLogicalInterfaceToDeviceType(TYPE_ID, draftLIToDTRequest.toString()); System.out.println("\n8. Draft Logical Interface added to device Type with id = " + draftLIToDTResponse.get("id").getAsString()); System.out.println("Draft Logical Interface to Device Type Object = " + draftLIToDTResponse.toString()); //Create mapping between Device Type and Logical Interface JsonObject mappings = new JsonObject(); mappings.addProperty("logicalInterfaceId", draftLIToDTResponse.get("id").getAsString()); mappings.addProperty("notificationStrategy", NOTIFICATION_STRATEGY); JsonObject propertyMapping = new JsonObject(); propertyMapping.addProperty("temperature", "($event.temp - 32) / 1.8"); JsonObject tempMapping = new JsonObject(); tempMapping.add(EVT_TOPIC, gson.fromJson(propertyMapping.toString(), JsonElement.class)); element = gson.fromJson(tempMapping.toString(), JsonElement.class); mappings.add("propertyMappings", tempMapping); JsonObject deviceToLImappings = myClient.addDraftPropertyMappingsToDeviceType(TYPE_ID, mappings.toString()); System.out.println("\n9. Mapping created between device type and Logical Interface with Id = " + deviceToLImappings.get("logicalInterfaceId").getAsString()); System.out.println( "Mapping between device type and logical interface = " + deviceToLImappings.toString()); //Validating configuration JsonObject validateOperation = new JsonObject(); validateOperation.addProperty("operation", SchemaOperation.VALIDATE.getOperation()); System.out.println("id" + draftLogicalInterfaceResponse.get("id").getAsString() + "validateOperation.toString()" + validateOperation.toString()); JsonObject validated = myClient.performOperationAgainstDraftLogicalInterface( draftLogicalInterfaceResponse.get("id").getAsString(), validateOperation.toString()); System.out.println("\n10. Validate operation = " + validated.toString()); Thread.sleep(20000); //Activating configuration if (validated.get("failures").getAsJsonArray().size() == 0) { System.out.println("No validation failures"); JsonObject activateOperation = new JsonObject(); activateOperation.addProperty("operation", SchemaOperation.ACTIVATE.getOperation()); System.out.println("id" + draftLogicalInterfaceResponse.get("id").getAsString() + " activateOperation.toString()" + activateOperation.toString()); JsonObject activated = myClient.performOperationAgainstDraftLogicalInterface( draftLogicalInterfaceResponse.get("id").getAsString(), activateOperation.toString()); System.out.println("\n11. Activate operation = " + activated.toString()); } Thread.sleep(25000); System.out.println("\n\nEnter anything to delete the artifacts created......"); System.out.println("Deleting artifacts"); System.out.println("\n12. Dissociating the device type from Physical Interface = " + TYPE_ID + " = " + myClient.dissociateDraftPhysicalInterfaceFromDeviceType(TYPE_ID)); System.out.println("\n13. Mappings between device type and LogicalInterface deleted = " + myClient.deleteDraftPropertyMappings(TYPE_ID, deviceToLImappings.get("logicalInterfaceId").getAsString())); System.out.println("\n14. Dissociating the device type from Logical Interface = " + TYPE_ID + " = " + myClient.dissociateDraftLogicalInterfaceFromDeviceType(TYPE_ID, draftLIToDTResponse.get("id").getAsString())); System.out.println("\n15. Mapping between Event = " + EVT_TOPIC + " and Physical Interface = " + draftPhysicalInterfaceResponse.get("id").getAsString() + " deleted = " + myClient.deleteEventMappingFromPhysicalInterface( draftPhysicalInterfaceResponse.get("id").getAsString(), EVT_TOPIC)); JsonObject deactivateOperation = new JsonObject(); deactivateOperation.addProperty("operation", SchemaOperation.DEACTIVATE.getOperation()); System.out.println("id" + draftLogicalInterfaceResponse.get("id").getAsString() + " deactivateOperation.toString()" + deactivateOperation.toString()); JsonObject deactivated = myClient.performOperationAgainstLogicalInterface( draftLogicalInterfaceResponse.get("id").getAsString(), deactivateOperation.toString()); System.out.println("\n16. Dectivate operation = " + deactivated.toString()); System.out.println("\n17. Draft Physical Interface with Id = " + draftPhysicalInterfaceResponse.get("id").getAsString() + " deleted = " + myClient .deleteDraftPhysicalInterface(draftPhysicalInterfaceResponse.get("id").getAsString())); System.out.println("\n18. Draft Event Type with Id = " + draftEventTypeId.toString() + " deleted = " + myClient.deleteDraftEventType(draftEventTypeId.getAsString())); System.out.println("\n19. Physical Schema with Id = " + draftSchemaId.toString() + " deleted = " + myClient.deleteDraftSchemaDefinition(draftSchemaId.getAsString())); System.out.println("\n20. Draft Logical Interface with Id = " + draftLogicalInterfaceResponse.get("id").getAsString() + " deleted = " + myClient.deleteDraftLogicalInterface(draftLogicalInterfaceResponse.get("id").getAsString())); System.out.println("\n21. Logical Schema with Id = " + draftLogicalSchemaId.toString() + " deleted = " + myClient.deleteDraftSchemaDefinition(draftLogicalSchemaId.getAsString())); } catch (Exception ioe) { ioe.printStackTrace(); } }