Example usage for com.badlogic.gdx.utils JsonValue isArray

List of usage examples for com.badlogic.gdx.utils JsonValue isArray

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils JsonValue isArray.

Prototype

public boolean isArray() 

Source Link

Usage

From source file:com.company.minery.utils.spine.SkeletonJson.java

License:Open Source License

void readCurve(CurveTimeline timeline, int frameIndex, JsonValue valueMap) {
    JsonValue curve = valueMap.get("curve");
    if (curve == null)
        return;/*from   w w w.  j  av a2 s  .  co m*/
    if (curve.isString() && curve.asString().equals("stepped"))
        timeline.setStepped(frameIndex);
    else if (curve.isArray()) {
        timeline.setCurve(frameIndex, curve.getFloat(0), curve.getFloat(1), curve.getFloat(2),
                curve.getFloat(3));
    }
}

From source file:com.jupiter.europa.entity.component.EffectsComponent.java

License:Open Source License

@Override
public void read(Json json, JsonValue jsonData) {
    if (jsonData.has(EFFECTS_KEY)) {
        JsonValue selectedData = jsonData.get(EFFECTS_KEY);
        if (selectedData.isArray()) {
            selectedData.iterator().forEach((JsonValue value) -> {
                if (value.has(EFFECT_CLASS_KEY) && value.has(EFFECT_DATA_KEY)) {
                    String typeName = value.getString(EFFECT_CLASS_KEY);
                    try {
                        Class<?> type = Class.forName(typeName);
                        if (Effect.class.isAssignableFrom(type)) {
                            this.effects
                                    .add((Effect) json.fromJson(type, value.get(EFFECT_DATA_KEY).toString()));
                        }/*from w ww. j  av  a 2s. c o  m*/
                    } catch (ClassNotFoundException ex) {

                    }

                }
            });
        }
    }
}

From source file:com.jupiter.europa.entity.trait.TraitPool.java

License:Open Source License

@Override
public void read(Json json, JsonValue jsonData) {
    if (jsonData.has(CAPACITY_KEY)) {
        this.capacity = jsonData.getInt(CAPACITY_KEY);
    }//from  w ww.jav a 2 s  .  c  o  m

    if (jsonData.has(SELECTED_KEY)) {
        JsonValue selectedData = jsonData.get(SELECTED_KEY);
        if (selectedData.isArray()) {
            selectedData.iterator().forEach((JsonValue value) -> {
                if (value.has(ITEM_CLASS_KEY) && value.has(ITEM_DATA_KEY)) {
                    String typeName = value.getString(ITEM_CLASS_KEY);
                    try {
                        Class<?> type = Class.forName(typeName);
                        if (Trait.class.isAssignableFrom(type)) {
                            this.selected.add((T) json.fromJson(type, value.get(ITEM_DATA_KEY).toString()));
                        }
                    } catch (ClassNotFoundException ex) {

                    }

                }
            });
        }
    }
}

From source file:com.mbrlabs.mundus.commons.g3d.MG3dModelLoader.java

License:Apache License

private void parseAnimations(ModelData model, JsonValue json) {
    JsonValue animations = json.get("animations");
    if (animations == null)
        return;/*from   ww w  . j a v  a  2 s.c  om*/

    model.animations.ensureCapacity(animations.size);

    for (JsonValue anim = animations.child; anim != null; anim = anim.next) {
        JsonValue nodes = anim.get("bones");
        if (nodes == null)
            continue;
        ModelAnimation animation = new ModelAnimation();
        model.animations.add(animation);
        animation.nodeAnimations.ensureCapacity(nodes.size);
        animation.id = anim.getString("id");
        for (JsonValue node = nodes.child; node != null; node = node.next) {
            ModelNodeAnimation nodeAnim = new ModelNodeAnimation();
            animation.nodeAnimations.add(nodeAnim);
            nodeAnim.nodeId = node.getString("boneId");

            // For backwards compatibility (version 0.1):
            JsonValue keyframes = node.get("keyframes");
            if (keyframes != null && keyframes.isArray()) {
                for (JsonValue keyframe = keyframes.child; keyframe != null; keyframe = keyframe.next) {
                    final float keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                    JsonValue translation = keyframe.get("translation");
                    if (translation != null && translation.size == 3) {
                        if (nodeAnim.translation == null)
                            nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> tkf = new ModelNodeKeyframe<Vector3>();
                        tkf.keytime = keytime;
                        tkf.value = new Vector3(translation.getFloat(0), translation.getFloat(1),
                                translation.getFloat(2));
                        nodeAnim.translation.add(tkf);
                    }
                    JsonValue rotation = keyframe.get("rotation");
                    if (rotation != null && rotation.size == 4) {
                        if (nodeAnim.rotation == null)
                            nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                        ModelNodeKeyframe<Quaternion> rkf = new ModelNodeKeyframe<Quaternion>();
                        rkf.keytime = keytime;
                        rkf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1),
                                rotation.getFloat(2), rotation.getFloat(3));
                        nodeAnim.rotation.add(rkf);
                    }
                    JsonValue scale = keyframe.get("scale");
                    if (scale != null && scale.size == 3) {
                        if (nodeAnim.scaling == null)
                            nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> skf = new ModelNodeKeyframe();
                        skf.keytime = keytime;
                        skf.value = new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
                        nodeAnim.scaling.add(skf);
                    }
                }
            } else { // Version 0.2:
                JsonValue translationKF = node.get("translation");
                if (translationKF != null && translationKF.isArray()) {
                    nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.translation.ensureCapacity(translationKF.size);
                    for (JsonValue keyframe = translationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.translation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue translation = keyframe.get("value");
                        if (translation != null && translation.size >= 3)
                            kf.value = new Vector3(translation.getFloat(0), translation.getFloat(1),
                                    translation.getFloat(2));
                    }
                }

                JsonValue rotationKF = node.get("rotation");
                if (rotationKF != null && rotationKF.isArray()) {
                    nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                    nodeAnim.rotation.ensureCapacity(rotationKF.size);
                    for (JsonValue keyframe = rotationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Quaternion> kf = new ModelNodeKeyframe<Quaternion>();
                        nodeAnim.rotation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue rotation = keyframe.get("value");
                        if (rotation != null && rotation.size >= 4)
                            kf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1),
                                    rotation.getFloat(2), rotation.getFloat(3));
                    }
                }

                JsonValue scalingKF = node.get("scaling");
                if (scalingKF != null && scalingKF.isArray()) {
                    nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.scaling.ensureCapacity(scalingKF.size);
                    for (JsonValue keyframe = scalingKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.scaling.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue scaling = keyframe.get("value");
                        if (scaling != null && scaling.size >= 3)
                            kf.value = new Vector3(scaling.getFloat(0), scaling.getFloat(1),
                                    scaling.getFloat(2));
                    }
                }
            }
        }
    }
}

From source file:de.tomgrill.gdxfacebook.core.GDXFacebookBasic.java

License:Apache License

protected void startSilentSignIn() {
    if (accessToken != null) {
        Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Starting silent sign in.");
        GDXFacebookGraphRequest request = new GDXFacebookGraphRequest();
        request.setMethod(Net.HttpMethods.POST);
        request.setNode("");
        request.putField("batch",
                "[{\"method\":\"GET\", \"relative_url\":\"me\"},{\"method\":\"GET\", \"relative_url\":\"me/permissions\"}]");
        request.putField("include_headers", "false");
        request.useCurrentAccessToken();
        newGraphRequest(request, new GDXFacebookCallback<JsonResult>() {

            @Override//  w  w  w .j  ava2  s .co m
            public void onSuccess(JsonResult result) {
                JsonValue value = result.getJsonValue();
                if (value != null && value.isArray()) {

                    JsonValue meValue = value.get(0);
                    JsonValue permissionsValue = value.get(1);

                    if (jsonHasCode200AndBody(meValue) && jsonHasCode200AndBody(permissionsValue)) {

                        JsonReader reader = new JsonReader();
                        JsonValue permissionBodyValue = reader.parse(permissionsValue.getString("body"));
                        JsonValue permissionArray = permissionBodyValue.get("data");

                        Array<String> grantedPermissions = new Array<String>();
                        for (int i = 0; i < permissionArray.size; i++) {
                            JsonValue permission = permissionArray.get(i);

                            if (permission.getString("status").equals("granted")) {
                                grantedPermissions.add(permission.getString("permission").toLowerCase());
                            }
                        }

                        if (arePermissionsGranted(grantedPermissions)) {
                            Gdx.app.debug(GDXFacebookVars.LOG_TAG,
                                    "Silent sign in successful. Current token is still valid.");
                            callback.onSuccess(new SignInResult(accessToken,
                                    "Silent sign in successful. Current token is still valid."));
                        } else {
                            signOut();
                            Gdx.app.debug(GDXFacebookVars.LOG_TAG,
                                    "Used access_token is valid but new permissions need to be granted. Need GUI sign in.");
                            callback.onError(new GDXFacebookError(
                                    "Used access_token is valid but new permissions need to be granted. Need GUI sign in."));
                            startGUISignIn();
                        }

                    } else {
                        signOut();
                        Gdx.app.debug(GDXFacebookVars.LOG_TAG,
                                "Silent sign in parse error: " + value.toString());
                        callback.onError(new GDXFacebookError(value.toString()));
                        startGUISignIn();
                    }
                } else {
                    callback.onError(new GDXFacebookError(
                            "Unexpected error occurred while trying to sign in. Error unknown, possible timeout."));
                }
            }

            private boolean arePermissionsGranted(Array<String> grantedPermissions) {
                for (int i = 0; i < permissions.size; i++) {
                    if (!grantedPermissions.contains(permissions.get(i).toLowerCase(), false)) {
                        return false;
                    }
                }
                return true;
            }

            @Override
            public void onError(GDXFacebookError error) {
                signOut();
                Gdx.app.debug(GDXFacebookVars.LOG_TAG,
                        "Silent sign in request error: " + error.getErrorMessage());
                callback.onError(error);
                startGUISignIn();
            }

            @Override
            public void onFail(Throwable t) {
                signOut();
                Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in failed: " + t);
                callback.onFail(t);
                startGUISignIn();
            }

            @Override
            public void onCancel() {
                signOut();
                Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in fail");
                callback.onCancel();
                startGUISignIn();
            }
        });
    } else {
        Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in fail. No existing access token.");
    }
}

From source file:de.tomgrill.gdxfacebook.html.HTMLGDXFacebook.java

License:Apache License

private void validatePermissions(final Array<String> permissions,
        final GDXFacebookCallback<SignInResult> callback) {
    Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Checking for permissions");

    GDXFacebookGraphRequest request = new GDXFacebookGraphRequest();
    request.setMethod(Net.HttpMethods.GET);
    request.setNode("me/permissions");
    request.useCurrentAccessToken();//w w w  .j  a va 2  s . c om

    graph(request, new GDXFacebookCallback<JsonResult>() {
        @Override
        public void onSuccess(JsonResult result) {

            JsonValue value = result.getJsonValue();
            if (value != null && value.has("data")) {
                JsonValue dataValue = value.get("data");
                if (dataValue != null && dataValue.isArray()) {

                    grantedPermissions.clear();
                    for (int i = 0; i < dataValue.size; i++) {
                        JsonValue permission = dataValue.get(i);

                        if (permission.getString("status").equals("granted")) {
                            grantedPermissions.add(permission.getString("permission").toLowerCase());
                        }
                    }

                    if (areSamePermissionsOrMore(permissions, grantedPermissions)) {
                        Gdx.app.debug(GDXFacebookVars.LOG_TAG, "AccessToken and permissions are valid.");
                        callback.onSuccess(
                                new SignInResult(accessToken, "AccessToken and permissions are valid."));
                        return;
                    }
                }
            }
            guiLogin(permissions, callback);
        }

        @Override
        public void onError(GDXFacebookError error) {
            guiLogin(permissions, callback);
        }

        @Override
        public void onFail(Throwable t) {
            guiLogin(permissions, callback);
        }

        @Override
        public void onCancel() {
            guiLogin(permissions, callback);
        }
    });

}

From source file:es.eucm.ead.engine.tracking.gleaner.GleanerGameTracker.java

License:Open Source License

/**
 * Configure the tracker//w  w w. j av a  2 s .  c  om
 *
 * @param config the json configuration
 */
private void configure(JsonValue config) {
    press = config.getBoolean(PRESS, false);
    phases = config.getBoolean(PHASES, false);
    tracker.setMaxTracesPerQueue(config.getInt(MAX_TRACES, -1));
    JsonValue variables = config.get(VARIABLES);
    if (variables != null) {
        if (variables.isArray()) {
            JsonValue current = variables.child();
            while (current != null) {
                this.watchField(current.asString());
                current = current.next();
            }
        } else {
            logger.warn("Invalid configuration value for variables. It should be an array of strings.");
        }
    }
}

From source file:Facebook.GDXFacebookBasic.java

License:Apache License

protected void startSilentSignIn() {
    if (accessToken != null) {
        Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Starting silent sign in.");
        GDXFacebookGraphRequest request = new GDXFacebookGraphRequest();
        request.setMethod(Net.HttpMethods.POST);
        request.setNode("");
        request.putField("batch",
                "[{\"method\":\"GET\", \"relative_url\":\"me\"},{\"method\":\"GET\", \"relative_url\":\"me/permissions\"}]");
        request.putField("include_headers", "false");
        request.useCurrentAccessToken();
        newGraphRequest(request, new GDXFacebookCallback<JsonResult>() {

            @Override/*from  w  w w  .  j  av  a  2 s.c  o  m*/
            public void onSuccess(JsonResult result) {
                JsonValue value = result.getJsonValue();
                if (value != null && value.isArray()) {

                    JsonValue meValue = value.get(0);
                    JsonValue permissionsValue = value.get(1);

                    if (jsonHasCode200AndBody(meValue) && jsonHasCode200AndBody(permissionsValue)) {

                        JsonReader reader = new JsonReader();
                        JsonValue permissionBodyValue = reader.parse(permissionsValue.getString("body"));
                        JsonValue permissionArray = permissionBodyValue.get("data");

                        Array<String> grantedPermissions = new Array<String>();
                        for (int i = 0; i < permissionArray.size; i++) {
                            JsonValue permission = permissionArray.get(i);

                            if (permission.getString("status").equals("granted")) {
                                grantedPermissions.add(permission.getString("permission").toLowerCase());
                            }
                        }

                        if (arePermissionsGranted(grantedPermissions)) {
                            Gdx.app.debug(GDXFacebookVars.LOG_TAG,
                                    "Silent sign in successful. Current token is still valid.");
                            callback.onSuccess(new SignInResult(accessToken,
                                    "Silent sign in successful. Current token is still valid."));
                        } else {
                            signOut();
                            Gdx.app.debug(GDXFacebookVars.LOG_TAG,
                                    "Used access_token is valid but new permissions need to be granted. Need GUI sign in.");
                            callback.onError(new GDXFacebookError(
                                    "Used access_token is valid but new permissions need to be granted. Need GUI sign in."));
                            startGUISignIn();
                        }

                    } else {
                        signOut();
                        Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in error: " + value.toString());
                        callback.onError(new GDXFacebookError(value.toString()));
                        startGUISignIn();
                    }
                } else {
                    callback.onError(new GDXFacebookError(
                            "Unexpected error occurred while trying to sign in. Error unknown, possible timeout."));
                }
            }

            private boolean arePermissionsGranted(Array<String> grantedPermissions) {
                for (int i = 0; i < permissions.size; i++) {
                    if (!grantedPermissions.contains(permissions.get(i).toLowerCase(), false)) {
                        return false;
                    }
                }
                return true;
            }

            @Override
            public void onError(GDXFacebookError error) {
                signOut();
                Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in error: " + error.getErrorMessage());
                callback.onError(error);
                startGUISignIn();
            }

            @Override
            public void onFail(Throwable t) {
                signOut();
                Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in failed: " + t);
                callback.onFail(t);
                startGUISignIn();
            }

            @Override
            public void onCancel() {
                signOut();
                Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in fail");
                callback.onCancel();
                startGUISignIn();
            }
        });
    } else {
        Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in fail. No existing access token.");
    }
}

From source file:mt.Json.java

License:Apache License

/** @param type May be null if the type is unknown.
 * @param elementType May be null if the type is unknown.
 * @return May be null. *///from   w w w .j a v  a 2  s  . c om
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
    if (jsonData == null)
        return null;

    if (jsonData.isObject()) {
        String className = typeName == null ? null : jsonData.getString(typeName, null);
        if (className != null) {
            jsonData.remove(typeName);
            try {
                type = (Class<T>) ClassReflection.forName(className);
            } catch (ReflectionException ex) {
                type = tagToClass.get(className);
                if (type == null)
                    throw new SerializationException(ex);
            }
        }

        Object object;
        if (type != null) {
            if (type == String.class || type == Integer.class || type == Boolean.class || type == Float.class
                    || type == Long.class || type == Double.class || type == Short.class || type == Byte.class
                    || type == Character.class || type.isEnum()) {
                return readValue("value", type, jsonData);
            }

            Serializer serializer = classToSerializer.get(type);
            if (serializer != null)
                return (T) serializer.read(this, jsonData, type);

            object = newInstance(type);

            if (object instanceof Serializable) {
                ((Serializable) object).read(this, jsonData);
                return (T) object;
            }

            if (object instanceof HashMap) {
                HashMap result = (HashMap) object;
                for (JsonValue child = jsonData.child(); child != null; child = child.next())
                    result.put(child.name(), readValue(elementType, null, child));
                return (T) result;
            }
        } else if (defaultSerializer != null) {
            return (T) defaultSerializer.read(this, jsonData, type);
        } else
            return (T) jsonData;

        if (object instanceof ObjectMap) {
            ObjectMap result = (ObjectMap) object;
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                result.put(child.name(), readValue(elementType, null, child));
            return (T) result;
        }
        readFields(object, jsonData);
        return (T) object;
    }

    if (type != null) {
        Serializer serializer = classToSerializer.get(type);
        if (serializer != null)
            return (T) serializer.read(this, jsonData, type);
    }

    if (jsonData.isArray()) {
        if ((type == null || type == Object.class) || ClassReflection.isAssignableFrom(Array.class, type)) {
            Array newArray = (type == null || type == Object.class) ? new Array() : (Array) newInstance(type);
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                newArray.add(readValue(elementType, null, child));
            return (T) newArray;
        }
        if (ClassReflection.isAssignableFrom(List.class, type)) {
            List newArray = (type == null || type.isInterface()) ? new ArrayList() : (List) newInstance(type);
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                newArray.add(readValue(elementType, null, child));
            return (T) newArray;
        }
        if (type.isArray()) {
            Class componentType = type.getComponentType();
            if (elementType == null)
                elementType = componentType;
            Object newArray = ArrayReflection.newInstance(componentType, jsonData.size);
            int i = 0;
            for (JsonValue child = jsonData.child(); child != null; child = child.next())
                ArrayReflection.set(newArray, i++, readValue(elementType, null, child));
            return (T) newArray;
        }
        throw new SerializationException(
                "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")");
    }

    if (jsonData.isNumber()) {
        try {
            if (type == null || type == float.class || type == Float.class)
                return (T) (Float) jsonData.asFloat();
            if (type == int.class || type == Integer.class)
                return (T) (Integer) jsonData.asInt();
            if (type == long.class || type == Long.class)
                return (T) (Long) jsonData.asLong();
            if (type == double.class || type == Double.class)
                return (T) (Double) (double) jsonData.asFloat();
            if (type == String.class)
                return (T) Float.toString(jsonData.asFloat());
            if (type == short.class || type == Short.class)
                return (T) (Short) (short) jsonData.asInt();
            if (type == byte.class || type == Byte.class)
                return (T) (Byte) (byte) jsonData.asInt();
        } catch (NumberFormatException ignored) {
        }
        jsonData = new JsonValue(jsonData.asString());
    }

    if (jsonData.isBoolean()) {
        try {
            if (type == null || type == boolean.class || type == Boolean.class)
                return (T) (Boolean) jsonData.asBoolean();
        } catch (NumberFormatException ignored) {
        }
        jsonData = new JsonValue(jsonData.asString());
    }

    if (jsonData.isString()) {
        String string = jsonData.asString();
        if (type == null || type == String.class)
            return (T) string;
        try {
            if (type == int.class || type == Integer.class)
                return (T) Integer.valueOf(string);
            if (type == float.class || type == Float.class)
                return (T) Float.valueOf(string);
            if (type == long.class || type == Long.class)
                return (T) Long.valueOf(string);
            if (type == double.class || type == Double.class)
                return (T) Double.valueOf(string);
            if (type == short.class || type == Short.class)
                return (T) Short.valueOf(string);
            if (type == byte.class || type == Byte.class)
                return (T) Byte.valueOf(string);
        } catch (NumberFormatException ignored) {
        }
        if (type == boolean.class || type == Boolean.class)
            return (T) Boolean.valueOf(string);
        if (type == char.class || type == Character.class)
            return (T) (Character) string.charAt(0);
        if (ClassReflection.isAssignableFrom(Enum.class, type)) {
            Object[] constants = type.getEnumConstants();
            for (int i = 0, n = constants.length; i < n; i++)
                if (string.equals(constants[i].toString()))
                    return (T) constants[i];
        }
        if (type == CharSequence.class)
            return (T) string;
        throw new SerializationException(
                "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")");
    }

    return null;
}