BrightSide Workbench Full Report + Source Code
JSONConverter.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
20 import java.lang.reflect.Array;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 
29 public class JSONConverter {
30  private StringBuffer result;
31 
35  public JSONConverter() {
36  }
37 
38  public String toJSON(Object javaObject) {
39  result = new StringBuffer();
40  processObject(javaObject);
41  return result.toString();
42  }
43 
44  private void processObject(Object javaObject) {
45  if(javaObject instanceof Array) {
46  processArray(javaObject);
47  }
48  else if(javaObject instanceof Map) {
49  processMap(javaObject);
50  }
51  else if(javaObject instanceof List) {
52  processList(javaObject);
53  }
54  else if(javaObject instanceof String) {
55  processString(javaObject);
56  }
57  else if(javaObject instanceof Number) {
58  processNumber(javaObject);
59  }
60  else if(javaObject instanceof Character) {
61  processCharacter(javaObject);
62  }
63  }
64 
65  private void processArray(Object javaObject) {
66  Array array = (Array)javaObject;
67  result.append("[");
68  boolean first = true;
69  for(int i = 0; i < Array.getLength(array); i++) {
70  if(!first) result.append(",");
71  processObject(Array.get(array, i));
72  first = false;
73  }
74  result.append("]");
75  }
76 
77  private void processMap(Object javaObject) {
78  Map map = (Map)javaObject;
79  Iterator it = map.keySet().iterator();
80  result.append("[");
81  Object obj;
82  boolean first = true;
83  while(it.hasNext()) {
84  if(!first) result.append(",");
85  obj = it.next();
86  result.append("[");
87  processObject(obj);
88  result.append(",");
89  processObject(map.get(obj));
90  result.append("]");
91  first = false;
92  }
93  result.append("]");
94  }
95 
96  private void processList(Object javaObject) {
97  List list = (List)javaObject;
98  Iterator it = list.iterator();
99  result.append("[");
100  boolean first = true;
101  while(it.hasNext()) {
102  if(!first) result.append(",");
103  processObject(it.next());
104  first = false;
105  }
106  result.append("]");
107  }
108 
109  private void processString(Object javaObject) {
110  result.append("\"" + (String)javaObject + "\"");
111  }
112 
113  private void processNumber(Object javaObject) {
114  result.append("\"" + ((Number)javaObject).toString() + "\"");
115  }
116 
117  private void processCharacter(Object javaObject) {
118  result.append("'" + ((Character)javaObject).toString() + "'");
119  }
120 
121 }