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

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

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

GSON的Maven依赖性

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

  1. <dependencies>
  2. <!-- Gson: Java to Json conversion -->
  3. <dependency>
  4. <groupId>com.google.code.gson</groupId>
  5. <artifactId>gson</artifactId>
  6. <version>2.8.5</version>
  7. <scope>compile</scope>
  8. </dependency>
  9. </dependencies>

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

  1. enum ProjectStatus {
  2. NEW, OPEN, PROGRESS, HOLD, COMPLETED, CLOSED
  3. }
  4. class Project {
  5. private int projectId;
  6. private String projectName;
  7. private String projectDesc;
  8. private ProjectStatus projectStatus;
  9. public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
  10. super();
  11. this.projectId = projectId;
  12. this.projectName = projectName;
  13. this.projectDesc = projectDesc;
  14. this.projectStatus = projectStatus;
  15. }
  16. public int getProjectId() {
  17. return projectId;
  18. }
  19. public void setProjectId(int projectId) {
  20. this.projectId = projectId;
  21. }
  22. public String getProjectName() {
  23. return projectName;
  24. }
  25. public void setProjectName(String projectName) {
  26. this.projectName = projectName;
  27. }
  28. public String getProjectDesc() {
  29. return projectDesc;
  30. }
  31. public void setProjectDesc(String projectDesc) {
  32. this.projectDesc = projectDesc;
  33. }
  34. public ProjectStatus getProjectStatus() {
  35. return projectStatus;
  36. }
  37. public void setProjectStatus(ProjectStatus projectStatus) {
  38. this.projectStatus = projectStatus;
  39. }
  40. @Override
  41. public String toString() {
  42. return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
  43. ", projectStatus=" + projectStatus + "]";
  44. }
  45. }

串行化枚举类型

  1. public class GSONEnumExamples {
  2. public static void main(String[] args) {
  3. serializeEnumGson();
  4. }
  5. public static void serializeEnumGson() {
  6. Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
  7. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  8. String result = gson.toJson(project);
  9. System.out.println(result);
  10. }
  11. }

输出。

  1. {
  2. "projectId": 100,
  3. "projectName": "CMS",
  4. "projectDesc": "Content Management System",
  5. "projectStatus": "NEW"
  6. }

反序列化枚举类型

  1. public class GSONEnumExamples {
  2. public static void main(String[] args) {
  3. deserializeEnumGson();
  4. }
  5. public static void deserializeEnumGson() {
  6. String input = "{\r\n" +
  7. " \"projectId\": 100,\r\n" +
  8. " \"projectName\": \"CMS\",\r\n" +
  9. " \"projectDesc\": \"Content Management System\",\r\n" +
  10. " \"projectStatus\": \"NEW\"\r\n" +
  11. "}";
  12. Gson gson = new GsonBuilder().create();
  13. Project project = gson.fromJson(input, Project.class);
  14. System.out.println(project.toString());
  15. }
  16. }

输出。

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

完整的例子供参考

  1. package net.javaguides.gson;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. public class GSONEnumExamples {
  5. public static void main(String[] args) {
  6. serializeEnumGson();
  7. deserializeEnumGson();
  8. }
  9. public static void serializeEnumGson() {
  10. Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
  11. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  12. String result = gson.toJson(project);
  13. System.out.println(result);
  14. }
  15. public static void deserializeEnumGson() {
  16. String input = "{\r\n" +
  17. " \"projectId\": 100,\r\n" +
  18. " \"projectName\": \"CMS\",\r\n" +
  19. " \"projectDesc\": \"Content Management System\",\r\n" +
  20. " \"projectStatus\": \"NEW\"\r\n" +
  21. "}";
  22. Gson gson = new GsonBuilder().create();
  23. Project project = gson.fromJson(input, Project.class);
  24. System.out.println(project.toString());
  25. }
  26. }
  27. enum ProjectStatus {
  28. NEW,
  29. OPEN,
  30. PROGRESS,
  31. HOLD,
  32. COMPLETED,
  33. CLOSED
  34. }
  35. class Project {
  36. private int projectId;
  37. private String projectName;
  38. private String projectDesc;
  39. private ProjectStatus projectStatus;
  40. public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
  41. super();
  42. this.projectId = projectId;
  43. this.projectName = projectName;
  44. this.projectDesc = projectDesc;
  45. this.projectStatus = projectStatus;
  46. }
  47. public int getProjectId() {
  48. return projectId;
  49. }
  50. public void setProjectId(int projectId) {
  51. this.projectId = projectId;
  52. }
  53. public String getProjectName() {
  54. return projectName;
  55. }
  56. public void setProjectName(String projectName) {
  57. this.projectName = projectName;
  58. }
  59. public String getProjectDesc() {
  60. return projectDesc;
  61. }
  62. public void setProjectDesc(String projectDesc) {
  63. this.projectDesc = projectDesc;
  64. }
  65. public ProjectStatus getProjectStatus() {
  66. return projectStatus;
  67. }
  68. public void setProjectStatus(ProjectStatus projectStatus) {
  69. this.projectStatus = projectStatus;
  70. }
  71. @Override
  72. public String toString() {
  73. return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
  74. ", projectStatus=" + projectStatus + "]";
  75. }
  76. }

输出。

  1. {
  2. "projectId": 100,
  3. "projectName": "CMS",
  4. "projectDesc": "Content Management System",
  5. "projectStatus": "NEW"
  6. }
  7. Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]

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

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

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

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

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

  1. package net.javaguides.gson;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.annotations.SerializedName;
  5. public class GSONEnumExamples {
  6. public static void main(String[] args) {
  7. serializeEnumGson();
  8. deserializeEnumGson();
  9. }
  10. public static void serializeEnumGson() {
  11. Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
  12. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  13. String result = gson.toJson(project);
  14. System.out.println(result);
  15. }
  16. public static void deserializeEnumGson() {
  17. String input = "{\r\n" + " \"projectId\": 100,\r\n" + " \"projectName\": \"CMS\",\r\n" +
  18. " \"projectDesc\": \"Content Management System\",\r\n" + " \"projectStatus\": \"1\"\r\n" + "}";
  19. Gson gson = new GsonBuilder().create();
  20. Project project = gson.fromJson(input, Project.class);
  21. System.out.println(project.toString());
  22. }
  23. }
  24. enum ProjectStatus {
  25. @SerializedName("1")
  26. NEW, @SerializedName("2")
  27. OPEN, @SerializedName("3")
  28. PROGRESS, @SerializedName("4")
  29. HOLD, @SerializedName("5")
  30. COMPLETED, @SerializedName("6")
  31. CLOSED
  32. }
  33. class Project {
  34. private int projectId;
  35. private String projectName;
  36. private String projectDesc;
  37. private ProjectStatus projectStatus;
  38. public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
  39. super();
  40. this.projectId = projectId;
  41. this.projectName = projectName;
  42. this.projectDesc = projectDesc;
  43. this.projectStatus = projectStatus;
  44. }
  45. public int getProjectId() {
  46. return projectId;
  47. }
  48. public void setProjectId(int projectId) {
  49. this.projectId = projectId;
  50. }
  51. public String getProjectName() {
  52. return projectName;
  53. }
  54. public void setProjectName(String projectName) {
  55. this.projectName = projectName;
  56. }
  57. public String getProjectDesc() {
  58. return projectDesc;
  59. }
  60. public void setProjectDesc(String projectDesc) {
  61. this.projectDesc = projectDesc;
  62. }
  63. public ProjectStatus getProjectStatus() {
  64. return projectStatus;
  65. }
  66. public void setProjectStatus(ProjectStatus projectStatus) {
  67. this.projectStatus = projectStatus;
  68. }
  69. @Override
  70. public String toString() {
  71. return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
  72. ", projectStatus=" + projectStatus + "]";
  73. }
  74. }

输出:

  1. {
  2. "projectId": 100,
  3. "projectName": "CMS",
  4. "projectDesc": "Content Management System",
  5. "projectStatus": "1"
  6. }
  7. Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]

相关文章