Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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 823 times)
0 Members and 1 Guest are viewing this topic.
Offline ReBirth
« Posted 2013-01-04 09: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 10: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: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2013-01-04 11: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.

Riven (11 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!