Samples JDK
Sample.java
1 package com.freemindcafe.serialization.sample3;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 
8 import junit.framework.Assert;
9 
10 import org.junit.Test;
11 
13 
14 public class Sample {
15 
16  @Test
17  public void map_to_json(){
18  Map<String, String> workerUri = new HashMap<>();
19  workerUri.put("hostname", "localhost");
20  workerUri.put("port", "9449");
21  workerUri.put("mbean", "com.freemindcafe");
22 
23  String jsonUsingJackson = JSONParserUtil.objectToJson(workerUri, Map.class);
24  System.out.println(jsonUsingJackson);
25 
26  String jsonUsingJettison = new SerializationSvc().toJSON(workerUri);
27  System.out.println(jsonUsingJettison);
28 
29  Map<String, String> deserializedWorkerUri = (Map)JSONParserUtil.jsonToObject(jsonUsingJackson, Map.class);
30  Assert.assertEquals(workerUri, deserializedWorkerUri);
31  }
32 
33  @Test
34  public void list_to_json(){
35  Map<String, String> workerUri1 = new HashMap<>();
36  workerUri1.put("hostname", "localhost");
37  workerUri1.put("port", "9449");
38  workerUri1.put("mbean", "com.freemindcafe");
39 
40  Map<String, String> workerUri2 = new HashMap<>();
41  workerUri2.put("hostname", "localhost");
42  workerUri2.put("port", "9449");
43  workerUri2.put("mbean", "com.freemindcafe");
44 
45  List<Map<String,String>> uris = new ArrayList<>();
46  uris.add(workerUri1);
47  uris.add(workerUri2);
48 
49 
50  String jsonUsingJackson = JSONParserUtil.objectToJson(uris, List.class);
51  System.out.println(jsonUsingJackson);
52 
53  String jsonUsingJettison = new SerializationSvc().toJSON(uris);
54  System.out.println(jsonUsingJettison);
55 
56  }
57 
58 }
static< T > String objectToJson(final Object t, final Class< T > clazz)
static< TextendsObject > T jsonToObject(final String json, final Class< T > clazz)
Xstream and Jettison based serialization Jackson is arguably the standard Java Library for processing...