0
\$\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\$
  • \$\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 at 11:40
  • \$\begingroup\$ yes But I really don't know how to properly do that \$\endgroup\$ – Macmanmatty Mar 18 at 13:41
  • 1
    \$\begingroup\$ Then show us what you tried, and we can help you with what's next. \$\endgroup\$ – DMGregory Mar 18 at 13:56
  • \$\begingroup\$ I thought libgx classes were automatically serializable via json. \$\endgroup\$ – Macmanmatty Mar 18 at 15:35
  • \$\begingroup\$ had someone have told me libgdx json can't de-serialize Enums I would have solved. this days ago \$\endgroup\$ – Macmanmatty Mar 21 at 18:29
1
\$\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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.