0

I've been playing around with the org.json library. I was trying to append a List of Enum types to a json object, but the resulting json array is empty. Here is what I mean:

attribEffects is supposed to hold "HEALTH" and "AMMO" respectively.

{"attr": [
    {
        "amount": [1000],
        "mult": [0],
        "attribId": [0],
        "time": [-1],
        "attribDesc": [null],
        "attribName": ["Health Buff (1000)(Debug)"],
        "attribEffects": [{}]
    },
    {
        "amount": [5],
        "mult": [0],
        "attribId": [1],
        "time": [5],
        "attribDesc": [null],
        "attribName": ["devgun_ammo"],
        "attribEffects": [{}]
    }
]}

Here is where I write to it:

for(Attribute a : (List<Attribute>) l) {
                JSONObject j = new JSONObject();
                j.append("attribName", a.getAttribName());
                j.append("attribId", a.getAttribId());
                j.append("attribDesc", a.getAttribDesc());

                j.put("attribEffects", a.getEffects());

                for(Object o : a.getEffects()) 
                    System.out.println(o.toString());

//              j.append("attribEffects", a.getAffects()); //TODO: Debug me
                j.append("amount", a.getAmount());
                j.append("mult", a.getMult());
                j.append("time", a.getTime());

                root.append("attr", j);
}

The output of the System.out.println for verification:

HEALTH
AMMO

Any ideas?

Edit: I realize having an array for a single value is kind of pointless, but I want this to be expandable in case of having a single thing with multiple effects

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Wep0n
  • 392
  • 3
  • 15
  • Possible duplicate of [Java : Convert Object consisting enum to Json Object](https://stackoverflow.com/questions/30871013/java-convert-object-consisting-enum-to-json-object) – Arnaud Jul 28 '17 at 10:00
  • 1
    It's not quite a duplicate but it's the same problem, thanks, that actually helped me out. – Wep0n Jul 28 '17 at 10:01
  • It seems JSONObject doesn't support enums –  Jul 28 '17 at 10:01
  • From what I gather it doesn't support enums without any data in them, yes. 2 ways to fix that would be getting the enum name and saving it as a string, or giving the enum data. I'll probably go with the enum name, since that's all it's used for in this context anyway. – Wep0n Jul 28 '17 at 10:02

1 Answers1

0

Since the json.org library is old and unsupported, it'd probably be wise to switch away from it. However, as a quick workaround since this isn't a serious project, this did the trick for now:

for(Attribute a : (List<Attribute>) l) {
                JSONObject j = new JSONObject();
                j.append("attribName", a.getAttribName());
                j.append("attribId", a.getAttribId());
                j.append("attribDesc", a.getAttribDesc());

                JSONArray jar = new JSONArray();

                for(Object o : a.getAffects()) 
                    jar.put(o.toString()); //TODO Optimize and Time me

                j.put("attribEffects", jar);

                j.append("amount", a.getAmount());
                j.append("mult", a.getMult());
                j.append("time", a.getTime());

                root.append("attr", j);
            }

(Notice: assembling a JSONArray, filling it and appending that instead of using the JSONObject's built-in function for collections)

Wep0n
  • 392
  • 3
  • 15