Clover icon

Coverage Report

  1. Project Clover database Wed Nov 6 2024 00:56:24 GMT
  2. Package org.json.simple

File JSONObject.java

 

Coverage histogram

../../../img/srcFileCovDistChart1.png
56% of files have more coverage

Code metrics

8
36
9
1
149
85
14
0.39
4
9
1.56

Classes

Class Line # Actions
JSONObject 20 36 14
0.037735853.8%
 

Contributing tests

This file is covered by 92 tests. .

Source view

1    /*
2    * $Id: JSONObject.java,v 1.1 2006/04/15 14:10:48 platform Exp $
3    * Created on 2006-4-10
4    */
5    package org.json.simple;
6   
7    import java.io.IOException;
8    import java.io.StringWriter;
9    import java.io.Writer;
10    import java.util.HashMap;
11    import java.util.Iterator;
12    import java.util.Map;
13   
14    /**
15    * A JSON object. Key value pairs are unordered. JSONObject supports
16    * java.util.Map interface.
17    *
18    * @author FangYidong<fangyidong@yahoo.com.cn>
19    */
 
20    public class JSONObject extends HashMap
21    implements Map, JSONAware, JSONStreamAware
22    {
23   
24    private static final long serialVersionUID = -503443796854799292L;
25   
 
26  53547 toggle public JSONObject()
27    {
28  53547 super();
29    }
30   
31    /**
32    * Allows creation of a JSONObject from a Map. After that, both the generated
33    * JSONObject and the Map can be modified independently.
34    *
35    * @param map
36    */
 
37  0 toggle public JSONObject(Map map)
38    {
39  0 super(map);
40    }
41   
42    /**
43    * Encode a map into JSON text and write it to out. If this map is also a
44    * JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific
45    * behaviours will be ignored at this top level.
46    *
47    * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
48    *
49    * @param map
50    * @param out
51    */
 
52  0 toggle public static void writeJSONString(Map map, Writer out) throws IOException
53    {
54  0 if (map == null)
55    {
56  0 out.write("null");
57  0 return;
58    }
59   
60  0 boolean first = true;
61  0 Iterator iter = map.entrySet().iterator();
62   
63  0 out.write('{');
64  0 while (iter.hasNext())
65    {
66  0 if (first)
67  0 first = false;
68    else
69  0 out.write(',');
70  0 Map.Entry entry = (Map.Entry) iter.next();
71  0 out.write('\"');
72  0 out.write(escape(String.valueOf(entry.getKey())));
73  0 out.write('\"');
74  0 out.write(':');
75  0 JSONValue.writeJSONString(entry.getValue(), out);
76    }
77  0 out.write('}');
78    }
79   
 
80  0 toggle public void writeJSONString(Writer out) throws IOException
81    {
82  0 writeJSONString(this, out);
83    }
84   
85    /**
86    * Convert a map to JSON text. The result is a JSON object. If this map is
87    * also a JSONAware, JSONAware specific behaviours will be omitted at this top
88    * level.
89    *
90    * @see org.json.simple.JSONValue#toJSONString(Object)
91    *
92    * @param map
93    * @return JSON text, or "null" if map is null.
94    */
 
95  0 toggle public static String toJSONString(Map map)
96    {
97  0 final StringWriter writer = new StringWriter();
98   
99  0 try
100    {
101  0 writeJSONString(map, writer);
102  0 return writer.toString();
103    } catch (IOException e)
104    {
105    // This should never happen with a StringWriter
106  0 throw new RuntimeException(e);
107    }
108    }
109   
 
110  0 toggle public String toJSONString()
111    {
112  0 return toJSONString(this);
113    }
114   
 
115  0 toggle public String toString()
116    {
117  0 return toJSONString();
118    }
119   
 
120  0 toggle public static String toString(String key, Object value)
121    {
122  0 StringBuffer sb = new StringBuffer();
123  0 sb.append('\"');
124  0 if (key == null)
125  0 sb.append("null");
126    else
127  0 JSONValue.escape(key, sb);
128  0 sb.append('\"').append(':');
129   
130  0 sb.append(JSONValue.toJSONString(value));
131   
132  0 return sb.toString();
133    }
134   
135    /**
136    * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
137    * (U+0000 through U+001F). It's the same as JSONValue.escape() only for
138    * compatibility here.
139    *
140    * @see org.json.simple.JSONValue#escape(String)
141    *
142    * @param s
143    * @return
144    */
 
145  0 toggle public static String escape(String s)
146    {
147  0 return JSONValue.escape(s);
148    }
149    }