fastjson-JSONObject-類JSON互轉(zhuǎn)

/*

  • Copyright 1999-2017 Alibaba Group.
  • 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.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.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.List;

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<String, Object>, Cloneable, Serializable, InvocationHandler {

private static final long         serialVersionUID         = 1L;
private static final int          DEFAULT_INITIAL_CAPACITY = 16;

private final Map<String, Object> map;

public JSONObject(){
    this(DEFAULT_INITIAL_CAPACITY, false);
}

public JSONObject(Map<String, Object> 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<String, Object>(initialCapacity);
    } else {
        map = new HashMap<String, Object>(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) && key instanceof Number) {
        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 && key instanceof Number) {
        val = map.get(key.toString());
    }

    return val;
}

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> T getObject(String key, Class<T> clazz) {
    Object obj = map.get(key);
    return TypeUtils.castToJavaBean(obj, clazz);
}

public <T> T getObject(String key, Type type) {
    Object obj = map.get(key);
    return TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance());
}

public <T> 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 java.sql.Date getSqlDate(String key) {
    Object value = get(key);

    return castToSqlDate(value);
}

public java.sql.Timestamp 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, ? extends Object> m) {
    map.putAll(m);
}

public JSONObject fluentPutAll(Map<? extends String, ? extends Object> 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<String> keySet() {
    return map.keySet();
}

public Collection<Object> values() {
    return map.values();
}

public Set<Map.Entry<String, Object>> entrySet() {
    return map.entrySet();
}

@Override
public Object clone() {
    return new JSONObject(map instanceof LinkedHashMap //
            ? new LinkedHashMap<String, Object>(map) //
            : new HashMap<String, Object>(map)
    );
}

public boolean equals(Object obj) {
    return this.map.equals(obj);
}

public int hashCode() {
    return this.map.hashCode();
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?>[] parameterTypes = method.getParameterTypes();
    if (parameterTypes.length == 1) {
        if (method.getName().equals("equals")) {
            return this.equals(args[0]);
        }

        Class<?> returnType = method.getReturnType();
        if (returnType != void.class) {
            throw new JSONException("illegal setter");
        }

        String name = null;
        JSONField annotation = TypeUtils.getAnnotation(method, JSONField.class);
        if (annotation != null) {
            if (annotation.name().length() != 0) {
                name = annotation.name();
            }
        }

        if (name == null) {
            name = method.getName();

            if (!name.startsWith("set")) {
                throw new JSONException("illegal setter");
            }

            name = name.substring(3);
            if (name.length() == 0) {
                throw new JSONException("illegal setter");
            }
            name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
        }

        map.put(name, args[0]);
        return null;
    }

    if (parameterTypes.length == 0) {
        Class<?> returnType = method.getReturnType();
        if (returnType == void.class) {
            throw new JSONException("illegal getter");
        }

        String name = null;
        JSONField annotation = TypeUtils.getAnnotation(method, JSONField.class);
        if (annotation != null) {
            if (annotation.name().length() != 0) {
                name = annotation.name();
            }
        }

        if (name == null) {
            name = method.getName();
            if (name.startsWith("get")) {
                name = name.substring(3);
                if (name.length() == 0) {
                    throw new JSONException("illegal getter");
                }
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
            } else if (name.startsWith("is")) {
                name = name.substring(2);
                if (name.length() == 0) {
                    throw new JSONException("illegal getter");
                }
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
            } else if (name.startsWith("hashCode")) {
                return this.hashCode();
            } else if (name.startsWith("toString")) {
                return this.toString();
            } else {
                throw new JSONException("illegal getter");
            }
        }

        Object value = map.get(name);
        return TypeUtils.cast(value, method.getGenericReturnType(), ParserConfig.getGlobalInstance());
    }

    throw new UnsupportedOperationException(method.toGenericString());
}

public Map<String, Object> getInnerMap() {
    return this.map;
}



private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    SecureObjectInputStream.ensureFields();
    if (SecureObjectInputStream.fields != null && !SecureObjectInputStream.fields_error) {
        ObjectInputStream secIn = new SecureObjectInputStream(in);
        try {
            secIn.defaultReadObject();
            return;
        } catch (java.io.NotActiveException e) {
            // skip
        }
    }

    in.defaultReadObject();
    for (Entry entry : map.entrySet()) {
        final Object key = entry.getKey();
        if (key != null) {
            ParserConfig.global.checkAutoType(key.getClass());
        }

        final Object value = entry.getValue();
        if (value != null) {
            ParserConfig.global.checkAutoType(value.getClass());
        }
    }
}

static class SecureObjectInputStream extends ObjectInputStream {
    static Field[] fields;
    static volatile boolean fields_error;

    static void ensureFields() {
        if (fields == null && !fields_error) {
            try {
                final Field[] declaredFields = ObjectInputStream.class.getDeclaredFields();
                String[] fieldnames = new String[]{"bin", "passHandle", "handles", "curContext"};
                Field[] array = new Field[fieldnames.length];
                for (int i = 0; i < fieldnames.length; i++) {
                    Field field = TypeUtils
                            .getField(ObjectInputStream.class
                                    , fieldnames[i]
                                    , declaredFields
                            );
                    field.setAccessible(true);
                    array[i] = field;
                }
                fields = array;
            } catch (Throwable error) {
                fields_error = true;
            }
        }
    }

    public SecureObjectInputStream(ObjectInputStream in) throws IOException {
        super(in);
        try {
            for (int i = 0; i < fields.length; i++) {
                final Field field = fields[i];
                final Object value = field.get(in);
                field.set(this, value);
            }
        } catch (IllegalAccessException e) {
            fields_error = true;
        }
    }

    protected Class<?> resolveClass(ObjectStreamClass desc)
            throws IOException, ClassNotFoundException {
        String name = desc.getName();
        if (name.length() > 2) {
            int index = name.lastIndexOf('[');
            if (index != -1) {
                name = name.substring(index + 1);
            }
            if (name.length() > 2 && name.charAt(0) == 'L' && name.charAt(name.length() - 1) == ';') {
                name = name.substring(1, name.length() - 1);
            }
            ParserConfig.global.checkAutoType(name, null, Feature.SupportAutoType.mask);
        }
        return super.resolveClass(desc);
    }

    protected Class<?> resolveProxyClass(String[] interfaces)
            throws IOException, ClassNotFoundException {
        for (String interfacename : interfaces) {
            //檢查是否處于黑名單
            ParserConfig.global.checkAutoType(interfacename, null);
        }
        return super.resolveProxyClass(interfaces);
    }

    //Hack:默認(rèn)構(gòu)造方法會(huì)調(diào)用這個(gè)方法,重寫此方法使用反射還原部分關(guān)鍵屬性
    protected void readStreamHeader() throws IOException, StreamCorruptedException {

    }
}

public <T> T toJavaObject(Class<T> clazz) {
    if (clazz == Map.class || clazz == JSONObject.class || clazz == JSON.class) {
        return (T) this;
    }

    if (clazz == Object.class && !containsKey(JSON.DEFAULT_TYPE_KEY)) {
        return (T) this;
    }

    return TypeUtils.castToJavaBean(this, clazz, ParserConfig.getGlobalInstance());
}

public <T> T toJavaObject(Class<T> clazz, ParserConfig config, int features) {
    if (clazz == Map.class) {
        return (T) this;
    }

    if (clazz == Object.class && !containsKey(JSON.DEFAULT_TYPE_KEY)) {
        return (T) this;
    }

    return TypeUtils.castToJavaBean(this, clazz, config);
}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末钙勃,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異师倔,居然都是意外死亡悔叽,警方通過查閱死者的電腦和手機(jī)莱衩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來娇澎,“玉大人笨蚁,你說我怎么就攤上這事√俗” “怎么了括细?”我有些...
    開封第一講書人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)戚啥。 經(jīng)常有香客問我奋单,道長(zhǎng),這世上最難降的妖魔是什么猫十? 我笑而不...
    開封第一講書人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任览濒,我火速辦了婚禮,結(jié)果婚禮上拖云,老公的妹妹穿的比我還像新娘贷笛。我一直安慰自己,他們只是感情好宙项,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開白布乏苦。 她就那樣靜靜地躺著,像睡著了一般杉允。 火紅的嫁衣襯著肌膚如雪邑贴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,784評(píng)論 1 290
  • 那天叔磷,我揣著相機(jī)與錄音拢驾,去河邊找鬼。 笑死改基,一個(gè)胖子當(dāng)著我的面吹牛繁疤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播秕狰,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼稠腊,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了鸣哀?” 一聲冷哼從身側(cè)響起架忌,我...
    開封第一講書人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎我衬,沒想到半個(gè)月后叹放,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體饰恕,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年井仰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了埋嵌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡俱恶,死狀恐怖雹嗦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情合是,我是刑警寧澤了罪,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站聪全,受9級(jí)特大地震影響捶惜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜荔烧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望汽久。 院中可真熱鬧鹤竭,春花似錦、人聲如沸景醇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)三痰。三九已至吧寺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間散劫,已是汗流浹背稚机。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留获搏,地道東北人赖条。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像常熙,于是被迫代替她去往敵國(guó)和親纬乍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348