01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package info.jtrac.wicket.yui;
18:
19: import java.util.Map;
20:
21: /**
22: * utilities for doing some javascript stuff e.g. basic JSON serialization
23: */
24: public class YuiUtils {
25:
26: /**
27: * custom Map to JSON converter
28: * TODO support values that should not be treated like strings (quoted)
29: */
30: public static String getJson(Map<String, Object> map) {
31: StringBuilder sb = new StringBuilder();
32: sb.append("{");
33: boolean firstTime = true;
34: for (Map.Entry entry : map.entrySet()) {
35: if (firstTime) {
36: firstTime = false;
37: } else {
38: sb.append(", ");
39: }
40: sb.append(entry.getKey());
41: sb.append(" : ");
42: Object value = entry.getValue();
43: if (value instanceof Map) {
44: sb.append(getJson((Map) value));
45: } else if (entry.getValue() instanceof String) {
46: sb.append("'" + value + "'");
47: } else {
48: sb.append(value);
49: }
50: }
51: sb.append("}");
52: return sb.toString();
53: }
54:
55: }
|