1 /**
2 * Copyright 2007 Art Technology Group, Inc (ATG)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and limitations under the License.
13 */
14
15 package atg.taglib.json;
16
17 import atg.taglib.json.util.JSONArray;
18 import atg.taglib.json.util.JSONException;
19 import atg.taglib.json.util.JSONObject;
20
21 /**
22 * Wrapper object for JSON Entities. A JSON Entity can be either a JSONObject or
23 * JSONArray. One of these object types will always be sitting on the top of the
24 * object stack, so this class provides a wrapper that can be used to either add
25 * a property to an object or append a property to an array.
26 *
27 * @author James Wiltshire
28 * @version $Id$
29 */
30
31 public class JsonEntity
32 {
33 private Object mWrappedObject;
34
35 /**
36 * Create a new JsonEntity object
37 *
38 * @param pWrapped The underlying object to wrap
39 */
40 public JsonEntity(Object pWrapped){
41 this.mWrappedObject=pWrapped;
42 }
43
44 /**
45 * Add a named entity to a JSONObject or append an entity to a JSONArray
46 * depending on whether the wrapped object is an object or array.
47 *
48 * @param pEntity The entity to add to the wrapped object
49 * @param pName The property name of the entity, or <code>null</code> if not available
50 * @throws JSONException
51 */
52 public void add(Object pEntity,String pName) throws JSONException{
53 if (mWrappedObject instanceof JSONObject){
54 if (pName==null){
55 throw new JSONException("Unable to add to JSONObject - property name is required.");
56 }
57 ((JSONObject)mWrappedObject).put(pName, pEntity);
58 }
59 else if (mWrappedObject instanceof JSONArray){
60 ((JSONArray)mWrappedObject).add(pEntity);
61 }
62 }
63
64 /**
65 * Get the underlying wrapped JSONObject or JSONArray object
66 *
67 * @return The wrapped object
68 */
69 public Object getWrappedObject()
70 {
71 return mWrappedObject;
72 }
73
74 /**
75 * Is this entity wrapping a JSONArray?
76 *
77 * @return <code>true</code> if this entity wraps a JSONArray, <code>false</code> otherwise
78 */
79 public boolean isArray()
80 {
81 return (mWrappedObject instanceof JSONArray);
82 }
83
84 /**
85 * Is this entity wrapping a JSONObject?
86 *
87 * @return <code>true</code> if this entity wraps a JSONObject, <code>false</code> otherwise
88 */
89 public boolean isObject()
90 {
91 return (mWrappedObject instanceof JSONObject);
92 }
93
94 /**
95 * Serialize the underlying wrapped entity to a JSON string
96 * @see java.lang.Object#toString()
97 *
98 * @return The wrapped JSON entity as a string
99 */
100 public String toString()
101 {
102 return mWrappedObject.toString();
103 }
104
105 /**
106 * Serialize the underlying wrapped entity to a JSON string
107 *
108 * @param pIndentFactor The number of spaces to use to pretty-print the data
109 * @return The wrapped JSON entity as a string
110 */
111 public String toString(int pIndentFactor) throws JSONException
112 {
113 if (isArray()){
114 return ((JSONArray)mWrappedObject).toString(pIndentFactor);
115 }
116 else if (isObject()){
117 return ((JSONObject)mWrappedObject).toString(pIndentFactor);
118 }
119 else{
120
121 return "";
122 }
123 }
124
125 }
126