Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to write ArrayMap to JSON?  (Read 938 times)
0 Members and 1 Guest are viewing this topic.
Offline ReBirth
« Posted 2013-01-04 08:15:25 »

This ArrayMap in question is libgdx's. I tried this,
1  
2  
3  
4  
5  
6  
7  
text: {
            class: map,
            "3": "Once upon a time, there are 3 bees",
            "4": "One of them was so ugly, so the other two died",
            "5": "Left alone, the ugly bee suicided",
            "6": "THE END"
         }

My goal is only to read Map with both String key-value pair. "text" is a ArrayMap<String, String> field and "map" is class tag for ArrayMap.class.

Offline meingrosserfreundjo

Senior Newbie





« Reply #1 - Posted 2013-01-04 09:18:15 »

I'm not sure that i fully understand what you are trying to accomplish but you could do
1  
2  
3  
4  
5  
6  
7  
8  
9  
text: {
  class: "map",
  entries: {
    "3": "Once upon a time, there are 3 bees",
    "4": "One of them was so ugly, so the other two died",
    "5": "Left alone, the ugly bee suicided",
    "6": "THE END"
  }
}

You could then read the Class Property as normal Text-Value (Maybe use the real Class name -> could use Reflection to instanciate) and the entries as Map-Object, where you can then iterate over the Content and fill your ArrayMap Object with the Keys and Values.
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2013-01-04 10:53:28 »

The Json class can automatically serialize to/from POJOs, OrderedMap, Array, String, Float, and Boolean. It doesn't know ArrayMap, so will treat it like a POJO, which isn't quite what you want. In this case you'll need to write a serializer that knows how to read/write ArrayMaps:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
static public void main (String[] args) throws Exception {
   Json json = new Json();
   json.setSerializer(ArrayMap.class, new Json.Serializer<ArrayMap>() {
      public void write (Json json, ArrayMap map, Class knownType) {
         Object[] keys = map.keys;
         Object[] values = map.values;
         json.writeObjectStart();
         for (int i = 0, n = map.size; i < n; i++)
            json.writeValue(keys[i].toString(), values[i]);
         json.writeObjectEnd();
      }

      public ArrayMap read (Json json, Object jsonData, Class type) {
         ArrayMap map = new ArrayMap();
         for (Entry entry : ((OrderedMap<?, ?>)jsonData).entries())
            map.put(entry.key, entry.value);
         return map;
      }
   });
   json.addClassTag("map", ArrayMap.class);
   ArrayMap map = new ArrayMap();
   map.put("3", "Once upon a time, there are 3 bees");
   map.put("4", "One of them was so ugly, so the other two died");
   map.put("5", "Left alone, the ugly bee suicided");
   map.put("6", "THE END");
   String text = json.toJson(map);
   System.out.println(json.prettyPrint(text));
   ArrayMap map2 = json.fromJson(ArrayMap.class, text);
   System.out.println(map2);
}


If your "text" fields was an OrderedMap instead of ArrayMap, you wouldn't have to do this (but of course these two map implementations are quite different).

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!