com.alogic.xscript.doc.json.JsonObject Maven / Gradle / Ivy
package com.alogic.xscript.doc.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.BooleanUtils;
import com.alogic.xscript.doc.XsArray;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.XsPrimitiveArray;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;
/**
* 基于Json的XsObject
* @author yyduan
* @since 1.6.8.14
*/
public class JsonObject implements XsObject {
/**
* content
*/
protected Map content;
protected String tag;
public JsonObject(String tag,Map content) {
this.content = content;
this.tag = tag;
}
@Override
public String getTag() {
return tag;
}
@Override
public Object getContent() {
return this.content;
}
@Override
public void addProperty(String name, String value) {
this.content.put(name, value);
}
@Override
public void addProperty(String name, Number value) {
this.content.put(name, value);
}
@Override
public void addProperty(String name, boolean value) {
this.content.put(name, value);
}
@Override
public boolean remove(String name) {
return this.content.remove(name) != null;
}
@Override
public boolean hasProperty(String name) {
return this.content.containsKey(name);
}
@Override
public String getProperty(String name, String dft) {
Object found = this.content.get(name);
if (found == null){
return dft;
}
if (found instanceof String){
return (String)found;
}
return found.toString();
}
@Override
public long getProperty(String name, long dft) {
Object found = this.content.get(name);
if (found == null){
return dft;
}
if (found instanceof Number){
Number v = (Number)found;
return v.longValue();
}
if (found instanceof String){
String v = (String)found;
try {
return Long.parseLong(v);
}catch (NumberFormatException ex){
return dft;
}
}
return dft;
}
@Override
public int getProperty(String name, int dft) {
Object found = this.content.get(name);
if (found == null){
return dft;
}
if (found instanceof Number){
Number v = (Number)found;
return v.intValue();
}
if (found instanceof String){
String v = (String)found;
try {
return Integer.parseInt(v);
}catch (NumberFormatException ex){
return dft;
}
}
return dft;
}
@Override
public boolean getProperty(String name, boolean dft) {
Object found = this.content.get(name);
if (found == null){
return dft;
}
if (found instanceof Boolean){
return (Boolean)found;
}
if (found instanceof String){
String v = (String)found;
return BooleanUtils.toBoolean(v);
}
return dft;
}
@Override
public float getProperty(String name, float dft) {
Object found = this.content.get(name);
if (found == null){
return dft;
}
if (found instanceof Number){
Number v = (Number)found;
return v.intValue();
}
if (found instanceof String){
String v = (String)found;
try {
return Float.parseFloat(v);
}catch (NumberFormatException ex){
return dft;
}
}
return dft;
}
@Override
public double getProperty(String name, double dft) {
Object found = this.content.get(name);
if (found == null){
return dft;
}
if (found instanceof Number){
Number v = (Number)found;
return v.doubleValue();
}
if (found instanceof String){
String v = (String)found;
try {
return Double.parseDouble(v);
}catch (NumberFormatException ex){
return dft;
}
}
return dft;
}
@SuppressWarnings("unchecked")
@Override
public XsArray getArrayChild(String name,boolean create) {
Object found = content.get(name);
if (found == null){
if (create){
found = new ArrayList