This project has retired. For details please refer to its Attic page.
JSONObject xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl.json;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * A JSON object. Key value pairs are unordered. JSONObject supports
28   * java.util.Map interface.
29   * 
30   * (Taken from JSON.simple <http://code.google.com/p/json-simple/> and modified
31   * for OpenCMIS.)
32   * 
33   * @author FangYidong<fangyidong@yahoo.com.cn>
34   */
35  public class JSONObject extends HashMap<String, Object> implements Map<String, Object>, JSONAware, JSONStreamAware {
36      private static final long serialVersionUID = -503443796854799292L;
37  
38      /**
39       * Encode a map into JSON text and write it to out. If this map is also a
40       * JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific
41       * behaviours will be ignored at this top level.
42       * 
43       * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
44       * 
45       * @param map
46       * @param out
47       */
48      public static void writeJSONString(Map<String, Object> map, Writer out) throws IOException {
49          if (map == null) {
50              out.write("null");
51              return;
52          }
53  
54          boolean first = true;
55  
56          out.write('{');
57          for (Map.Entry<String, Object> entry : map.entrySet()) {
58              if (first) {
59                  first = false;
60              } else {
61                  out.write(',');
62              }
63  
64              out.write('\"');
65              out.write(escape(entry.getKey()));
66              out.write('\"');
67              out.write(':');
68              JSONValue.writeJSONString(entry.getValue(), out);
69          }
70          out.write('}');
71      }
72  
73      public void writeJSONString(Writer out) throws IOException {
74          writeJSONString(this, out);
75      }
76  
77      /**
78       * Convert a map to JSON text. The result is a JSON object. If this map is
79       * also a JSONAware, JSONAware specific behaviours will be omitted at this
80       * top level.
81       * 
82       * @see org.json.simple.JSONValue#toJSONString(Object)
83       * 
84       * @param map
85       * @return JSON text, or "null" if map is null.
86       */
87      public static String toJSONString(Map<String, Object> map) {
88          if (map == null) {
89              return "null";
90          }
91  
92          StringBuilder sb = new StringBuilder();
93          boolean first = true;
94  
95          sb.append('{');
96          for (Map.Entry<String, Object> entry : map.entrySet()) {
97              if (first) {
98                  first = false;
99              } else {
100                 sb.append(',');
101             }
102 
103             toJSONString(entry.getKey(), entry.getValue(), sb);
104         }
105         sb.append('}');
106 
107         return sb.toString();
108     }
109 
110     public String toJSONString() {
111         return toJSONString(this);
112     }
113 
114     private static String toJSONString(String key, Object value, StringBuilder sb) {
115         sb.append('\"');
116         if (key == null) {
117             sb.append("null");
118         } else {
119             JSONValue.escape(key, sb);
120         }
121 
122         sb.append('\"').append(':');
123 
124         sb.append(JSONValue.toJSONString(value));
125 
126         return sb.toString();
127     }
128 
129     public String toString() {
130         return toJSONString();
131     }
132 
133     public static String toString(String key, Object value) {
134         StringBuilder sb = new StringBuilder();
135         toJSONString(key, value, sb);
136         return sb.toString();
137     }
138 
139     /**
140      * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
141      * (U+0000 through U+001F). It's the same as JSONValue.escape() only for
142      * compatibility here.
143      * 
144      * @see JSONValue#escape(String)
145      * 
146      * @param s
147      * @return
148      */
149     public static String escape(String s) {
150         return JSONValue.escape(s);
151     }
152 }