com.ibm.commons.util.io.json.JsonJavaObject Maven / Gradle / Ivy
The newest version!
/*
* © Copyright IBM Corp. 2012-2013
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.ibm.commons.util.io.json;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.ibm.commons.Platform;
/**
* Default JSON implementation for a JsonObject.
*
* It can be used instead of Maps as it proposes some very convenient methods for
* accessing the properties.
* This implementation internal uses a Map for storing the properties.
*
* @ibm-api
*/
public class JsonJavaObject extends HashMap implements JsonObject, Serializable {
private static final long serialVersionUID = 1L;
public JsonJavaObject() {
}
@SuppressWarnings("unchecked")
public JsonJavaObject(Map props) {
super(props!=null?props:Collections.EMPTY_MAP);
}
public JsonJavaObject(Object...properties) {
if(properties!=null) {
for(int i=0; i getJsonProperties() {
return getProperties();
}
public final Object getJsonProperty(String property) {
return get(property);
}
public final void putJsonProperty(String property, Object value) {
put(property, value);
}
// Json Object implementation
/** @ibm-api */
public boolean isEmpty() {
return super.isEmpty();
}
/** @ibm-api */
public Iterator getProperties() {
return keySet().iterator();
}
/** @ibm-api */
public Object get(String property) {
return super.get(property);
}
/** @ibm-api */
public String getString(String property) {
Object o = get(property);
if(o!=null) {
return o.toString();
}
return null;
}
/** @ibm-api */
public int getInt(String property) {
Object o = get(property);
if(o!=null) {
return ((Number)o).intValue();
}
return 0;
}
/** @ibm-api */
public long getLong(String property) {
Object o = get(property);
if(o!=null) {
return ((Number)o).longValue();
}
return 0L;
}
/** @ibm-api */
public double getDouble(String property) {
Object o = get(property);
if(o!=null) {
return ((Number)o).doubleValue();
}
return 0.0;
}
/** @ibm-api */
public boolean getBoolean(String property) {
Object o = get(property);
if(o!=null) {
return ((Boolean)o).booleanValue();
}
return false;
}
/** @ibm-api */
public JsonJavaObject getJsonObject(String property) {
Object o = get(property);
return (JsonJavaObject)o;
}
/** @ibm-api */
public void remove(String property) {
super.remove(property);
}
/** @ibm-api */
public void removeAll() {
super.clear();
}
/** @ibm-api */
public Object put(String property, Object value) {
return super.put(property,value);
}
/** @ibm-api */
public void putString(String property, Object value) {
putString(property,value!=null?value.toString():null);
}
/** @ibm-api */
public void putString(String property, String value) {
put(property,value);
}
/** @ibm-api */
public void putInt(String property, int value) {
put(property,value);
}
/** @ibm-api */
public void putLong(String property, long value) {
put(property,value);
}
/** @ibm-api */
public void putDouble(String property, double value) {
put(property,value);
}
/** @ibm-api */
public void putBoolean(String property, boolean value) {
put(property,Boolean.valueOf(value));
}
/** @ibm-api */
public void putObject(String property, Object value) {
put(property,value);
}
/** @ibm-api */
public void putMap(String property, Map value) {
put(property,value);
}
/** @ibm-api */
public void putArray(String property, JsonJavaArray value) {
put(property,value);
}
/** @ibm-api */
public void putList(String property, List