Search in sources :

Example 36 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gaiasky by langurmonkey.

the class JsonLoader method to3DoubleArray.

public Pair<Object, Class> to3DoubleArray(JsonValue attribute) {
    JsonValue json = attribute.child;
    int size = attribute.size;
    double[][][] result = new double[size][][];
    int i = 0;
    do {
        double[][] l1 = new double[json.size][];
        // Fill in last level
        JsonValue child = json.child;
        int j = 0;
        do {
            double[] l2 = child.asDoubleArray();
            l1[j] = l2;
            child = child.next();
            j++;
        } while (child != null);
        result[i] = l1;
        json = json.next();
        i++;
    } while (json != null);
    return new Pair<>(result, double[][][].class);
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 37 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gaiasky by langurmonkey.

the class JsonLoader method convertJsonToMap.

public Map<String, Object> convertJsonToMap(JsonValue json) {
    Map<String, Object> map = new TreeMap<>();
    JsonValue child = json.child;
    while (child != null) {
        Object val = getValue(child);
        if (val != null) {
            map.put(child.name, val);
        }
        child = child.next;
    }
    return map;
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) TreeMap(java.util.TreeMap)

Example 38 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gaiasky by langurmonkey.

the class JsonLoader method to2DoubleArray.

public Pair<Object, Class> to2DoubleArray(JsonValue attribute) {
    JsonValue json = attribute.child;
    int size = attribute.size;
    double[][] result = new double[size][];
    int i = 0;
    do {
        result[i] = json.asDoubleArray();
        json = json.next();
        i++;
    } while (json != null);
    return new Pair<>(result, double[][].class);
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue)

Example 39 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gaiasky by langurmonkey.

the class JsonLoader method loadData.

@Override
public Array<? extends SceneGraphNode> loadData() {
    Array<T> bodies = new Array<>();
    Array<String> filePaths = new Array<>(this.filePaths);
    // Actually load the files.
    JsonReader json = new JsonReader();
    for (String filePath : filePaths) {
        try {
            FileHandle file = Settings.settings.data.dataFileHandle(filePath);
            JsonValue model = json.parse(file.read());
            // Must have an 'objects' element.
            if (model.has("objects")) {
                JsonValue child = model.get("objects").child;
                final int count = model.get("objects").size;
                int current = 0;
                while (child != null) {
                    current++;
                    String clazzName = child.getString("impl").replace("gaia.cu9.ari.gaiaorbit", "gaiasky");
                    @SuppressWarnings("unchecked") Class<Object> clazz = (Class<Object>) ClassReflection.forName(clazzName);
                    // Convert to object and add to list.
                    @SuppressWarnings("unchecked") T object = (T) convertJsonToObject(child, clazz);
                    bodies.add(object);
                    child = child.next;
                    EventManager.publish(Event.UPDATE_LOAD_PROGRESS, this, file.name(), (float) current / (float) count);
                }
                EventManager.publish(Event.UPDATE_LOAD_PROGRESS, this, file.name(), 2f);
                logger.info(I18n.txt("notif.nodeloader", current, filePath));
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }
    return bodies;
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) JsonValue(com.badlogic.gdx.utils.JsonValue) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Array(com.badlogic.gdx.utils.Array) JsonReader(com.badlogic.gdx.utils.JsonReader)

Example 40 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gaiasky by langurmonkey.

the class ArchiveViewWindow method isToArray.

private String[][] isToArray(InputStream is, String format) {
    String data = slurp(is, 2046);
    if (format.equalsIgnoreCase("csv")) {
        /**
         * PARSE CSV *
         */
        String[] rows = data.split(separator);
        if (rows.length <= 1) {
            return null;
        }
        String[][] matrix = new String[rows.length][];
        int r = 0;
        for (String row : rows) {
            String[] tokens = row.split(",");
            int i = 0;
            for (String token : tokens) {
                token = token.trim();
                tokens[i] = trim(token, "\"");
                i++;
            }
            matrix[r] = tokens;
            r++;
        }
        return matrix;
    } else if (format.equalsIgnoreCase("json")) {
        /**
         * PARSE JSON *
         */
        JsonReader json = new JsonReader();
        JsonValue root = json.parse(data);
        JsonValue metadata = root.child;
        int size = metadata.size;
        JsonValue column = metadata.child;
        String[] colnames = new String[size];
        String[] descriptions = new String[size];
        String[] values = new String[size];
        int i = 0;
        do {
            colnames[i] = column.getString("name");
            descriptions[i] = column.getString("description") + (column.has("unit") ? " [" + column.getString("unit") + "]" : "");
            i++;
            column = column.next;
        } while (column != null);
        JsonValue datacol = metadata.next.child.child;
        i = 0;
        do {
            values[i] = datacol.asString();
            i++;
            datacol = datacol.next;
        } while (datacol != null);
        String[][] matrix = new String[3][];
        matrix[0] = colnames;
        matrix[1] = values;
        matrix[2] = descriptions;
        return matrix;
    }
    return null;
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) JsonReader(com.badlogic.gdx.utils.JsonReader)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)148 JsonReader (com.badlogic.gdx.utils.JsonReader)27 IOException (java.io.IOException)21 Array (com.badlogic.gdx.utils.Array)20 Json (com.badlogic.gdx.utils.Json)15 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)14 FileHandle (com.badlogic.gdx.files.FileHandle)11 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)10 BladeJson (com.bladecoder.engine.serialization.BladeJson)9 HashMap (java.util.HashMap)8 Color (com.badlogic.gdx.graphics.Color)7 GraphBoxImpl (com.gempukku.libgdx.graph.ui.graph.GraphBoxImpl)7 ArrayList (java.util.ArrayList)7 Vector2 (com.badlogic.gdx.math.Vector2)6 Actor (com.badlogic.gdx.scenes.scene2d.Actor)6 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)6 Action (com.bladecoder.engine.actions.Action)6 File (java.io.File)6 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 SerializationException (com.badlogic.gdx.utils.SerializationException)5