java对象序列化与反序列化的默认格式和json格式使用示例
默认格式
public class MyClass implements Serializable{
...}
序列化:
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(outputPath));
output.writeObject(myObject);
反序列化:
ObjectInputStream input = new ObjectInputStream(new FileInputStream(inputPath));
return (MyClass)input.readObject();
JSON格式
使用jackson包。jackson是一个效率非常高的Java JSON包。文档和下载见官网。
序列化
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File(outputPath), myObject);
反序列化:
return mapper.readValue(new File(outputPath), MyClass.class);
完整测试代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;public class Zoo implements Serializable {
private static final long serialVersionUID = 1L;
private static ObjectMapper mapper = new ObjectMapper();public static int maxAnimalCount;
public ArrayList<String> animals;public Zoo() {
animals = new ArrayList<String>();
}
public static void setMax(int max){
maxAnimalCount = max;
}/**
* Add an animal to animals Array.
* @param animalName
*/
public void addAnimal(String animalName){
if (animals.size() < maxAnimalCount)
animals.add(animalName);
}@Override
public String toString(){
return "Zoo: \n animals: " + animals.toString() +
"\n maxAnimalCount: " + maxAnimalCount + "\n";
}/**
* Output standard serialization to file at logPath.
* @param logPath
*/
public void serializeToLog(String logPath) {
ObjectOutputStream output = null;
try
{
output = new ObjectOutputStream(
new FileOutputStream(logPath));
output.writeObject(this);
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}/**
* Output JSON serialization(using jackson) to file at logPath.
* @param logPath
*/
public void serializeJSONToLog(String logPath){try {
mapper.writeValue(new File(logPath), this);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}/**
* Standard deserialize a Zoo instance from file at logPath.
* @param logPath
* @return deserialized zoo instance
*/
public static Zoo deserializeFromLog(String logPath) {
ObjectInputStream input = null;
try
{
input =new ObjectInputStream(
new FileInputStream(logPath));
return (Zoo)input.readObject();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}return null;
}/**
* JSON deserialize a Zoo instance from file at logPath.
* @param logPath
* @return JSON deserialized zoo instance
*/
public static Zoo deserializeJSONFromLog(String logPath){
try {
return mapper.readValue(new File(logPath), Zoo.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}return null;
}
}class ZooSerializeTest {
public static void main(String[] args) {
Zoo zoo1 = new Zoo();
Zoo.setMax(100);
zoo1.addAnimal("hamster");
zoo1.addAnimal("sheep");zoo1.serializeToLog("zoo1.log");
Zoo zoo2 = new Zoo();
Zoo.setMax(200);
zoo2.addAnimal("tiger");zoo2.serializeToLog("zoo2.log");
Zoo.setMax(300);
//Deserialization
zoo1 = Zoo.deserializeFromLog("zoo1.log");
zoo2 = Zoo.deserializeFromLog("zoo2.log");System.out.println("zoo1: \n" + zoo1);
System.out.println("zoo2: \n" + zoo2);//Serialize to JSON
zoo1.serializeJSONToLog("zoo1.json");
zoo1 = Zoo.deserializeJSONFromLog("zoo1.json");System.out.println("zoo1 from json: \n" + zoo1);
}
}
您可能感兴趣的文章:
- Java对象的序列化与反序列化:默认格式及JSON格式(使用jackson)
- java中对象和json格式相互序列化和反序列化的函数
- 【Java】使用Json-lib序列化关联对象的异常解决
- 使用jacksonjava对象转换成标准的json格式,便于与easyui...
- 百度天气api开发 全解析xml和json格式 使用json-lib jsonObject、 Gson 方式把json 转换成 java对象
- 使用Struts 2将Java对象序列化成JSON
- java教程之对象序列化使用基础示例详解
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
- 使用Java 语言可以方便的把任意对象转换成json格式的数据
- 【json】使用json和java对象的序列化和反序列化
- JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)
- Java的微信开发中使用XML格式和JSON格式数据的示例
- 使用jackson将java对象转换成标准的json格式,便于与easyui的datagrid交互
- JSON对象反序列化为Java对象的时候自定义Date类型的字符串格式
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- 使用Struts 2将Java对象序列化成JSON
- JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
- JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)
- java对象转化为json格式数据