2
\$\begingroup\$

Any one have any ideas on how I can serialize / de-serialize an

ObjectMap<Enum, ObjectMap<String, Object>>(); 

(Object is usually a String Integer or Vector2 ) using libgdx's built in json classes?. On de-serialize the inner ObjectMap, is always null even though the values are written to the json file.

\$\endgroup\$
6
  • \$\begingroup\$ Usually I convert my map to a list of key-value pairs, and serialize that. Have you tried anything similar? \$\endgroup\$
    – DMGregory
    Mar 18, 2020 at 11:40
  • \$\begingroup\$ yes But I really don't know how to properly do that \$\endgroup\$ Mar 18, 2020 at 13:41
  • 1
    \$\begingroup\$ Then show us what you tried, and we can help you with what's next. \$\endgroup\$
    – DMGregory
    Mar 18, 2020 at 13:56
  • \$\begingroup\$ I thought libgx classes were automatically serializable via json. \$\endgroup\$ Mar 18, 2020 at 15:35
  • \$\begingroup\$ had someone have told me libgdx json can't de-serialize Enums I would have solved. this days ago \$\endgroup\$ Mar 21, 2020 at 18:29

1 Answer 1

2
\$\begingroup\$

libGDX Json converts Enums to strings and therefore cannot serialize / de-serialize Map keys to anything but strings.

To fix this, either...

  1. Use strings for keys in Maps (ObjectMap, HashMap ect.).

  2. Use GSON, which can serialize Enum Map Keys just fine.

  3. Write lots of special serialization / de-serialization code using a class that implements Json.Serializer<T> where T is your map and then register that with your libGDX Json instance.

I recommend option one or two.

\$\endgroup\$

You must log in to answer this question.

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