1
\$\begingroup\$

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

}

\$\endgroup\$
2
  • 1
    \$\begingroup\$ What do you mean by "fails to load"? What errors are thrown / what exactly goes wrong? Have you tried stepping through the program to see where it goes wrong etc.? \$\endgroup\$
    – Charanor
    Aug 17, 2017 at 17:20
  • \$\begingroup\$ The console output is: Asset not loaded: data/map.tmx. After stepping through the program I found out that it fails at parametersValue.toString() method. \$\endgroup\$ Aug 17, 2017 at 17:22

1 Answer 1

0
\$\begingroup\$

You have to access the data inside of the { } brackets of your array before you can do anything with it. To do this simply make this simple change:

JsonValue parametersValue = jsonData.get("parameters").child;

The .child will access the first child of the JsonValue (if any). In this case the child is the { } brackets.

Note that you also will probably have to change json.fromJson(AssetLoaderParameters.class, ...) to json.fromJson(TmxMapLoader.Parameters.class, ...) since otherwise it will throw the exception:

Field not found: flipY (com.badlogic.gdx.assets.AssetLoaderParameters)

\$\endgroup\$
1
  • \$\begingroup\$ Adding .child makes the program crash with the same error as before. I have experimented a bit, loading parametersValue = jsonData.get("parameters") and printing it off works, but if I try to do anything with its child elements it crashes instantly. Could it be that by .json structure within "parameters" element is wrong? \$\endgroup\$ Aug 17, 2017 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .