org.zodiac.sdk.json.JSONValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-json Show documentation
Show all versions of zodiac-sdk-json Show documentation
Zodiac SDK JSON(JavaScript Object Notation)
package org.zodiac.sdk.json;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Date;
import org.zodiac.sdk.json.exception.JSONException;
public class JSONValue {
protected String _string; //字符串
protected boolean _bool; //布尔值
protected Date _date; //日期
protected Number _number;//数字
protected JSONNode _n;
public JSONValue(JSONNode n) {
_n = n;
}
private JSONValueType _type = JSONValueType.Null;
public JSONValueType type() {
return _type;
}
public void set(Object val) {
if (val == null) {
_type = JSONValueType.Null;
return;
}
if (val instanceof String) { //string
setString((String) val);
return;
}
if (val instanceof Date) { //date
setDate((Date) val);
return;
}
if (val instanceof Number) { //number
setNumber((Number) val);
return;
}
if (val instanceof Boolean) { //bool
setBool((Boolean) val);
return;
}
throw new JSONException("Unsupported type class" + val.getClass().getName());
}
public void setNull() {
_type = JSONValueType.Null;
}
public void setNumber(Number val) {
_type = JSONValueType.Number;
_number = val;
}
public void setString(String val) {
_type = JSONValueType.String;
_string = val;
}
public void setBool(boolean val) {
_type = JSONValueType.Boolean;
_bool = val;
}
public void setDate(Date val) {
_type = JSONValueType.DateTime;
_date = val;
}
//==================
public Object getRaw() {
switch (_type) {
case String:
return _string;
case DateTime:
return _date;
case Boolean:
return _bool;
case Number:
return _number;
default:
return null;
}
}
public String getRawString() {
return _string;
}
public boolean getRawBoolean() {
return _bool;
}
public Date getRawDate() {
return _date;
}
public Number getRawNumber() {
return _number;
}
//==================
public boolean isNull() {
return _type == JSONValueType.Null;
}
public char getChar() {
switch (_type) {
case Number:
return (char) _number.longValue();
case String:
if (_string == null || _string.length() == 0) {
return 0;
} else {
return _string.charAt(0);
}
case Boolean:
return _bool ? '1' : '0';
case DateTime:
return 0;
default:
return 0;
}
}
public short getShort() {
return (short) getLong();
}
public int getInt() {
return (int) getLong();
}
public long getLong() {
switch (_type) {
case Number:
return _number.longValue();
case String: {
if (_string == null || _string.length() == 0) {
return 0;
} else {
return Long.parseLong(_string);
}
}
case Boolean:
return _bool ? 1 : 0;
case DateTime:
return _date.getTime();
default:
return 0;
}
}
public float getFloat() {
return (float) getDouble();
}
public double getDouble() {
switch (_type) {
case Number:
return _number.doubleValue();
case String: {
if (_string == null || _string.length() == 0) {
return 0;
} else {
return Double.parseDouble(_string);
}
}
case Boolean:
return _bool ? 1 : 0;
case DateTime:
return _date.getTime();
default:
return 0;
}
}
public String getString() {
switch (_type) {
case String:
return _string;
case Number: {
if (_number instanceof BigInteger) {
return ((BigInteger) _number).toString();
} else if (_number instanceof BigDecimal) {
return ((BigDecimal) _number).toPlainString();
} else {
return String.valueOf(_number);
}
}
case Boolean:
return String.valueOf(_bool);
case DateTime:
return String.valueOf(_date);
default:
return _n._o.nullString();
}
}
public boolean getBoolean() {
switch (_type) {
case Boolean:
return _bool;
case Number:
return _number.intValue() > 0;
case String:
return false;
case DateTime:
return false;
default:
return false;
}
}
public Date getDate() {
switch (_type) {
case DateTime:
return _date;
case String:
return parseDate(_string);
case Number: {
if (_number instanceof Long) {
return new Date(_number.longValue());
} else {
return null;
}
}
default:
return null;
}
}
private Date parseDate(String dateString) {
try {
return _n._o.stringToDate(dateString);
} catch (ParseException ex) {
return null;
}
}
@Override
public String toString() {
return getString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return isNull();
}
if (o instanceof JSONValue) {
JSONValue o2 = (JSONValue) o;
switch (_type) {
case String:
return _string.equals(o2._string);
case DateTime:
return _date.equals(o2._date);
case Boolean:
return _bool == o2._bool;
case Number: {
if (_number instanceof BigInteger) {
return toString().equals(o2.toString());
} else if (_number instanceof BigDecimal) {
return toString().equals(o2.toString());
} else if (_number instanceof Double || _number instanceof Float) {
return getDouble() == o2.getDouble();
} else {
return getLong() == o2.getLong();
}
}
default:
return isNull() && o2.isNull();
}
} else {
switch (_type) {
case String:
return _string.equals(o);
case DateTime:
return _date.equals(o);
case Boolean: {
if (o instanceof Boolean) {
return _bool == (Boolean) o;
} else {
return false;
}
}
case Number: {
if (o instanceof Number) {
Number o2 = (Number) o;
if (_number instanceof BigInteger) {
return toString().equals(o2.toString());
} else if (_number instanceof BigDecimal) {
return toString().equals(o2.toString());
} else if (_number instanceof Double || _number instanceof Float) {
return _number.doubleValue() == o2.doubleValue();
} else {
return _number.longValue() == o2.longValue();
}
} else {
return false;
}
}
default:
return false;
}
}
}
@Override
public int hashCode() {
switch (_type) {
case String:
return _string.hashCode();
case DateTime:
return _date.hashCode();
case Boolean:
return Boolean.hashCode(_bool);
case Number:
return _number.hashCode();
default:
return 0;
}
}
}