Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
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 365 times)
0 Members and 1 Guest are viewing this topic.
Online 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 Wizard


Medals: 88
Projects: 3


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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
mrbenebob (4 views)
2013-06-19 14:55:23

BrassApparatus (12 views)
2013-06-19 08:52:37

NegativeZero (17 views)
2013-06-19 03:31:52

NegativeZero (19 views)
2013-06-19 03:24:09

Jesse_Attard (21 views)
2013-06-18 22:03:02

HeroesGraveDev (62 views)
2013-06-15 23:35:23

Vermeer (61 views)
2013-06-14 20:08:06

davedes (61 views)
2013-06-14 16:03:55

alaslipknot (56 views)
2013-06-13 07:56:31

Roquen (77 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
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!