GSON - 序列化和反序列化枚举

x33g5p2x  于2022-10-07 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(852)

在这篇文章中,我们将向您展示如何将枚举类型序列化和反序列化为JSON表示。

GSON的Maven依赖性

要在Maven2/3中使用Gson,你可以通过添加以下依赖项来使用Maven Central中的Gson版本。

<dependencies>
    <!--  Gson: Java to Json conversion -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
      <scope>compile</scope>
    </dependency>
</dependencies>

让我们先创建Project POJO类和ProjectStatus枚举,我们将在接下来的例子中使用这个类。

enum ProjectStatus {
 NEW, OPEN, PROGRESS, HOLD, COMPLETED, CLOSED
}

class Project {
    private int projectId;
    private String projectName;
    private String projectDesc;
    private ProjectStatus projectStatus;

    public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
        super();
        this.projectId = projectId;
        this.projectName = projectName;
        this.projectDesc = projectDesc;
        this.projectStatus = projectStatus;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectDesc() {
        return projectDesc;
    }

    public void setProjectDesc(String projectDesc) {
        this.projectDesc = projectDesc;
    }

    public ProjectStatus getProjectStatus() {
        return projectStatus;
    }

    public void setProjectStatus(ProjectStatus projectStatus) {
        this.projectStatus = projectStatus;
    }

    @Override
    public String toString() {
        return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
            ", projectStatus=" + projectStatus + "]";
    }
}

串行化枚举类型

public class GSONEnumExamples {
    public static void main(String[] args) {
        serializeEnumGson();
    }

    public static void serializeEnumGson() {
        Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String result = gson.toJson(project);
        System.out.println(result);
    }
}

输出。

{
  "projectId": 100,
  "projectName": "CMS",
  "projectDesc": "Content Management System",
  "projectStatus": "NEW"
}

反序列化枚举类型

public class GSONEnumExamples {
    public static void main(String[] args) {
        deserializeEnumGson();
    }

    public static void deserializeEnumGson() {
        String input = "{\r\n" +
            "  \"projectId\": 100,\r\n" +
            "  \"projectName\": \"CMS\",\r\n" +
            "  \"projectDesc\": \"Content Management System\",\r\n" +
            "  \"projectStatus\": \"NEW\"\r\n" +
            "}";
        Gson gson = new GsonBuilder().create();
        Project project = gson.fromJson(input, Project.class);
        System.out.println(project.toString());
    }
}

输出。

Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]

完整的例子供参考

package net.javaguides.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GSONEnumExamples {
    public static void main(String[] args) {
        serializeEnumGson();
        deserializeEnumGson();
    }

    public static void serializeEnumGson() {
        Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String result = gson.toJson(project);
        System.out.println(result);
    }

    public static void deserializeEnumGson() {
        String input = "{\r\n" +
            "  \"projectId\": 100,\r\n" +
            "  \"projectName\": \"CMS\",\r\n" +
            "  \"projectDesc\": \"Content Management System\",\r\n" +
            "  \"projectStatus\": \"NEW\"\r\n" +
            "}";
        Gson gson = new GsonBuilder().create();
        Project project = gson.fromJson(input, Project.class);
        System.out.println(project.toString());
    }
}

enum ProjectStatus {
    NEW,
    OPEN,
    PROGRESS,
    HOLD,
    COMPLETED,
    CLOSED
}

class Project {
    private int projectId;
    private String projectName;
    private String projectDesc;
    private ProjectStatus projectStatus;

    public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
        super();
        this.projectId = projectId;
        this.projectName = projectName;
        this.projectDesc = projectDesc;
        this.projectStatus = projectStatus;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectDesc() {
        return projectDesc;
    }

    public void setProjectDesc(String projectDesc) {
        this.projectDesc = projectDesc;
    }

    public ProjectStatus getProjectStatus() {
        return projectStatus;
    }

    public void setProjectStatus(ProjectStatus projectStatus) {
        this.projectStatus = projectStatus;
    }

    @Override
    public String toString() {
        return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
            ", projectStatus=" + projectStatus + "]";
    }
}

输出。

{
  "projectId": 100,
  "projectName": "CMS",
  "projectDesc": "Content Management System",
  "projectStatus": "NEW"
}
Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]

使用@SerializedName注解对枚举进行序列化和反序列化

默认情况下,Gson用**小写的名字来序列化枚举。如果这还不够,你想要一些其他的字符串或整数值,那么可以看看@SerializedName注解。Gson可以使用@SerializedName注解对枚举进行序列化和反序列化。如果我们用@SerializedName注解一个枚举,我们可以提供一个值,当GSON将JSON序列化或反序列化为Java对象时,这个值将被映射。

让我们从一个例子开始。在下面的类中,我们用@SerializedName注释了状态枚举。我们可以用一个值来映射这些枚举,这个值在序列化时被使用。当反序列化时,如果指定了这个值,它将使用正确的枚举。如果没有找到这个值,它将返回 "null"。
让我们给ProjectStatus枚举添加一个@SerializedName注解,如下所示。

enum ProjectStatus {
    @SerializedName("1")
    NEW, @SerializedName("2")
    OPEN, @SerializedName("3")
    PROGRESS, @SerializedName("4")
    HOLD, @SerializedName("5")
    COMPLETED, @SerializedName("6")
    CLOSED
}

让我们写一个完整的例子,使用@SerializedName注解对枚举进行序列化和反序列化。

package net.javaguides.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

public class GSONEnumExamples {
    public static void main(String[] args) {
        serializeEnumGson();
        deserializeEnumGson();
    }

    public static void serializeEnumGson() {
        Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String result = gson.toJson(project);
        System.out.println(result);
    }

    public static void deserializeEnumGson() {
        String input = "{\r\n" + "  \"projectId\": 100,\r\n" + "  \"projectName\": \"CMS\",\r\n" +
            "  \"projectDesc\": \"Content Management System\",\r\n" + "  \"projectStatus\": \"1\"\r\n" + "}";
        Gson gson = new GsonBuilder().create();
        Project project = gson.fromJson(input, Project.class);
        System.out.println(project.toString());
    }
}

enum ProjectStatus {
    @SerializedName("1")
    NEW, @SerializedName("2")
    OPEN, @SerializedName("3")
    PROGRESS, @SerializedName("4")
    HOLD, @SerializedName("5")
    COMPLETED, @SerializedName("6")
    CLOSED
}

class Project {
    private int projectId;
    private String projectName;
    private String projectDesc;
    private ProjectStatus projectStatus;

    public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
        super();
        this.projectId = projectId;
        this.projectName = projectName;
        this.projectDesc = projectDesc;
        this.projectStatus = projectStatus;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectDesc() {
        return projectDesc;
    }

    public void setProjectDesc(String projectDesc) {
        this.projectDesc = projectDesc;
    }

    public ProjectStatus getProjectStatus() {
        return projectStatus;
    }

    public void setProjectStatus(ProjectStatus projectStatus) {
        this.projectStatus = projectStatus;
    }

    @Override
    public String toString() {
        return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
            ", projectStatus=" + projectStatus + "]";
    }
}

输出:

{
  "projectId": 100,
  "projectName": "CMS",
  "projectDesc": "Content Management System",
  "projectStatus": "1"
}
Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]

相关文章