List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:com.esotericsoftware.spine.SkeletonJson.java
License:Open Source License
private void readAnimation(String name, JsonObject map, SkeletonData skeletonData) { float scale = this.scale; //Array<Timeline> timelines = new Array(); ArrayList<Timeline> timelines = new ArrayList<Timeline>(); float duration = 0; // Slot timelines. //for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) { if (map.has("slots")) { for (Entry<String, JsonElement> slotMap : map.get("slots").getAsJsonObject().entrySet()) { int slotIndex = skeletonData.findSlotIndex(slotMap.getKey()); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.getKey()); //for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) { for (Entry<String, JsonElement> timelineMap : slotMap.getValue().getAsJsonObject().entrySet()) { String timelineName = timelineMap.getKey(); if (timelineName.equals("color")) { ColorTimeline timeline = new ColorTimeline(timelineMap.getValue().getAsJsonArray().size()); timeline.slotIndex = slotIndex; int frameIndex = 0; //for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { for (JsonElement valueMap : timelineMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); Color color = Color.valueOf(currentValue.get("color").getAsString()); timeline.setFrame(frameIndex, currentValue.get("time").getAsFloat(), color.r, color.g, color.b, color.a); readCurve(timeline, frameIndex, currentValue); frameIndex++;//from ww w. j a va2 s . c om } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]); } else if (timelineName.equals("attachment")) { AttachmentTimeline timeline = new AttachmentTimeline( timelineMap.getValue().getAsJsonArray().size()); timeline.slotIndex = slotIndex; int frameIndex = 0; //for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) for (JsonElement valueMap : timelineMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); timeline.setFrame(frameIndex++, currentValue.get("time").getAsFloat(), currentValue.get("name") instanceof JsonNull ? null : currentValue.get("name").getAsString()); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } else throw new RuntimeException("Invalid timeline type for a slot: " + timelineName + " (" + slotMap.getKey() + ")"); } } } // Bone timelines. //for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) { if (map.has("bones")) { for (Entry<String, JsonElement> boneMap : map.get("bones").getAsJsonObject().entrySet()) { int boneIndex = skeletonData.findBoneIndex(boneMap.getKey()); if (boneIndex == -1) throw new SerializationException("Bone not found: " + boneMap.getKey()); //for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) { for (Entry<String, JsonElement> timelineMap : boneMap.getValue().getAsJsonObject().entrySet()) { String timelineName = timelineMap.getKey(); if (timelineName.equals("rotate")) { RotateTimeline timeline = new RotateTimeline( timelineMap.getValue().getAsJsonArray().size()); timeline.boneIndex = boneIndex; int frameIndex = 0; //for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { for (JsonElement valueMap : timelineMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); timeline.setFrame(frameIndex, currentValue.get("time").getAsFloat(), currentValue.get("angle").getAsFloat()); readCurve(timeline, frameIndex, currentValue); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]); } else if (timelineName.equals("translate") || timelineName.equals("scale")) { TranslateTimeline timeline; float timelineScale = 1; if (timelineName.equals("scale")) timeline = new ScaleTimeline(timelineMap.getValue().getAsJsonArray().size()); else { timeline = new TranslateTimeline(timelineMap.getValue().getAsJsonArray().size()); timelineScale = scale; } timeline.boneIndex = boneIndex; int frameIndex = 0; //for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { for (JsonElement valueMap : timelineMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); float x = currentValue.has("x") ? currentValue.get("x").getAsFloat() : 0;//valueMap.getFloat("x", 0); float y = currentValue.has("y") ? currentValue.get("y").getAsFloat() : 0;//valueMap.getFloat("y", 0); timeline.setFrame(frameIndex, currentValue.get("time").getAsFloat(), x * timelineScale, y * timelineScale); readCurve(timeline, frameIndex, currentValue); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]); } else if (timelineName.equals("flipX") || timelineName.equals("flipY")) { boolean x = timelineName.equals("flipX"); FlipXTimeline timeline = x ? new FlipXTimeline(timelineMap.getValue().getAsJsonArray().size()) : new FlipYTimeline(timelineMap.getValue().getAsJsonArray().size()); timeline.boneIndex = boneIndex; String field = x ? "x" : "y"; int frameIndex = 0; //for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) { for (JsonElement valueMap : timelineMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); timeline.setFrame(frameIndex, currentValue.get("time").getAsFloat(), currentValue.has(field) ? currentValue.get(field).getAsBoolean() : false); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]); } else throw new RuntimeException("Invalid timeline type for a bone: " + timelineName + " (" + boneMap.getKey() + ")"); } } } // IK timelines. //for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) { if (map.has("ik")) { for (Entry<String, JsonElement> ikMap : map.get("ik").getAsJsonObject().entrySet()) { IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.getKey()); IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.getValue().getAsJsonArray().size()); timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint); int frameIndex = 0; //for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) { for (JsonElement valueMap : ikMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); timeline.setFrame(frameIndex, currentValue.get("time").getAsFloat(), currentValue.get("mix").getAsFloat(), currentValue.get("bendPositive").getAsBoolean() ? 1 : -1); readCurve(timeline, frameIndex, currentValue); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]); } } // FFD timelines. //for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) { if (map.has("ffd")) { for (Entry<String, JsonElement> ffdMap : map.get("ffd").getAsJsonObject().entrySet()) { Skin skin = skeletonData.findSkin(ffdMap.getKey()); if (skin == null) throw new SerializationException("Skin not found: " + ffdMap.getKey()); //for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) { for (Entry<String, JsonElement> slotMap : ffdMap.getValue().getAsJsonObject().entrySet()) { int slotIndex = skeletonData.findSlotIndex(slotMap.getKey()); if (slotIndex == -1) throw new SerializationException("Slot not found: " + slotMap.getKey()); //for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) { for (Entry<String, JsonElement> meshMap : slotMap.getValue().getAsJsonObject().entrySet()) { FfdTimeline timeline = new FfdTimeline(meshMap.getValue().getAsJsonArray().size()); Attachment attachment = skin.getAttachment(slotIndex, meshMap.getKey()); if (attachment == null) throw new SerializationException("FFD attachment not found: " + meshMap.getKey()); timeline.slotIndex = slotIndex; timeline.attachment = attachment; int vertexCount; if (attachment instanceof MeshAttachment) vertexCount = ((MeshAttachment) attachment).getVertices().length; else vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2; int frameIndex = 0; //for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) { for (JsonElement valueMap : meshMap.getValue().getAsJsonArray()) { JsonObject currentValue = valueMap.getAsJsonObject(); float[] vertices; //JsonValue verticesValue = valueMap.get("vertices"); JsonElement verticesValue = currentValue.get("vertices"); if (verticesValue == null) { if (attachment instanceof MeshAttachment) vertices = ((MeshAttachment) attachment).getVertices(); else vertices = new float[vertexCount]; } else { vertices = new float[vertexCount]; int start = currentValue.has("offset") ? currentValue.get("offset").getAsInt() : 0;//valueMap.getInt("offset", 0); float[] currentVertices = new float[verticesValue.getAsJsonArray().size()]; for (int f = 0; f < currentVertices.length; f++) { currentVertices[f] = verticesValue.getAsJsonArray().get(f).getAsFloat(); } System.arraycopy(currentVertices, 0, vertices, start, verticesValue.getAsJsonArray().size()); if (scale != 1) { for (int i = start, n = i + verticesValue.getAsJsonArray().size(); i < n; i++) vertices[i] *= scale; } if (attachment instanceof MeshAttachment) { float[] meshVertices = ((MeshAttachment) attachment).getVertices(); for (int i = 0; i < vertexCount; i++) vertices[i] += meshVertices[i]; } } timeline.setFrame(frameIndex, currentValue.get("time").getAsFloat(), vertices); readCurve(timeline, frameIndex, currentValue); frameIndex++; } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } } } } // Draw order timeline. //JsonValue drawOrdersMap = map.get("drawOrder"); JsonElement drawOrdersMap = map.get("drawOrder"); if (drawOrdersMap == null) drawOrdersMap = map.get("draworder"); if (drawOrdersMap != null) { DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.getAsJsonArray().size()); int slotCount = skeletonData.slots.size(); int frameIndex = 0; //for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) { for (JsonElement drawOrderMap : drawOrdersMap.getAsJsonArray()) { int[] drawOrder = null; //JsonValue offsets = drawOrderMap.get("offsets"); JsonElement offsets = drawOrderMap.getAsJsonObject().get("offsets"); if (offsets != null) { drawOrder = new int[slotCount]; for (int i = slotCount - 1; i >= 0; i--) drawOrder[i] = -1; int[] unchanged = new int[slotCount - offsets.getAsJsonArray().size()]; int originalIndex = 0, unchangedIndex = 0; //for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) { for (JsonElement offsetMap : offsets.getAsJsonArray()) { JsonObject currentOffset = offsetMap.getAsJsonObject(); int slotIndex = skeletonData.findSlotIndex(currentOffset.get("slot").getAsString()); if (slotIndex == -1) throw new SerializationException( "Slot not found: " + currentOffset.get("slot").getAsString()); // Collect unchanged items. while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++; // Set changed items. drawOrder[originalIndex + currentOffset.get("offset").getAsInt()] = originalIndex++; } // Collect remaining unchanged items. while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++; // Fill in unchanged items. for (int i = slotCount - 1; i >= 0; i--) if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex]; } timeline.setFrame(frameIndex++, drawOrderMap.getAsJsonObject().get("time").getAsFloat(), drawOrder); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } // Event timeline. //JsonValue eventsMap = map.get("events"); JsonElement eventsMap = map.get("events"); if (eventsMap != null) { EventTimeline timeline = new EventTimeline(eventsMap.getAsJsonArray().size()); int frameIndex = 0; //for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) { for (JsonElement eventMap : eventsMap.getAsJsonArray()) { JsonObject currentEvent = eventMap.getAsJsonObject(); EventData eventData = skeletonData.findEvent(currentEvent.get("name").getAsString()); if (eventData == null) throw new SerializationException("Event not found: " + currentEvent.get("name").getAsString()); Event event = new Event(eventData); event.intValue = currentEvent.has("int") ? currentEvent.get("int").getAsInt() : eventData.getInt();//eventMap.getInt("int", eventData.getInt()); event.floatValue = currentEvent.has("float") ? currentEvent.get("float").getAsFloat() : eventData.getFloat();//eventMap.getFloat("float", eventData.getFloat()); event.stringValue = currentEvent.has("string") ? currentEvent.get("string").getAsString() : eventData.getString();//eventMap.getString("string", eventData.getString()); timeline.setFrame(frameIndex++, currentEvent.get("time").getAsFloat(), event); } timelines.add(timeline); duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]); } //timelines.shrink(); skeletonData.animations.add(new Animation(name, timelines, duration)); }
From source file:com.esotericsoftware.spine.SkeletonJson.java
License:Open Source License
void readCurve(CurveTimeline timeline, int frameIndex, JsonObject valueMap) { JsonElement curve = valueMap.has("curve") ? valueMap.get("curve") : null; if (curve == null) return;//w w w . j a va 2s . c om if (curve.isJsonPrimitive() && curve.getAsString().equals("stepped")) timeline.setStepped(frameIndex); else if (curve.isJsonArray()) { JsonArray curveArray = curve.getAsJsonArray(); timeline.setCurve(frameIndex, curveArray.get(0).getAsFloat(), curveArray.get(1).getAsFloat(), curveArray.get(2).getAsFloat(), curveArray.get(3).getAsFloat()); } }
From source file:com.example.android.xyztouristattractions.ui.AttractionListFragment.java
License:Open Source License
@Override public void success(JsonElement string, Response response) { JsonObject mJsonObject = null;/*from w ww . ja v a 2 s.c om*/ List<Attraction> attractionsList = new ArrayList<Attraction>(); if (string.isJsonObject()) { // Toast.makeText(getActivity(), "OnSuccess--Object" + string.toString(), Toast.LENGTH_LONG).show(); mJsonObject = string.getAsJsonObject(); JsonElement mElement = mJsonObject.get("attractions"); if (mElement.isJsonArray()) { JsonArray mArray = mElement.getAsJsonArray(); HashMap<String, List<Attraction>> ATTRACTIONS = new HashMap<String, List<Attraction>>(); String city = ""; for (JsonElement element : mArray) { JsonObject attractions = null; if (element.isJsonObject()) { attractions = element.getAsJsonObject(); JsonObject imageURl = attractions.get("imageUrl").getAsJsonObject(); JsonObject secImageURl = attractions.get("secondaryImageUrl").getAsJsonObject(); JsonObject location = attractions.get("location").getAsJsonObject(); city = attractions.get("city").toString(); // Toast.makeText(getActivity(), "OnSuccess--" + attractions.get("name") + imageURl.get("uriString") // + attractions.get("city") + attractions.get("longDescription") // + attractions.get("description"), Toast.LENGTH_LONG).show(); // String name, String description, String longDescription, Uri imageUrl, // Uri secondaryImageUrl, LatLng location, String city attractionsList.add(new Attraction(attractions.get("name").toString(), attractions.get("description").toString(), attractions.get("longDescription").toString(), Uri.parse(imageURl.get("uriString").toString()), Uri.parse(secImageURl.get("uriString").toString()), new LatLng(Float.parseFloat(location.get("latitude").toString()), Float.parseFloat(location.get("longitude").toString())), attractions.get("city").toString())); Log.d(AttractionListFragment.class.getSimpleName(), imageURl.get("uriString").toString()); Log.d(AttractionListFragment.class.getSimpleName(), secImageURl.get("uriString").toString()); } else Toast.makeText(getActivity(), "OnSuccess--Object--Array--Not Object", Toast.LENGTH_LONG) .show(); } ATTRACTIONS.put(city, attractionsList); } else Toast.makeText(getActivity(), "OnSuccess--Object--Not Array", Toast.LENGTH_LONG).show(); } else Toast.makeText(getActivity(), "OnSuccess--Not Object", Toast.LENGTH_LONG).show(); // DataModel model = new Gson().fromJson(string.toString(), DataModel.class); // List<Attraction> attractions = model.getAttractions(); mAdapter = new AttractionAdapter(getActivity(), attractionsList); recyclerView.setAdapter(mAdapter); }
From source file:com.exsoloscript.challonge.gson.MatchListAdapter.java
License:Apache License
@Override public List<Match> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { if (jsonElement.isJsonArray()) { JsonArray array = jsonElement.getAsJsonArray(); List<Match> matches = Lists.newArrayList(); for (JsonElement arrayElement : array) { matches.add(this.gson.fromJson(arrayElement, Match.class)); }//from w ww . ja v a2 s . c o m return matches; } return null; }
From source file:com.exsoloscript.challonge.gson.ParticipantListAdapter.java
License:Apache License
@Override public List<Participant> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { if (jsonElement.isJsonArray()) { JsonArray array = jsonElement.getAsJsonArray(); List<Participant> participants = Lists.newArrayList(); for (JsonElement arrayElement : array) { participants.add(this.gson.fromJson(arrayElement, Participant.class)); }/* w w w .j av a 2 s. c o m*/ return participants; } return null; }
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;/*from w ww .java 2 s . com*/ 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 APINodeList<AdAccountDeliveryEstimate> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountDeliveryEstimate> adAccountDeliveryEstimates = new APINodeList<AdAccountDeliveryEstimate>( request, json);/*from w ww . 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 APINodeList<AdAccountGroup> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountGroup> adAccountGroups = new APINodeList<AdAccountGroup>(request, json); JsonArray arr;/* ww w. j a va 2s . c om*/ 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 APINodeList<AdAccountGroupResult> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountGroupResult> adAccountGroupResults = new APINodeList<AdAccountGroupResult>(request, json);// ww w . j a v a 2 s .co 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++) { adAccountGroupResults.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountGroupResults; } 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; adAccountGroupResults.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++) { adAccountGroupResults.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()) { adAccountGroupResults.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountGroupResults.add(loadJSON(obj.toString(), context)); } } return adAccountGroupResults; } 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()) { adAccountGroupResults.add(loadJSON(entry.getValue().toString(), context)); } return adAccountGroupResults; } 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)) { adAccountGroupResults.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountGroupResults; } // Sixth, check if it's pure JsonObject adAccountGroupResults.clear(); adAccountGroupResults.add(loadJSON(json, context)); return adAccountGroupResults; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountRoas.java
License:Open Source License
public static APINodeList<AdAccountRoas> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountRoas> adAccountRoass = new APINodeList<AdAccountRoas>(request, json); JsonArray arr;/*from ww w. j av a 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++) { adAccountRoass.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountRoass; } 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; adAccountRoass.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++) { adAccountRoass.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()) { adAccountRoass.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountRoass.add(loadJSON(obj.toString(), context)); } } return adAccountRoass; } 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()) { adAccountRoass.add(loadJSON(entry.getValue().toString(), context)); } return adAccountRoass; } 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)) { adAccountRoass.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountRoass; } // Sixth, check if it's pure JsonObject adAccountRoass.clear(); adAccountRoass.add(loadJSON(json, context)); return adAccountRoass; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }