]>
iEval git - unical.git/blob - JsonObject.java
78c7a177a89f67d937faf739edc5800cbcf95b04
2 * Copyright (C) 2008 Google Inc.
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 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.google
.gson
;
19 import com
.google
.gson
.internal
.LinkedTreeMap
;
25 * A class representing an object type in Json. An object consists of name-value pairs where names
26 * are strings, and values are any other type of {@link JsonElement}. This allows for a creating a
27 * tree of JsonElements. The member elements of this object are maintained in order they were added.
29 * @author Inderjeet Singh
32 public final class JsonObject
extends JsonElement
{
33 private final LinkedTreeMap
<String
, JsonElement
> members
=
34 new LinkedTreeMap
<String
, JsonElement
>();
37 JsonObject
deepCopy() {
38 JsonObject result
= new JsonObject();
39 for (Map
.Entry
<String
, JsonElement
> entry
: members
.entrySet()) {
40 result
.add(entry
.getKey(), entry
.getValue().deepCopy());
46 * Adds a member, which is a name-value pair, to self. The name must be a String, but the value
47 * can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements
48 * rooted at this node.
50 * @param property name of the member.
51 * @param value the member object.
53 public void add(String property
, JsonElement value
) {
55 value
= JsonNull
.INSTANCE
;
57 members
.put(property
, value
);
61 * Removes the {@code property} from this {@link JsonObject}.
63 * @param property name of the member that should be removed.
64 * @return the {@link JsonElement} object that is being removed.
67 public JsonElement
remove(String property
) {
68 return members
.remove(property
);
72 * Convenience method to add a primitive member. The specified value is converted to a
73 * JsonPrimitive of String.
75 * @param property name of the member.
76 * @param value the string value associated with the member.
78 public void addProperty(String property
, String value
) {
79 add(property
, createJsonElement(value
));
83 * Convenience method to add a primitive member. The specified value is converted to a
84 * JsonPrimitive of Number.
86 * @param property name of the member.
87 * @param value the number value associated with the member.
89 public void addProperty(String property
, Number value
) {
90 add(property
, createJsonElement(value
));
94 * Convenience method to add a boolean member. The specified value is converted to a
95 * JsonPrimitive of Boolean.
97 * @param property name of the member.
98 * @param value the number value associated with the member.
100 public void addProperty(String property
, Boolean value
) {
101 add(property
, createJsonElement(value
));
105 * Convenience method to add a char member. The specified value is converted to a
106 * JsonPrimitive of Character.
108 * @param property name of the member.
109 * @param value the number value associated with the member.
111 public void addProperty(String property
, Character value
) {
112 add(property
, createJsonElement(value
));
116 * Creates the proper {@link JsonElement} object from the given {@code value} object.
118 * @param value the object to generate the {@link JsonElement} for
119 * @return a {@link JsonPrimitive} if the {@code value} is not null, otherwise a {@link JsonNull}
121 private JsonElement
createJsonElement(Object value
) {
122 return value
== null ? JsonNull
.INSTANCE
: new JsonPrimitive(value
);
126 * Returns a set of members of this object. The set is ordered, and the order is in which the
127 * elements were added.
129 * @return a set of members of this object.
131 public Set
<Map
.Entry
<String
, JsonElement
>> entrySet() {
132 return members
.entrySet();
136 * Convenience method to check if a member with the specified name is present in this object.
138 * @param memberName name of the member that is being checked for presence.
139 * @return true if there is a member with the specified name, false otherwise.
141 public boolean has(String memberName
) {
142 return members
.containsKey(memberName
);
146 * Returns the member with the specified name.
148 * @param memberName name of the member that is being requested.
149 * @return the member matching the name. Null if no such member exists.
151 public JsonElement
get(String memberName
) {
152 return members
.get(memberName
);
156 * Convenience method to get the specified member as a JsonPrimitive element.
158 * @param memberName name of the member being requested.
159 * @return the JsonPrimitive corresponding to the specified member.
161 public JsonPrimitive
getAsJsonPrimitive(String memberName
) {
162 return (JsonPrimitive
) members
.get(memberName
);
166 * Convenience method to get the specified member as a JsonArray.
168 * @param memberName name of the member being requested.
169 * @return the JsonArray corresponding to the specified member.
171 public JsonArray
getAsJsonArray(String memberName
) {
172 return (JsonArray
) members
.get(memberName
);
176 * Convenience method to get the specified member as a JsonObject.
178 * @param memberName name of the member being requested.
179 * @return the JsonObject corresponding to the specified member.
181 public JsonObject
getAsJsonObject(String memberName
) {
182 return (JsonObject
) members
.get(memberName
);
186 public boolean equals(Object o
) {
187 return (o
== this) || (o
instanceof JsonObject
188 && ((JsonObject
) o
).members
.equals(members
));
192 public int hashCode() {
193 return members
.hashCode();
This page took 0.054585 seconds and 3 git commands to generate.