现在写java程序,很少需要写一些底层的数据结构和算法,因为这些轮子早已造好,拿来用就行。比如在代码中我们经常用到的这个类JSONObject ,还有我们经常使用的String类型,它都是有底层实现的,我们直接引用别人开发的java包就行了,无需要关心它是如何实现的, 所以平时开发其实对于底层的东西了解越来越少,这一节就分析一下JSONObject的源码,看它是如何实现的。
package com.alibaba.fastjson;
import static com.alibaba.fastjson.util.TypeUtils.castToBigDecimal;
import static com.alibaba.fastjson.util.TypeUtils.castToBigInteger;
import static com.alibaba.fastjson.util.TypeUtils.castToBoolean;
import static com.alibaba.fastjson.util.TypeUtils.castToByte;
import static com.alibaba.fastjson.util.TypeUtils.castToBytes;
import static com.alibaba.fastjson.util.TypeUtils.castToDate;
import static com.alibaba.fastjson.util.TypeUtils.castToDouble;
import static com.alibaba.fastjson.util.TypeUtils.castToFloat;
import static com.alibaba.fastjson.util.TypeUtils.castToInt;
import static com.alibaba.fastjson.util.TypeUtils.castToLong;
import static com.alibaba.fastjson.util.TypeUtils.castToShort;
import static com.alibaba.fastjson.util.TypeUtils.castToSqlDate;
import static com.alibaba.fastjson.util.TypeUtils.castToTimestamp;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
public class JSONObject extends JSON implements Map, Cloneable, Serializable, InvocationHandler {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private final Map map;
public JSONObject(){
this(DEFAULT_INITIAL_CAPACITY, false);
}
public JSONObject(Map map){
if (map == null) {
throw new IllegalArgumentException("map is null.");
}
this.map = map;
}
public JSONObject(boolean ordered){
this(DEFAULT_INITIAL_CAPACITY, ordered);
}
public JSONObject(int initialCapacity){
this(initialCapacity, false);
}
public JSONObject(int initialCapacity, boolean ordered){
if (ordered) {
map = new LinkedHashMap(initialCapacity);
} else {
map = new HashMap(initialCapacity);
}
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(Object key) {
boolean result = map.containsKey(key);
if (!result) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
result = map.containsKey(key.toString());
}
}
return result;
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
public Object get(Object key) {
Object val = map.get(key);
if (val == null) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
val = map.get(key.toString());
}
}
return val;
}
public Object getOrDefault(Object key, Object defaultValue) {
Object v;
return ((v = get(key)) != null) ? v : defaultValue;
}
public JSONObject getJSONObject(String key) {
Object value = map.get(key);
if (value instanceof JSONObject) {
return (JSONObject) value;
}
if (value instanceof Map) {
return new JSONObject((Map) value);
}
if (value instanceof String) {
return JSON.parseObject((String) value);
}
return (JSONObject) toJSON(value);
}
public JSONArray getJSONArray(String key) {
Object value = map.get(key);
if (value instanceof JSONArray) {
return (JSONArray) value;
}
if (value instanceof List) {
return new JSONArray((List) value);
}
if (value instanceof String) {
return (JSONArray) JSON.parse((String) value);
}
return (JSONArray) toJSON(value);
}
public T getObject(String key, Class clazz) {
Object obj = map.get(key);
return TypeUtils.castToJavaBean(obj, clazz);
}
public T getObject(String key, Type type) {
Object obj = map.get(key);
return TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance());
}
public T getObject(String key, TypeReference typeReference) {
Object obj = map.get(key);
if (typeReference == null) {
return (T) obj;
}
return TypeUtils.cast(obj, typeReference.getType(), ParserConfig.getGlobalInstance());
}
public Boolean getBoolean(String key) {
Object value = get(key);
if (value == null) {
return null;
}
return castToBoolean(value);
}
public byte[] getBytes(String key) {
Object value = get(key);
if (value == null) {
return null;
}
return castToBytes(value);
}
public boolean getBooleanValue(String key) {
Object value = get(key);
Boolean booleanVal = castToBoolean(value);
if (booleanVal == null) {
return false;
}
return booleanVal.booleanValue();
}
public Byte getByte(String key) {
Object value = get(key);
return castToByte(value);
}
public byte getByteValue(String key) {
Object value = get(key);
Byte byteVal = castToByte(value);
if (byteVal == null) {
return 0;
}
return byteVal.byteValue();
}
public Short getShort(String key) {
Object value = get(key);
return castToShort(value);
}
public short getShortValue(String key) {
Object value = get(key);
Short shortVal = castToShort(value);
if (shortVal == null) {
return 0;
}
return shortVal.shortValue();
}
public Integer getInteger(String key) {
Object value = get(key);
return castToInt(value);
}
public int getIntValue(String key) {
Object value = get(key);
Integer intVal = castToInt(value);
if (intVal == null) {
return 0;
}
return intVal.intValue();
}
public Long getLong(String key) {
Object value = get(key);
return castToLong(value);
}
public long getLongValue(String key) {
Object value = get(key);
Long longVal = castToLong(value);
if (longVal == null) {
return 0L;
}
return longVal.longValue();
}
public Float getFloat(String key) {
Object value = get(key);
return castToFloat(value);
}
public float getFloatValue(String key) {
Object value = get(key);
Float floatValue = castToFloat(value);
if (floatValue == null) {
return 0F;
}
return floatValue.floatValue();
}
public Double getDouble(String key) {
Object value = get(key);
return castToDouble(value);
}
public double getDoubleValue(String key) {
Object value = get(key);
Double doubleValue = castToDouble(value);
if (doubleValue == null) {
return 0D;
}
return doubleValue.doubleValue();
}
public BigDecimal getBigDecimal(String key) {
Object value = get(key);
return castToBigDecimal(value);
}
public BigInteger getBigInteger(String key) {
Object value = get(key);
return castToBigInteger(value);
}
public String getString(String key) {
Object value = get(key);
if (value == null) {
return null;
}
return value.toString();
}
public Date getDate(String key) {
Object value = get(key);
return castToDate(value);
}
public Object getSqlDate(String key) {
Object value = get(key);
return castToSqlDate(value);
}
public Object getTimestamp(String key) {
Object value = get(key);
return castToTimestamp(value);
}
public Object put(String key, Object value) {
return map.put(key, value);
}
public JSONObject fluentPut(String key, Object value) {
map.put(key, value);
return this;
}
public void putAll(Map extends String, ?> m) {
map.putAll(m);
}
public JSONObject fluentPutAll(Map extends String, ?> m) {
map.putAll(m);
return this;
}
public void clear() {
map.clear();
}
public JSONObject fluentClear() {
map.clear();
return this;
}
public Object remove(Object key) {
return map.remove(key);
}
public JSONObject fluentRemove(Object key) {
map.remove(key);
return this;
}
public Set keySet() {
return map.keySet();
}
public Collection
代码量还是很多,还没包含其他第三方引用的源码。
接下来我们一步步看
1、首先定义了这个类型:
public class JSONObject extends JSON implements Map
2、我们看它的构造方法
public JSONObject(){
this(DEFAULT_INITIAL_CAPACITY, false);
}
public JSONObject(Map map){
if (map == null) {
throw new IllegalArgumentException("map is null.");
}
this.map = map;
}
public JSONObject(boolean ordered){
this(DEFAULT_INITIAL_CAPACITY, ordered);
}
public JSONObject(int initialCapacity){
this(initialCapacity, false);
}
public JSONObject(int initialCapacity, boolean ordered){
if (ordered) {
map = new LinkedHashMap(initialCapacity);
} else {
map = new HashMap(initialCapacity);
}
}
粗略看一下,JSONObject的下一层大概就是hashMap来实现的。
private static final long serialVersionUID = 1L;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
DEFAULT_INITIAL_CAPACITY我们知道HasMap在构造JSONObject分配内存空间时 初始化是16
3、接下里的类里封装的方法都是map的方法
判断大小,判空,获取key等
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(Object key) {
boolean result = map.containsKey(key);
if (!result) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
result = map.containsKey(key.toString());
}
}
return result;
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
public Object get(Object key) {
Object val = map.get(key);
if (val == null) {
if (key instanceof Number
|| key instanceof Character
|| key instanceof Boolean
|| key instanceof UUID
) {
val = map.get(key.toString());
}
}
return val;
}
从这个方法我们可以看出,就是从HashMap转化过来的
public JSONObject getJSONObject(String key) {
Object value = map.get(key);
if (value instanceof JSONObject) {
return (JSONObject) value;
}
if (value instanceof Map) {
return new JSONObject((Map) value);
}
if (value instanceof String) {
return JSON.parseObject((String) value);
}
return (JSONObject) toJSON(value);
}
功能非常多,以至于源码就10000多行。