Friday, May 10, 2024
HomeGame DevelopmentCan't deserialize parameters from JSON in LibGDX

Can’t deserialize parameters from JSON in LibGDX


I am quite new to JSON and I have been having trouble with this data-driven asset manager (not my code). I have been trying to deserialize parameters for my TmxMapLoader object from JSON. It takes the parameters of TmxMapLoader.Parameters type.

Here is the Asset.class file that’s responsible for deserializing JSON:

public class Asset implements Json.Serializable  {
public Class<?> type;
public String path;
public AssetLoaderParameters parameters;

@Override
public void write(Json json) {
    json.writeValue("assetType", type.getName());
    json.writeValue("path", path);
    json.writeValue("parameters", parameters);
}

@Override
public void read(Json json, JsonValue jsonData) {
    try {
        type = ClassReflection.forName(jsonData.get("type").asString());
    } catch (Exception e) {
        type = null;
    }

    path = jsonData.get("path").asString();

    JsonValue parametersValue = jsonData.get("parameters");
    parameters = parametersValue != null ? json.fromJson(AssetLoaderParameters.class, parametersValue.toString()) : null;
}
}

And here is my JSON file:

{
"base" : [
{   
    "type" : "com.badlogic.gdx.graphics.Texture",
    "path" : "data/background.jpg"
},

{
    "type" : "com.badlogic.gdx.graphics.Texture",
    "path" : "data/player.png"
},

{
    "type" : "com.badlogic.gdx.maps.tiled.TiledMap",
    "path" : "data/map.tmx",
    "parameters": [{
        "flipY": true
    }]
}
]}

Everything works fine and loads properly if I don’t add the “parameters” element. Whenever I add the “parameters” element into the JSON file, the map fails to load. Am I adding the parameters wrong or missing something? Please help!

EDIT: Whenever I add the “parameters” element in .json file, the assets are being loaded. Here is my .json loading method:

private void loadGroups(String assetFile) {
    groups = new ObjectMap<String, Array<Asset>>();


    Gdx.app.log(TAG, "loading file " + assetFile);

    try {
        Json json = new Json();
        JsonReader reader = new JsonReader();
        JsonValue root = reader.parse(Gdx.files.internal(assetFile));

        JsonIterator groupIt = root.iterator();

        while (groupIt.hasNext()) {
            JsonValue groupValue = groupIt.next();

            if (groups.containsKey(groupValue.name)) {
                Gdx.app.log(TAG, "group " + groupValue.name + " already exists, skipping");
                continue;
            }

            Gdx.app.log(TAG, "registering group " + groupValue.name);

            Array<Asset> assets = new Array<Asset>();

            JsonIterator assetIt = groupValue.iterator();

            while (assetIt.hasNext()) {
                JsonValue assetValue = assetIt.next();

                Asset asset = json.fromJson(Asset.class, assetValue.toString());
                assets.add(asset);
            }

            groups.put(groupValue.name, assets);
        }
    }
    catch (Exception e) {
        Gdx.app.log(TAG, "error loading file " + assetFile + " " + e.getMessage());
    }
}

And the console output is:

Assets: loading file data/assets.json
Assets: registering group base
Assets: error loading file data/assets.json Error parsing JSON on line 2     near: parameters: {
*ERROR*flipY: false

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments