List of usage examples for com.google.gson JsonParser parse
@Deprecated public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException
From source file:com.esotericsoftware.spine.SkeletonJson.java
License:Open Source License
public SkeletonData readSkeletonData(String folder, String file) { float scale = this.scale; SkeletonData skeletonData = new SkeletonData(); //skeletonData.name = file.nameWithoutExtension(); skeletonData.name = file;/*from ww w. j a va 2s. c o m*/ //JsonValue root = new JsonReader().parse(file); try (InputStreamReader isReader = new InputStreamReader( ResourceLoader.getResourceAsStream(folder + "/" + file + ".json"))) { JsonReader reader = new JsonReader(isReader); JsonParser parser = new JsonParser(); JsonObject root = (JsonObject) parser.parse(reader); // Skeleton. JsonObject skeletonMap = root.get("skeleton").getAsJsonObject(); if (skeletonMap != null) { skeletonData.hash = skeletonMap.has("hash") ? skeletonMap.get("hash").getAsString() : null; skeletonData.version = skeletonMap.has("spine") ? skeletonMap.get("spine").getAsString() : null; skeletonData.width = skeletonMap.has("width") ? skeletonMap.get("width").getAsFloat() : 0; skeletonData.height = skeletonMap.has("height") ? skeletonMap.get("height").getAsFloat() : 0; skeletonData.imagesPath = skeletonMap.has("images") ? skeletonMap.get("images").getAsString() : null; } // Bones. //for (JsonValue boneMap = root.getChild("bones"); boneMap != null; boneMap = boneMap.next) { for (JsonElement boneMap : root.get("bones").getAsJsonArray()) { JsonObject currentBone = boneMap.getAsJsonObject(); BoneData parent = null; String parentName = currentBone.has("parent") ? currentBone.get("parent").getAsString() : null; if (parentName != null) { parent = skeletonData.findBone(parentName); if (parent == null) throw new SerializationException("Parent bone not found: " + parentName); } BoneData boneData = new BoneData(currentBone.get("name").getAsString(), parent); boneData.length = (currentBone.has("length") ? currentBone.get("length").getAsFloat() : 0) * scale;//boneMap.getFloat("length", 0) * scale; boneData.x = (currentBone.has("x") ? currentBone.get("x").getAsFloat() : 0) * scale;//boneMap.getFloat("x", 0) * scale; boneData.y = (currentBone.has("y") ? currentBone.get("y").getAsFloat() : 0) * scale;//boneMap.getFloat("y", 0) * scale; boneData.rotation = currentBone.has("rotation") ? currentBone.get("rotation").getAsFloat() : 0;//boneMap.getFloat("rotation", 0); boneData.scaleX = currentBone.has("scaleX") ? currentBone.get("scaleX").getAsFloat() : 1;//boneMap.getFloat("scaleX", 1); boneData.scaleY = currentBone.has("scaleY") ? currentBone.get("scaleY").getAsFloat() : 1;//boneMap.getFloat("scaleY", 1); boneData.flipX = currentBone.has("flipX") ? currentBone.get("flipX").getAsBoolean() : false;//boneMap.getBoolean("flipX", false); boneData.flipY = currentBone.has("flipY") ? currentBone.get("flipY").getAsBoolean() : false;//boneMap.getBoolean("flipY", false); boneData.inheritScale = currentBone.has("inheritScale") ? currentBone.get("inheritScale").getAsBoolean() : true;//boneMap.getBoolean("inheritScale", true); boneData.inheritRotation = currentBone.has("inheritRotation") ? currentBone.get("inheritRotation").getAsBoolean() : true;//boneMap.getBoolean("inheritRotation", true); String color = currentBone.has("color") ? currentBone.get("color").getAsString() : null;//boneMap.getString("color", null); if (color != null) boneData.getColor().set(Color.valueOf(color)); skeletonData.bones.add(boneData); } // IK constraints. // TODO May return a JsonObject not Array //for (JsonValue ikMap = root.getChild("ik"); ikMap != null; ikMap = ikMap.next) { if (root.has("ik")) { for (JsonElement ikMap : root.get("ik").getAsJsonArray()) { JsonObject currentIk = ikMap.getAsJsonObject(); IkConstraintData ikConstraintData = new IkConstraintData(currentIk.get("name").getAsString()); //for (JsonValue boneMap = ikMap.getChild("bones"); boneMap != null; boneMap = boneMap.next) { for (JsonElement boneMap : currentIk.get("bones").getAsJsonArray()) { String boneName = boneMap.getAsString(); BoneData bone = skeletonData.findBone(boneName); if (bone == null) throw new SerializationException("IK bone not found: " + boneName); ikConstraintData.bones.add(bone); } String targetName = currentIk.get("target").getAsString(); ikConstraintData.target = skeletonData.findBone(targetName); if (ikConstraintData.target == null) throw new SerializationException("Target bone not found: " + targetName); ikConstraintData.bendDirection = (currentIk.has("bendPositive") ? currentIk.get("bendPositive").getAsBoolean() : true) ? 1 : -1;//ikMap.getBoolean("bendPositive", true) ? 1 : -1; ikConstraintData.mix = currentIk.has("mix") ? currentIk.get("mix").getAsFloat() : 1;//ikMap.getFloat("mix", 1); skeletonData.ikConstraints.add(ikConstraintData); } } // Slots. //for (JsonValue slotMap = root.getChild("slots"); slotMap != null; slotMap = slotMap.next) { for (JsonElement slotMap : root.get("slots").getAsJsonArray()) { JsonObject currentSlot = slotMap.getAsJsonObject(); String slotName = currentSlot.get("name").getAsString(); String boneName = currentSlot.get("bone").getAsString(); BoneData boneData = skeletonData.findBone(boneName); if (boneData == null) throw new SerializationException("Slot bone not found: " + boneName); SlotData slotData = new SlotData(slotName, boneData); String color = currentSlot.has("color") ? currentSlot.get("color").getAsString() : null;//slotMap.getString("color", null); if (color != null) slotData.getColor().set(Color.valueOf(color)); slotData.attachmentName = currentSlot.has("attachment") ? currentSlot.get("attachment").getAsString() : null;//slotMap.getString("attachment", null); slotData.additiveBlending = currentSlot.has("additive") ? currentSlot.get("additive").getAsBoolean() : false;//slotMap.getBoolean("additive", false); skeletonData.slots.add(slotData); } // Skins. //for (JsonValue skinMap = root.getChild("skins"); skinMap != null; skinMap = skinMap.next) { for (Entry<String, JsonElement> skinMap : root.get("skins").getAsJsonObject().entrySet()) { Skin skin = new Skin(skinMap.getKey()); //for (JsonValue slotEntry = skinMap.child; slotEntry != null; slotEntry = slotEntry.next) { for (Entry<String, JsonElement> slotEntry : skinMap.getValue().getAsJsonObject().entrySet()) { int slotIndex = skeletonData.findSlotIndex(slotEntry.getKey()); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotEntry.getKey()); //for (JsonValue entry = slotEntry.child; entry != null; entry = entry.next) { for (Entry<String, JsonElement> entry : slotEntry.getValue().getAsJsonObject().entrySet()) { Attachment attachment = readAttachment(skin, entry.getKey(), entry.getValue().getAsJsonObject()); if (attachment != null) { skin.addAttachment(slotIndex, entry.getKey(), attachment); } } } skeletonData.skins.add(skin); if (skin.name.equals("default")) skeletonData.defaultSkin = skin; } // Events. //for (JsonValue eventMap = root.getChild("events"); eventMap != null; eventMap = eventMap.next) { if (root.has("events")) { for (Entry<String, JsonElement> eventMap : root.get("events").getAsJsonObject().entrySet()) { JsonObject currentEvent = eventMap.getValue().getAsJsonObject(); EventData eventData = new EventData(eventMap.getKey()); eventData.intValue = currentEvent.has("int") ? currentEvent.get("int").getAsInt() : 0;//eventMap.getInt("int", 0); eventData.floatValue = currentEvent.has("float") ? currentEvent.get("float").getAsFloat() : 0f;//eventMap.getFloat("float", 0f); eventData.stringValue = currentEvent.has("string") ? currentEvent.get("string").getAsString() : null;//eventMap.getString("string", null); skeletonData.events.add(eventData); } } // Animations. //for (JsonValue animationMap = root.getChild("animations"); animationMap != null; animationMap = animationMap.next) for (Entry<String, JsonElement> animationMap : root.get("animations").getAsJsonObject().entrySet()) { readAnimation(animationMap.getKey(), animationMap.getValue().getAsJsonObject(), skeletonData); } } catch (JsonIOException | IOException e) { e.printStackTrace(); } /*skeletonData.bones.shrink(); skeletonData.slots.shrink(); skeletonData.skins.shrink(); skeletonData.animations.shrink();*/ return skeletonData; }
From source file:com.exalttech.trex.util.Util.java
License:Apache License
/** * Convert JSON to pretty format/*from w w w . j av a 2 s . c om*/ * * @param jsonString * @return */ public static String toPrettyFormat(String jsonString) { try { JsonParser parser = new JsonParser(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); // Check if it is an Array if ('[' == jsonString.charAt(0)) { JsonArray jsonArray = parser.parse(jsonString).getAsJsonArray(); return getSubString(gson.toJson(jsonArray)); } else { JsonObject json = parser.parse(jsonString).getAsJsonObject(); return getSubString(gson.toJson(json)); } } catch (JsonSyntaxException ex) { // return the original string in case of exception LOG.error("Error formatting string", ex); return getSubString(jsonString); } }
From source file:com.example.counter.LoadSave.java
License:GNU General Public License
public List<CounterModel> loadFromFile(String FILENAME2, List<CounterModel> objList, Context appcontext) { try {//from w w w. ja v a 2s.co m FileInputStream fis = appcontext.openFileInput(FILENAME2); //http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonArray jArray = parser.parse(new InputStreamReader(fis)).getAsJsonArray(); for (JsonElement obj : jArray) { CounterModel cam = gson.fromJson(obj, CounterModel.class); objList.add(cam); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return objList; }
From source file:com.facebook.ads.sdk.AdAccount.java
License:Open Source License
public static AdAccount loadJSON(String json, APIContext context) { AdAccount adAccount = getGson().fromJson(json, AdAccount.class); if (context.isDebug()) { JsonParser parser = new JsonParser(); JsonElement o1 = parser.parse(json); JsonElement o2 = parser.parse(adAccount.toString()); if (o1.getAsJsonObject().get("__fb_trace_id__") != null) { o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__")); }/*from ww w.j a va2 s. com*/ if (!o1.equals(o2)) { context.log("[Warning] When parsing response, object is not consistent with JSON:"); context.log("[JSON]" + o1); context.log("[Object]" + o2); } ; } adAccount.context = context; adAccount.rawValue = json; JsonParser parser = new JsonParser(); JsonObject o = parser.parse(json).getAsJsonObject(); if (o.has("account_id")) { String accountId = o.get("account_id").getAsString(); if (accountId != null) { adAccount.mId = accountId; } } if (adAccount.mId != null) { adAccount.mId = adAccount.mId.replaceAll("act_", ""); } return adAccount; }
From source file:com.facebook.ads.sdk.AdAccount.java
License:Open Source License
public static APINodeList<AdAccount> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccount> adAccounts = new APINodeList<AdAccount>(request, json); JsonArray arr;/*w w w. j ava 2 s . c o m*/ JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccounts; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccounts.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccounts.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccounts.add(loadJSON(obj.toString(), context)); } } return adAccounts; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccounts.add(loadJSON(entry.getValue().toString(), context)); } return adAccounts; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccounts.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccounts; } // Sixth, check if it's pure JsonObject adAccounts.clear(); adAccounts.add(loadJSON(json, context)); return adAccounts; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountDeliveryEstimate.java
License:Open Source License
public static AdAccountDeliveryEstimate loadJSON(String json, APIContext context) { AdAccountDeliveryEstimate adAccountDeliveryEstimate = getGson().fromJson(json, AdAccountDeliveryEstimate.class); if (context.isDebug()) { JsonParser parser = new JsonParser(); JsonElement o1 = parser.parse(json); JsonElement o2 = parser.parse(adAccountDeliveryEstimate.toString()); if (o1.getAsJsonObject().get("__fb_trace_id__") != null) { o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__")); }/* ww w.j av a 2s . co m*/ if (!o1.equals(o2)) { context.log("[Warning] When parsing response, object is not consistent with JSON:"); context.log("[JSON]" + o1); context.log("[Object]" + o2); } ; } adAccountDeliveryEstimate.context = context; adAccountDeliveryEstimate.rawValue = json; return adAccountDeliveryEstimate; }
From source file:com.facebook.ads.sdk.AdAccountDeliveryEstimate.java
License:Open Source License
public static APINodeList<AdAccountDeliveryEstimate> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountDeliveryEstimate> adAccountDeliveryEstimates = new APINodeList<AdAccountDeliveryEstimate>( request, json);/*from ww w. j a v a 2 s .c o m*/ JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountDeliveryEstimates.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountDeliveryEstimates; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccountDeliveryEstimates.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountDeliveryEstimates .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountDeliveryEstimates.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountDeliveryEstimates.add(loadJSON(obj.toString(), context)); } } return adAccountDeliveryEstimates; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountDeliveryEstimates.add(loadJSON(entry.getValue().toString(), context)); } return adAccountDeliveryEstimates; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccountDeliveryEstimates.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountDeliveryEstimates; } // Sixth, check if it's pure JsonObject adAccountDeliveryEstimates.clear(); adAccountDeliveryEstimates.add(loadJSON(json, context)); return adAccountDeliveryEstimates; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountGroup.java
License:Open Source License
public static AdAccountGroup loadJSON(String json, APIContext context) { AdAccountGroup adAccountGroup = getGson().fromJson(json, AdAccountGroup.class); if (context.isDebug()) { JsonParser parser = new JsonParser(); JsonElement o1 = parser.parse(json); JsonElement o2 = parser.parse(adAccountGroup.toString()); if (o1.getAsJsonObject().get("__fb_trace_id__") != null) { o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__")); }//w ww .ja va 2s . c o m if (!o1.equals(o2)) { context.log("[Warning] When parsing response, object is not consistent with JSON:"); context.log("[JSON]" + o1); context.log("[Object]" + o2); } ; } adAccountGroup.context = context; adAccountGroup.rawValue = json; return adAccountGroup; }
From source file:com.facebook.ads.sdk.AdAccountGroup.java
License:Open Source License
public static APINodeList<AdAccountGroup> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountGroup> adAccountGroups = new APINodeList<AdAccountGroup>(request, json); JsonArray arr;/*w w w. j ava 2s. co m*/ JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountGroups; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccountGroups.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountGroups.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountGroups.add(loadJSON(obj.toString(), context)); } } return adAccountGroups; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountGroups.add(loadJSON(entry.getValue().toString(), context)); } return adAccountGroups; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccountGroups.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountGroups; } // Sixth, check if it's pure JsonObject adAccountGroups.clear(); adAccountGroups.add(loadJSON(json, context)); return adAccountGroups; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountGroupResult.java
License:Open Source License
public static AdAccountGroupResult loadJSON(String json, APIContext context) { AdAccountGroupResult adAccountGroupResult = getGson().fromJson(json, AdAccountGroupResult.class); if (context.isDebug()) { JsonParser parser = new JsonParser(); JsonElement o1 = parser.parse(json); JsonElement o2 = parser.parse(adAccountGroupResult.toString()); if (o1.getAsJsonObject().get("__fb_trace_id__") != null) { o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__")); }//from w ww. ja v a 2 s.co m if (!o1.equals(o2)) { context.log("[Warning] When parsing response, object is not consistent with JSON:"); context.log("[JSON]" + o1); context.log("[Object]" + o2); } ; } adAccountGroupResult.context = context; adAccountGroupResult.rawValue = json; return adAccountGroupResult; }