001package ca.uhn.fhir.parser.json; 002 003import java.util.Set; 004 005/* 006 * #%L 007 * HAPI FHIR - Core Library 008 * %% 009 * Copyright (C) 2014 - 2017 University Health Network 010 * %% 011 * Licensed under the Apache License, Version 2.0 (the "License"); 012 * you may not use this file except in compliance with the License. 013 * You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software 018 * distributed under the License is distributed on an "AS IS" BASIS, 019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 020 * See the License for the specific language governing permissions and 021 * limitations under the License. 022 * #L% 023 */ 024 025public abstract class JsonLikeObject extends JsonLikeValue { 026 027 @Override 028 public ValueType getJsonType() { 029 return ValueType.OBJECT; 030 } 031 032 @Override 033 public ScalarType getDataType() { 034 return null; 035 } 036 037 @Override 038 public boolean isObject() { 039 return true; 040 } 041 042 @Override 043 public JsonLikeObject getAsObject() { 044 return this; 045 } 046 047 @Override 048 public String getAsString() { 049 return null; 050 } 051 052 public abstract Set<String> keySet (); 053 054 public abstract JsonLikeValue get (String key); 055 056 public String getString (String key) { 057 JsonLikeValue value = this.get(key); 058 if (null == value) { 059 throw new NullPointerException("Json object missing element named \""+key+"\""); 060 } 061 return value.getAsString(); 062 } 063 064 public String getString (String key, String defaultValue) { 065 String result = defaultValue; 066 JsonLikeValue value = this.get(key); 067 if (value != null) { 068 result = value.getAsString(); 069 } 070 return result; 071 } 072}