Java中有哪些序列化格式

发布时间:2021-07-01 15:19:45 作者:Leah
来源:亿速云 阅读:397

Java中有哪些序列化格式

在Java编程中,序列化是将对象转换为字节流的过程,以便可以将其存储在文件中、通过网络传输或在内存中持久化。反序列化则是将字节流转换回对象的过程。Java提供了多种序列化格式,每种格式都有其特定的用途和优缺点。本文将详细介绍Java中常见的序列化格式,包括Java原生序列化、JSON、XML、Protocol Buffers、Avro、Thrift等。

1. Java原生序列化

Java原生序列化是Java语言内置的序列化机制,通过实现java.io.Serializable接口,可以将对象转换为字节流。Java原生序列化的主要优点是简单易用,但它的性能和兼容性较差。

1.1 使用方法

import java.io.*;

class Person implements Serializable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + "}";
    }
}

public class JavaSerializationExample {
    public static void main(String[] args) {
        Person person = new Person("John", 30);

        // 序列化
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反序列化
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {
            Person deserializedPerson = (Person) ois.readObject();
            System.out.println(deserializedPerson);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

1.2 优缺点

2. JSON序列化

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于Web应用和API中。Java中有多个库支持JSON序列化,如Jackson、Gson等。

2.1 使用方法(以Jackson为例)

import com.fasterxml.jackson.databind.ObjectMapper;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + "}";
    }
}

public class JsonSerializationExample {
    public static void main(String[] args) {
        Person person = new Person("John", 30);

        // 序列化
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(person);
            System.out.println("Serialized JSON: " + json);

            // 反序列化
            Person deserializedPerson = mapper.readValue(json, Person.class);
            System.out.println("Deserialized Person: " + deserializedPerson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.2 优缺点

3. XML序列化

XML(eXtensible Markup Language)是一种标记语言,广泛用于数据存储和传输。Java中有多个库支持XML序列化,如JAXB、XStream等。

3.1 使用方法(以JAXB为例)

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

@XmlRootElement
class Person {
    private String name;
    private int age;

    public Person() {}

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + "}";
    }
}

public class XmlSerializationExample {
    public static void main(String[] args) {
        Person person = new Person("John", 30);

        // 序列化
        try {
            JAXBContext context = JAXBContext.newInstance(Person.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            StringWriter writer = new StringWriter();
            marshaller.marshal(person, writer);
            String xml = writer.toString();
            System.out.println("Serialized XML: " + xml);

            // 反序列化
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Person deserializedPerson = (Person) unmarshaller.unmarshal(new StringReader(xml));
            System.out.println("Deserialized Person: " + deserializedPerson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.2 优缺点

4. Protocol Buffers

Protocol Buffers(简称Protobuf)是Google开发的一种高效的数据序列化格式,主要用于数据存储和通信协议。Protobuf使用二进制格式,序列化后的数据较小,性能较高。

4.1 使用方法

首先需要定义.proto文件:

syntax = "proto3";

message Person {
    string name = 1;
    int32 age = 2;
}

然后使用protoc编译器生成Java类:

protoc --java_out=. person.proto

最后在Java代码中使用生成的类进行序列化和反序列化:

import com.example.PersonProto.Person;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ProtobufSerializationExample {
    public static void main(String[] args) {
        Person person = Person.newBuilder()
                .setName("John")
                .setAge(30)
                .build();

        // 序列化
        try (FileOutputStream output = new FileOutputStream("person.bin")) {
            person.writeTo(output);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 反序列化
        try (FileInputStream input = new FileInputStream("person.bin")) {
            Person deserializedPerson = Person.parseFrom(input);
            System.out.println("Deserialized Person: " + deserializedPerson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4.2 优缺点

5. Avro

Avro是Apache开发的一种数据序列化系统,支持二进制和JSON格式。Avro使用Schema定义数据结构,支持动态类型和复杂数据结构。

5.1 使用方法

首先定义Avro Schema:

{
  "type": "record",
  "name": "Person",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "age", "type": "int"}
  ]
}

然后使用Avro工具生成Java类:

java -jar avro-tools-1.10.2.jar compile schema person.avsc .

最后在Java代码中使用生成的类进行序列化和反序列化:

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import com.example.Person;

import java.io.File;
import java.io.IOException;

public class AvroSerializationExample {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("John");
        person.setAge(30);

        // 序列化
        try (DataFileWriter<Person> writer = new DataFileWriter<>(new SpecificDatumWriter<>(Person.class))) {
            writer.create(person.getSchema(), new File("person.avro"));
            writer.append(person);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反序列化
        try (DataFileReader<Person> reader = new DataFileReader<>(new File("person.avro"), new SpecificDatumReader<>(Person.class))) {
            Person deserializedPerson = reader.next();
            System.out.println("Deserialized Person: " + deserializedPerson);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5.2 优缺点

6. Thrift

Thrift是Apache开发的一种跨语言的服务开发框架,支持多种编程语言。Thrift使用IDL(Interface Definition Language)定义数据结构和服务接口,支持二进制和JSON格式。

6.1 使用方法

首先定义Thrift IDL文件:

struct Person {
    1: string name,
    2: i32 age
}

然后使用Thrift编译器生成Java类:

thrift --gen java person.thrift

最后在Java代码中使用生成的类进行序列化和反序列化:

import org.apache.thrift.TSerializer;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.protocol.TBinaryProtocol;
import com.example.Person;

public class ThriftSerializationExample {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("John");
        person.setAge(30);

        // 序列化
        try {
            TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
            byte[] bytes = serializer.serialize(person);
            System.out.println("Serialized bytes: " + bytes.length);

            // 反序列化
            TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
            Person deserializedPerson = new Person();
            deserializer.deserialize(deserializedPerson, bytes);
            System.out.println("Deserialized Person: " + deserializedPerson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

6.2 优缺点

7. 总结

Java中有多种序列化格式可供选择,每种格式都有其特定的用途和优缺点。Java原生序列化简单易用,但性能和兼容性较差;JSON和XML适合跨平台和跨语言的数据交换,但序列化后的数据较大;Protocol Buffers、Avro和Thrift适合高性能和高压缩比的场景,但使用较为复杂。开发者应根据具体需求选择合适的序列化格式。

推荐阅读:
  1. java如何实现序列化
  2. java怎么实现序列化

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:计算机中iges是什么文件格式

下一篇:Java中怎么实现数组冒泡与选择排序改版

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》