forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONUtil.java.html
More file actions
114 lines (98 loc) · 3 KB
/
Copy pathJSONUtil.java.html
File metadata and controls
114 lines (98 loc) · 3 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999.html">
<head>
<title>JSONUtil.java</title>
<link rel="stylesheet" type="text/css" href="../../common.css"/>
</head>
<body>
<h2>JSONUtil.java</h2>
<pre><div class="code">package com.ociweb.json;
import java.util.*;
import net.sf.json.*;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaClass;
import org.apache.commons.beanutils.DynaProperty;
import static com.ociweb.lang.SystemUtil.*;
public class JSONUtil {
private Map<String, Class> classMap = new HashMap<String, Class>();
/**
* Adds a mapping that is used when converting from a JSON map
* to a Java Bean. Values of map entries with the given key
* will be converted to Java Beans with the given Class.
*/
public void addMapping(String key, Class clazz) {
classMap.put(key, clazz);
}
/**
* Converts a JSON string representing an array
* to an array of Java Beans of a given Class.
*/
public Object[] fromJSONArray(String json, Class clazz) {
JSONArray ja = JSONArray.fromString(json);
return JSONArray.toArray(ja, clazz, classMap);
}
/**
* Converts a JSON string representing an array
* to a java.util.List that contains Java Beans of a given Class.
*/
public List fromJSONList(String json, Class clazz) {
JSONArray ja = JSONArray.fromString(json);
return JSONArray.toList(ja, clazz, classMap);
}
/**
* Converts a JSON string representing an object
* to a Java Bean object of a given Class.
*/
public Object fromJSONObject(String json, Class clazz) {
JSONObject jo = JSONObject.fromString(json);
return JSONObject.toBean(jo, clazz, classMap);
}
/**
* Pretty prints a JSONArray or JSONObject.
*/
private void prettyPrint(JSON j) {
out(j.toString(2));
}
/**
* Pretty prints a JSON string that represents an array.
*/
public void prettyPrintArray(String json) {
prettyPrint(JSONArray.fromString(json));
}
/**
* Pretty prints a JSON string that represents an object.
*/
public void prettyPrintObject(String json) {
prettyPrint(JSONObject.fromString(json));
}
/**
* Converts a java.util.List to a JSON string.
*/
public String toJSON(List list) {
JSONArray ja = JSONArray.fromCollection(list);
return ja.toString();
}
/**
* Converts a Java Bean to a JSON string.
*/
public String toJSON(Object object) {
JSONObject jo = JSONObject.fromObject(object);
return jo.toString();
}
/**
* Converts an array of booleans, numbers, strings and Java Beans
* to a JSON string.
*/
public String toJSON(Object[] array) {
JSONArray ja = JSONArray.fromArray(array);
return ja.toString();
}
}</div></pre>
<hr/>
<p style="text-align:center">
Copyright © 2007 Object Computing, Inc. All rights reserved.
</p>
</body>
</html>