001/* Copyright 2006 FangYidong
002
003   Licensed under the Apache License, Version 2.0 (the "License");
004   you may not use this file except in compliance with the License.
005   You may obtain a copy of the License at
006
007       http://www.apache.org/licenses/LICENSE-2.0
008
009   Unless required by applicable law or agreed to in writing, software
010   distributed under the License is distributed on an "AS IS" BASIS,
011   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012   See the License for the specific language governing permissions and
013   limitations under the License. */
014package org.json.simple;
015
016import java.io.IOException;
017import java.io.StringWriter;
018import java.io.Writer;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023/**
024 * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
025 * 
026 * @author FangYidong<fangyidong@yahoo.com.cn>
027 * @deprecated since 2.0.0, replaced by {@link org.json.simple.JsonObject}
028 */
029@Deprecated
030public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{
031        
032        private static final long serialVersionUID = -503443796854799292L;
033        
034        
035        /**
036         * 
037         */
038        public JSONObject() {
039                super();
040        }
041
042        /**
043         * Allows creation of a JSONObject from a Map. After that, both the
044         * generated JSONObject and the Map can be modified independently.
045         * 
046         * @param map description omitted.
047         */
048        public JSONObject(Map map) {
049                super(map);
050        }
051
052
053    /**
054     * Encode a map into JSON text and write it to out.
055     * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
056     * 
057     * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
058     * 
059     * @param map description omitted.
060     * @param out description omitted.
061     * @throws IOException description omitted.
062     */
063        public static void writeJSONString(Map map, Writer out) throws IOException {
064                if(map == null){
065                        out.write("null");
066                        return;
067                }
068                
069                boolean first = true;
070                Iterator iter=map.entrySet().iterator();
071                
072        out.write('{');
073                while(iter.hasNext()){
074            if(first)
075                first = false;
076            else
077                out.write(',');
078                        Map.Entry entry=(Map.Entry)iter.next();
079            out.write('\"');
080            out.write(escape(String.valueOf(entry.getKey())));
081            out.write('\"');
082            out.write(':');
083                        JSONValue.writeJSONString(entry.getValue(), out);
084                }
085                out.write('}');
086        }
087
088        public void writeJSONString(Writer out) throws IOException{
089                writeJSONString(this, out);
090        }
091        
092        /**
093         * Convert a map to JSON text. The result is a JSON object. 
094         * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
095         * 
096         * @see org.json.simple.JSONValue#toJSONString(Object)
097         * 
098         * @param map description omitted.
099         * @return JSON text, or "null" if map is null.
100         */
101        public static String toJSONString(Map map){
102                final StringWriter writer = new StringWriter();
103                
104                try {
105                        writeJSONString(map, writer);
106                        return writer.toString();
107                } catch (IOException e) {
108                        // This should never happen with a StringWriter
109                        throw new RuntimeException(e);
110                }
111        }
112        
113        public String toJSONString(){
114                return toJSONString(this);
115        }
116        
117        public String toString(){
118                return toJSONString();
119        }
120
121        /**
122         * description omitted.
123         *
124         * @param key description omitted.
125         * @param value description omitted.
126         * @return description omitted.
127         */
128        public static String toString(String key,Object value){
129        StringBuffer sb = new StringBuffer();
130        sb.append('\"');
131        if(key == null)
132            sb.append("null");
133        else
134            JSONValue.escape(key, sb);
135                sb.append('\"').append(':');
136                
137                sb.append(JSONValue.toJSONString(value));
138                
139                return sb.toString();
140        }
141        
142        /**
143         * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
144         * It's the same as JSONValue.escape() only for compatibility here.
145         * 
146         * @see org.json.simple.JSONValue#escape(String)
147         * 
148         * @param s description omitted.
149         * @return description omitted.
150         */
151        public static String escape(String s){
152                return JSONValue.escape(s);
153        }
154}