Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package org.json.simple

File JSONObject.java

 

Coverage histogram

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

Code metrics

8
36
9
1
132
70
14
0.39
4
9
1.56

Classes

Class Line # Actions
JSONObject 19 36 14
0.037735853.8%
 

Contributing tests

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