View Javadoc
1   package ca.uhn.fhir.parser.json;
2   
3   import java.util.Set;
4   
5   /*
6    * #%L
7    * HAPI FHIR - Core Library
8    * %%
9    * Copyright (C) 2014 - 2018 University Health Network
10   * %%
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   * 
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   * 
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   * #L%
23   */
24  
25  public abstract class JsonLikeObject extends JsonLikeValue {
26  
27  	@Override
28  	public ValueType getJsonType() {
29  		return ValueType.OBJECT;
30  	}
31  	
32  	@Override
33  	public ScalarType getDataType() {
34  		return null;
35  	}
36  
37  	@Override
38  	public boolean isObject() {
39  		return true;
40  	}
41  
42  	@Override
43  	public JsonLikeObject getAsObject() {
44  		return this;
45  	}
46  
47  	@Override
48  	public String getAsString() {
49  		return null;
50  	}
51  
52  	public abstract Set<String> keySet ();
53  	
54  	public abstract JsonLikeValue get (String key);
55  	
56  	public String getString (String key) {
57  		JsonLikeValue value = this.get(key);
58  		if (null == value) {
59  			throw new NullPointerException("Json object missing element named \""+key+"\"");
60  		}
61  		return value.getAsString();
62  	}
63  	
64  	public String getString (String key, String defaultValue) {
65  		String result = defaultValue;
66  		JsonLikeValue value = this.get(key);
67  		if (value != null) {
68  			result = value.getAsString();
69  		}
70  		return result;
71  	}
72  }