001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.json;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: public class JSONObject implements JSONValue {
029:
030: final static char LCB = '{';
031: final static char RCB = '}';
032: final static char COL = ':';
033: final static char COM = ',';
034:
035: final static List<String> emptyIteratorList = new ArrayList<String>();
036:
037: Map<String, Object> members;
038:
039: public boolean hasMember(String memberName) {
040: if (members == null)
041: return false;
042: return members.get(memberName) != null;
043: }
044:
045: public Iterator<String> getMemberNames() {
046: if (members == null)
047: return emptyIteratorList.iterator();
048: return members.keySet().iterator();
049: }
050:
051: public void putMember(String memberName, Object memberValue) {
052: if (members == null)
053: members = new HashMap<String, Object>();
054: members.put(memberName, memberValue);
055: }
056:
057: public Object getMember(String memberName) {
058: if (members == null)
059: return null;
060: return members.get(memberName);
061: }
062:
063: public String getStringMember(String memberName) {
064: Object obj = members.get(memberName);
065: if (obj != null) {
066: if (!(obj instanceof String))
067: throw new IllegalArgumentException("Member '"
068: + memberName + "' has wrong type: "
069: + obj.getClass().getName());
070: return (String) obj;
071: }
072: return null;
073: }
074:
075: public Boolean getBooleanMember(String memberName) {
076: Object obj = members.get(memberName);
077: if (obj != null) {
078: if (!(obj instanceof Boolean))
079: throw new IllegalArgumentException("Member '"
080: + memberName + "' has wrong type: "
081: + obj.getClass().getName());
082: return (Boolean) obj;
083: }
084: return null;
085: }
086:
087: public Number getNumberMember(String memberName) {
088: Object obj = members.get(memberName);
089: if (obj != null) {
090: if (!(obj instanceof Number))
091: throw new IllegalArgumentException("Member '"
092: + memberName + "' has wrong type: "
093: + obj.getClass().getName());
094: return (Number) obj;
095: }
096: return null;
097: }
098:
099: public JSONArray getArrayMember(String memberName) {
100: Object obj = members.get(memberName);
101: if (obj != null) {
102: if (!(obj instanceof JSONArray))
103: throw new IllegalArgumentException("Member '"
104: + memberName + "' has wrong type: "
105: + obj.getClass().getName());
106: return (JSONArray) obj;
107: }
108: return null;
109: }
110:
111: public String toJSONString() {
112: StringBuffer sb = new StringBuffer();
113: sb.append(LCB);
114: if (members != null) {
115: Iterator<String> it = getMemberNames();
116: while (it.hasNext()) {
117: String memberName = it.next();
118: sb.append(ParserUtils.jsonEscape(memberName));
119: sb.append(COL);
120: Object memberValue = getMember(memberName);
121: sb.append(ParserUtils.javaToJson(memberValue));
122: if (it.hasNext())
123: sb.append(COM);
124: }
125: }
126: sb.append(RCB);
127: return sb.toString();
128: }
129:
130: }
|