List of usage examples for com.google.gson JsonElement getAsJsonObject
public JsonObject getAsJsonObject()
From source file:adams.data.report.ReportJsonUtils.java
License:Open Source License
/** * Creates a report from the reader, reading in JSON. * * @param reader the reader to obtain the JSON from * @return the report, null if failed to create or find data * @throws Exception if reading/parsing fails *///from w w w. j a va 2 s . c o m public static Report fromJson(Reader reader) throws Exception { JsonParser jp; JsonElement je; jp = new JsonParser(); je = jp.parse(reader); return ReportJsonUtils.fromJson(je.getAsJsonObject()); }
From source file:allout58.mods.techtree.tree.ItemStackGSON.java
License:Open Source License
@Override public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject obj = json.getAsJsonObject(); final String name = obj.get("name").getAsString(); final Item it = (Item) Item.itemRegistry.getObject(name); final int meta = obj.get("meta").getAsInt(); return new ItemStack(it, 1, meta); }
From source file:allout58.mods.techtree.tree.TechNodeGSON.java
License:Open Source License
@Override public TechNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject obj = json.getAsJsonObject(); final int id = obj.get("id").getAsInt(); final String name = obj.get("name").getAsString(); final int science = obj.get("scienceRequired").getAsInt(); final String description = obj.get("description").getAsString(); final ItemStack[] items = context.deserialize(obj.get("lockedItems"), ItemStack[].class); final TechNode node = new TechNode(id); // ItemStack[] items = new ItemStack[] { new ItemStack(Items.apple), new ItemStack(Items.arrow), new ItemStack(Items.bow) }; node.setup(name, science, description, items); final JsonArray jsonParentArray = obj.getAsJsonArray("parents"); for (int i = 0; i < jsonParentArray.size(); i++) { node.addParentNode(jsonParentArray.get(i).getAsInt()); }//from w w w . ja va 2s . c om return node; }
From source file:ambari.interaction.NodeController.java
public static ArrayList<ComponentInformation> getListOfNodes() throws Exception { String url = "http://127.0.0.1:8080/api/v1/clusters/mycluster/services/HDFS/components/DATANODE?fields=host_components/HostRoles/desired_admin_state,host_components/HostRoles/state"; String responseJson = HttpURLConnectionExample.sendGet(url); System.out.print(responseJson); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse(responseJson); ArrayList<ComponentInformation> listOfCompoents = new ArrayList<ComponentInformation>(); if (jsonTree.isJsonObject()) { JsonObject jsonObject = jsonTree.getAsJsonObject(); JsonElement hostComponents = jsonObject.get("host_components"); if (hostComponents.isJsonArray()) { JsonArray hostComponentsJsonArray = hostComponents.getAsJsonArray(); System.out.println("\nStart"); System.out.println(hostComponentsJsonArray); for (int i = 0; i < hostComponentsJsonArray.size(); i++) { JsonElement hostComponentsElem = hostComponentsJsonArray.get(i); if (hostComponentsElem.isJsonObject()) { JsonObject jsonObjectElem = hostComponentsElem.getAsJsonObject(); JsonElement hostRolesElem = jsonObjectElem.get("HostRoles"); if (hostRolesElem.isJsonObject()) { JsonObject hostRolesObjs = hostRolesElem.getAsJsonObject(); JsonElement componentNameElem = hostRolesObjs.get("component_name"); JsonElement hostNameElem = hostRolesObjs.get("host_name"); JsonElement stateElem = hostRolesObjs.get("state"); listOfCompoents.add(new ComponentInformation(componentNameElem.toString(), hostNameElem.toString(), stateElem.toString())); }/*from ww w .jav a2 s. c o m*/ } } } } return listOfCompoents; }
From source file:angularBeans.remote.InvocationHandler.java
License:LGPL
private void update(Object o, JsonObject params) { if (params != null) { // boolean firstIn = false; for (Map.Entry<String, JsonElement> entry : params.entrySet()) { JsonElement value = entry.getValue(); String name = entry.getKey(); if ((name.equals("sessionUID")) || (name.equals("args"))) { continue; }//ww w . ja v a2 s . co m if ((value.isJsonObject()) && (!value.isJsonNull())) { String getName; try { getName = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name)); Method getter = o.getClass().getMethod(getName); Object subObj = getter.invoke(o); // logger.log(Level.INFO, "#entring sub object "+name); update(subObj, value.getAsJsonObject()); } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); } } // ------------------------------------ if (value.isJsonArray()) { try { String getter = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name)); Method get = o.getClass().getDeclaredMethod(getter); Type type = get.getGenericReturnType(); ParameterizedType pt = (ParameterizedType) type; Type actType = pt.getActualTypeArguments()[0]; String className = actType.toString(); className = className.substring(className.indexOf("class") + 6); Class clazz = Class.forName(className); JsonArray array = value.getAsJsonArray(); Collection collection = (Collection) get.invoke(o); Object elem; for (JsonElement element : array) { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); elem = element; if (primitive.isBoolean()) elem = primitive.getAsBoolean(); if (primitive.isString()) { elem = primitive.getAsString(); } if (primitive.isNumber()) elem = primitive.isNumber(); } else { elem = util.deserialise(clazz, element); } try { if (collection instanceof List) { if (collection.contains(elem)) collection.remove(elem); } collection.add(elem); } catch (UnsupportedOperationException e) { Logger.getLogger("AngularBeans").log(java.util.logging.Level.WARNING, "trying to modify an immutable collection : " + name); } } } catch (Exception e) { e.printStackTrace(); } } // ------------------------------------------ if (value.isJsonPrimitive() && (!name.equals("setSessionUID"))) { try { if (!CommonUtils.hasSetter(o.getClass(), name)) { continue; } name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Class type = null; for (Method set : o.getClass().getDeclaredMethods()) { if (CommonUtils.isSetter(set)) { if (set.getName().equals(name)) { Class<?>[] pType = set.getParameterTypes(); type = pType[0]; break; } } } if (type.equals(LobWrapper.class)) continue; Object param = null; if ((params.entrySet().size() >= 1) && (type != null)) { param = CommonUtils.convertFromString(value.getAsString(), type); } o.getClass().getMethod(name, type).invoke(o, param); } catch (Exception e) { e.printStackTrace(); } } } } }
From source file:angularBeans.util.AngularBeansUtil.java
License:Open Source License
public static JsonObject parse(String message) { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(message); JsonObject jObj = element.getAsJsonObject(); return jObj;// ww w.ja v a 2 s.c o m }
From source file:angularBeans.util.AngularBeansUtils.java
License:LGPL
public Object convertEvent(NGEvent event) throws ClassNotFoundException { JsonElement element = CommonUtils.parse(event.getData()); JsonElement data;/*ww w. ja va2s. com*/ Class javaClass; try { data = element.getAsJsonObject(); javaClass = Class.forName(event.getDataClass()); } catch (Exception e) { data = element.getAsJsonPrimitive(); if (event.getDataClass() == null) { event.setDataClass("String"); } javaClass = Class.forName("java.lang." + event.getDataClass()); } Object o; if (javaClass.equals(String.class)) { o = data.toString().substring(1, data.toString().length() - 1); } else { o = deserialise(javaClass, data); } return o; }
From source file:aopdomotics.storage.food.FoodJsonDecoder.java
/** * Decode bill, using storage array(with prices) and bill json and return total price. * @param billJson/* ww w . ja v a2 s .co m*/ * @param magazinList * @return */ public static float decodeBill(String billJson, ArrayList<Food> magazinList) { JsonElement jelement = new JsonParser().parse(billJson); JsonObject json = jelement.getAsJsonObject(); JsonElement billElement = json.get("Bill"); System.out.println("Bill JSON " + billElement.toString()); Gson gson = new GsonBuilder().create(); FoodJsonDecoder billdecoder = gson.fromJson(billElement, FoodJsonDecoder.class); return billdecoder.billPrice(magazinList); }
From source file:aopdomotics.storage.food.FoodJsonDecoder.java
/** * Decode recipe using the three food items from json recipe. * @param recipeJson/*from ww w . j a v a 2 s. co m*/ * @return */ public static Recipe decodeRecipe(String recipeJson) { Recipe recipe; Food[] component = new Food[3]; JsonElement jelement = new JsonParser().parse(recipeJson); JsonObject json = jelement.getAsJsonObject(); JsonElement recipeElement = json.get("Recipe"); System.out.println("Recipe JSON " + recipeElement.toString()); Gson gson = new GsonBuilder().create(); FoodJsonDecoder recipedecoder = gson.fromJson(recipeElement, FoodJsonDecoder.class); int i = 0; for (Food item : recipedecoder.foodList()) { if (item.quantity > 0) { component[i++] = item; } } recipe = new Recipe("eaten", component[0], component[1], component[2]); return recipe; }
From source file:aopdomotics.storage.food.FoodJsonDecoder.java
/** * Decode storage update json, return entire catalogue list * @param storageJson/*from w w w . j a v a 2s .c o m*/ * @return */ public static ArrayList<Food> decodeStorage(String storageJson) { System.out.println("Storage Json : " + storageJson); JsonElement jelement = new JsonParser().parse(storageJson); JsonObject json = jelement.getAsJsonObject(); JsonElement storageElement = json.get("Storage"); System.out.println("Storage JSON " + storageElement.toString()); Gson gson = new GsonBuilder().create(); FoodJsonDecoder recipedecoder = gson.fromJson(storageElement, FoodJsonDecoder.class); return recipedecoder.foodList(); }