List of usage examples for com.google.gson JsonObject has
public boolean has(String memberName)
From source file:com.driver733.vkuploader.wallpost.attachment.support.string.AttachmentStringFromJson.java
License:Open Source License
@Override @SuppressWarnings("PMD.AvoidDuplicateLiterals") public String attachmentString() throws IOException { final String formatted; final JsonObject object; if (this.element.isJsonPrimitive()) { formatted = String.format("%s%d_%d", AttachmentType.AUDIO, -this.group, this.element.getAsInt()); } else if (this.element.isJsonObject()) { object = this.element.getAsJsonObject(); if (object.has("artist")) { formatted = String.format("%s%d_%d", AttachmentType.AUDIO.toString(), object.get("owner_id").getAsInt(), object.get("id").getAsInt()); } else if (object.has("photo_75")) { formatted = String.format("%s%d_%d", AttachmentType.PHOTO, object.get("owner_id").getAsInt(), object.get("id").getAsInt()); } else if (object.has("ext")) { formatted = String.format("%s%d_%d", AttachmentType.DOC, object.get("owner_id").getAsInt(), object.get("id").getAsInt()); } else {//w ww. jav a 2s. c om throw new IOException("Unknown AttachmentFormat"); } } else { throw new IOException("Unknown AttachmentFormat"); } return formatted; }
From source file:com.elevenpaths.latch.sdk.impl.LatchSDKImpl.java
License:Open Source License
/** * Returns the status of the latch identified by the provided * account id and, if not null, the provided operation id. * <p>//from w ww . j a va 2 s . c om * If something has gone wrong trying to get the latch status an * special <code>LatchSDKStatus</code> value will be returned ( * <code>UNKNOWN</code>), so the application can choose what to * do. * <p> * If the backend returns a JSON with an error, this error is * returned as a <code>LatchErrorException</code>. It's also * responsibility of the application using this method to choose * what to do. * * @param accountId * The account id that identifies the latch being checked. * @param operationId * The operation id that identifies the latch being * checked. If the application has no operations, it should * be null. * * @return The status of the latch. * * @throws LatchErrorException * If the backend returns a JSON with an error. */ public LatchSDKStatus parsedStatus(String accountId, String operationId) throws LatchErrorException { Utils.checkMethodRequiredParameter(LOG, "accountId", accountId); /* The default status is UNKNOWN */ LatchSDKStatus rv = LatchSDKStatus.UNKNOWN; /* Connect to the server and get the JSON response to the status request */ LatchResponse latchResponse = null; try { if (operationId == null) { latchResponse = status(accountId); } else { latchResponse = operationStatus(accountId, operationId); } } catch (Exception e) { LOG.error("And exception has been thrown when communicating with Latch backend", e); return rv; } /* Check if a response has been received and parsed correctly */ if (latchResponse != null) { if (latchResponse.getData() != null) { /* The backend has responded with data */ JsonObject jsonObject = (JsonObject) latchResponse.getData(); if (jsonObject.has("operations")) { JsonObject operations = jsonObject.getAsJsonObject("operations"); JsonObject latch = null; if (operationId == null) { if (operations.has(applicationId)) { latch = operations.getAsJsonObject(applicationId); } } else { if (operations.has(operationId)) { latch = operations.getAsJsonObject(operationId); } } if (latch != null && latch.has("status")) { if ("off".equals(latch.get("status").getAsString())) { rv = LatchSDKStatus.LOCKED; } else if ("on".equals(latch.get("status").getAsString())) { rv = LatchSDKStatus.UNLOCKED; } } } } else if (latchResponse.getError() != null) { /* The backend has responded with an application error message */ LOG.error( "The backend has responded with an application error message: " + latchResponse.getError()); throw new LatchErrorException(latchResponse.getError()); } } return rv; }
From source file:com.enablens.dfa.base.DcnmAuthToken.java
License:Open Source License
/** * Authenticate./*from w w w. j a v a2 s .co m*/ */ private void authenticate() { authTime = System.currentTimeMillis(); HttpResponse<String> response; int responseCode; try { response = Unirest.post("http://" + server + "/rest/logon/").basicAuth(username, password) .body("{expiration: " + tokenLife + "}").asString(); responseCode = response.getCode(); } catch (final UnirestException e) { token = ""; state = DcnmResponseStates.NET_ERROR; return; } responseCode = response.getCode(); if (responseCode == HTTP_OK) { final JsonParser parser = new JsonParser(); final JsonObject jsonObject = parser.parse(response.getBody().trim()).getAsJsonObject(); if (jsonObject.has(DCNM_TOKEN_KEY)) { token = jsonObject.get(DCNM_TOKEN_KEY).getAsString(); state = DcnmResponseStates.VALID; } else { // No token returned - DCNM returns {} // if credentials are incorrect token = ""; state = DcnmResponseStates.SERVERSIDE_ERROR; } } else { // Not Response Code 200 token = ""; state = DcnmResponseStates.WEB_FAILURE; } }
From source file:com.epishie.tabs.gson.ThingDeserializer.java
License:Apache License
@Override public Thing deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Expected json object"); }//from w w w . j a v a2 s . c om JsonObject jsonObject = json.getAsJsonObject(); if (!jsonObject.has(KIND)) { throw new JsonParseException("Expected to have \"kind\" field"); } JsonElement kindElement = jsonObject.get(KIND); if (!kindElement.isJsonPrimitive()) { throw new JsonParseException("Expected to have a primitive \"kind\" field"); } if (!jsonObject.has(DATA)) { throw new JsonParseException("Expected to have a\"data\" field"); } String kind = kindElement.getAsString(); String id = null; String name = null; if (jsonObject.has(ID)) { JsonElement idElement = jsonObject.get(ID); id = idElement.getAsString(); } if (jsonObject.has(NAME)) { JsonElement nameElement = jsonObject.get(NAME); name = nameElement.getAsString(); } Thing thing = null; switch (kind) { case Link.KIND: Link link = context.deserialize(jsonObject.get(DATA), Link.class); thing = new Thing<>(id, name, link); break; case Listing.KIND: Listing listing = context.deserialize(jsonObject.get(DATA), Listing.class); // noinspection unchecked thing = new Thing(id, name, listing); break; case Subreddit.KIND: Subreddit subreddit = context.deserialize(jsonObject.get(DATA), Subreddit.class); // noinspection unchecked thing = new Thing(id, name, subreddit); break; case Comment.KIND: Comment comment = context.deserialize(jsonObject.get(DATA), Comment.class); // noinspection unchecked thing = new Thing(id, name, comment); break; } return thing; }
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;//w w w . j a v a2 s . 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.esotericsoftware.spine.SkeletonJson.java
License:Open Source License
@SuppressWarnings("null") private Attachment readAttachment(Skin skin, String name, JsonObject map) { float scale = this.scale; name = map.has("name") ? map.get("name").getAsString() : name;//map.getString("name", name); String path = map.has("path") ? map.get("path").getAsString() : name;//map.getString("path", name); //switch (AttachmentType.valueOf(map.getString("type", AttachmentType.region.name()))) { switch (AttachmentType .valueOf(map.has("type") ? map.get("type").getAsString() : AttachmentType.region.name())) { case region: { RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, path); if (region == null) return null; region.setPath(path);/*from www .j av a2 s. c o m*/ region.setX((map.has("x") ? map.get("x").getAsFloat() : 0) * scale); region.setY((map.has("y") ? map.get("y").getAsFloat() : 0) * scale); region.setScaleX(map.has("scaleX") ? map.get("scaleX").getAsFloat() : 1); region.setScaleY(map.has("scaleY") ? map.get("scaleY").getAsFloat() : 1); region.setRotation(map.has("rotation") ? map.get("rotation").getAsFloat() : 0); region.setWidth(map.get("width").getAsFloat() * scale); region.setHeight(map.get("height").getAsFloat() * scale); String color = map.has("color") ? map.get("color").getAsString() : null;//map.getString("color", null); if (color != null) region.getColor().set(Color.valueOf(color)); region.updateOffset(); return region; } case boundingbox: { // TODO Can't implement without proper Spine-Pro reference BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name); if (box == null) return null; //float[] vertices = map.require("vertices").asFloatArray(); float[] vertices = null; if (scale != 1) { for (int i = 0, n = vertices.length; i < n; i++) vertices[i] *= scale; } box.setVertices(vertices); return box; } case mesh: { // TODO Can't implement without proper Spine-Pro reference MeshAttachment mesh = attachmentLoader.newMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] vertices = null;//map.require("vertices").asFloatArray(); if (scale != 1) { for (int i = 0, n = vertices.length; i < n; i++) vertices[i] *= scale; } mesh.setVertices(vertices); //mesh.setTriangles(map.require("triangles").asShortArray()); //mesh.setRegionUVs(map.require("uvs").asFloatArray()); mesh.updateUVs(); String color = map.has("color") ? map.get("color").getAsString() : null;//map.getString("color", null); if (color != null) mesh.getColor().set(Color.valueOf(color)); //if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); //if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); mesh.setWidth((map.has("width") ? map.get("width").getAsFloat() : 0) * scale); mesh.setHeight((map.has("height") ? map.get("height").getAsFloat() : 0) * scale); return mesh; } case skinnedmesh: { // TODO Can't implement without proper Spine-Pro reference SkinnedMeshAttachment mesh = attachmentLoader.newSkinnedMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); //float[] uvs = map.require("uvs").asFloatArray(); float[] uvs = null; //float[] vertices = map.require("vertices").asFloatArray(); float[] vertices = null; //FloatArray weights = new FloatArray(uvs.length * 3 * 3); ArrayList<Float> weights = new ArrayList<Float>(); //IntArray bones = new IntArray(uvs.length * 3); ArrayList<Integer> bones = new ArrayList<Integer>(); for (int i = 0, n = vertices.length; i < n;) { int boneCount = (int) vertices[i++]; bones.add(boneCount); for (int nn = i + boneCount * 4; i < nn;) { bones.add((int) vertices[i]); weights.add(vertices[i + 1] * scale); weights.add(vertices[i + 2] * scale); weights.add(vertices[i + 3]); i += 4; } } //mesh.setBones(bones.toArray()); //mesh.setWeights(weights.toArray()); //mesh.setTriangles(map.require("triangles").asShortArray()); mesh.setRegionUVs(uvs); mesh.updateUVs(); String color = map.has("color") ? map.get("color").getAsString() : null;//map.getString("color", null); if (color != null) mesh.getColor().set(Color.valueOf(color)); //if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); //if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); mesh.setWidth((map.has("width") ? map.get("width").getAsFloat() : 0) * scale); mesh.setHeight((map.has("height") ? map.get("height").getAsFloat() : 0) * scale); return mesh; } case box2dregion: Box2dAttachment box2d = attachmentLoader.newBox2dAttachment(skin, name, path); if (box2d == null) return null; box2d.setPath(path); box2d.setX((map.has("x") ? map.get("x").getAsFloat() : 0) * scale); box2d.setY((map.has("y") ? map.get("y").getAsFloat() : 0) * scale); box2d.setScaleX(map.has("scaleX") ? map.get("scaleX").getAsFloat() : 1); box2d.setScaleY(map.has("scaleY") ? map.get("scaleY").getAsFloat() : 1); box2d.setRotation(map.has("rotation") ? map.get("rotation").getAsFloat() : 0); box2d.setWidth(map.get("width").getAsFloat() * scale); box2d.setHeight(map.get("height").getAsFloat() * scale); String color = map.has("color") ? map.get("color").getAsString() : null;//map.getString("color", null); if (color != null) box2d.getColor().set(Color.valueOf(color)); box2d.updateOffset(); return box2d; default: break; } // RegionSequenceAttachment regionSequenceAttachment = (RegionSequenceAttachment)attachment; // // float fps = map.getFloat("fps"); // regionSequenceAttachment.setFrameTime(fps); // // String modeString = map.getString("mode"); // regionSequenceAttachment.setMode(modeString == null ? Mode.forward : Mode.valueOf(modeString)); return null; }
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 w w w . j av a2 s. co m } 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;//from w ww . j a va2 s . c o m 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.etermax.conversations.repository.impl.elasticsearch.mapper.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from www.j a v a 2 s. c o m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.exorath.buycraftconsumer.commandHandlers.ActionAPIHandler.java
License:Apache License
@Override public boolean handle(JsonObject command, DuePlayer duePlayer, DueCommand dueCommand) { try {/*from w ww. ja va 2 s.c o m*/ String type = command.get("type").getAsString(); String subject = command.has("subject") ? command.get("subject").getAsString() : duePlayer.getUuid(); String destination = command.has("destination") ? command.get("destination").getAsString() : null; boolean spigot = command.has("spigot") ? command.get("spigot").getAsBoolean() : true; JsonObject meta = command.has("meta") ? command.get("meta").getAsJsonObject() : new JsonObject(); meta = GSON.fromJson(meta.toString().replaceAll("%NAME%", duePlayer.getName()).replaceAll("%UUID%", duePlayer.getUuid()), JsonObject.class); if (subject.equals("uuid")) subject = duePlayer.getUuid(); Action action = new Action(subject, destination, type, meta, spigot); Success success = actionAPIServiceAPI.publishAction(action); if (!success.getSuccess()) System.out.println(success.getError()); return success.getSuccess(); } catch (Exception e) { e.printStackTrace(); return false; } }