2015年11月14日 星期六

客製化型態轉換(兩種JSON) (Hibernate3.x 二十)

不會用的可先參考這裡的文章

先寫個JSON和List的互轉程式

pom.xml

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
</dependency>
    
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>

※java版本太高(7、8)要降版
※org.json選較低版本即可, net.sf要加一行classifier


org.json

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
    
public class OrgJSONTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
    
        JSONObject jObj = new JSONObject();
        jObj.put("interest", list);
        System.out.println(jObj);// {"interest":["A","B","C"]}
    
        JSONArray jArray = jObj.getJSONArray("interest");// get()也可以
        List<String> rtn = new ArrayList<>();
        for (int i = 0; i < jArray.length(); i++) {
            rtn.add(jArray.getString(i));
        }
        System.out.println(rtn);// [A, B, C]
    }
}


net.sf.json

import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
    
public class NetSfJSONTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
    
        JSONObject jObj = new JSONObject();
        jObj.put("interest", JSONArray.fromObject(list));
        System.out.println(jObj);// {"interest":["A","B","C"]}
    
        JSONArray jArray = jObj.getJSONArray("interest");// get()也可以
        List<String> rtn = new ArrayList<>();
        for (int i = 0; i < jArray.size(); i++) {
            rtn.add(jArray.getString(i));
        }
        System.out.println(rtn);// [A, B, C]
    }
}





依照客製化型態轉換 (Hibernate3.x 十九)來改
主要改UserType裡面的兩個方法,assemble和disassemble

org.json

/**
 * JSON轉List
 */
@Override
public Serializable disassemble(Object value) throws HibernateException {
    JSONObject jObj = new JSONObject(value);
    JSONArray jArray = jObj.getJSONArray("interest");// get()也可以
    List<String> rtn = new ArrayList<>();
    for (int i = 0; i < jArray.length(); i++) {
        rtn.add(jArray.getString(i));
    }
    return (Serializable) rtn;
}
    
/**
 * List轉JSON
 */
@SuppressWarnings("unchecked")
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
    JSONObject jObj = new JSONObject();
    jObj.put("interest", ((List<String>) owner));
    return jObj;
}


net.sf.json

/**
 * JSON轉List
 */
@Override
public Serializable disassemble(Object value) throws HibernateException {
    JSONObject jObj = JSONObject.fromObject(value) ;
    JSONArray jArray = jObj.getJSONArray("interest");// get()也可以
    List<String> rtn = new ArrayList<>();
    for (int i = 0; i < jArray.size(); i++) {
        rtn.add(jArray.getString(i));
    }
    return (Serializable) rtn;
}
    
/**
 * List轉JSON
 */
@SuppressWarnings("unchecked")
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
    JSONObject jObj = new JSONObject();
    jObj.put("interest", JSONArray.fromObject((List<String>) owner));
    return jObj;
}

測試類和設定都和客製化型態轉換 (Hibernate3.x 十九)一樣

沒有留言:

張貼留言