杰克逊反序列化使用与JSON不同的类名

时间:2018-11-16 12:27:15

标签: java json jackson annotations

我正在尝试使用Jackson来反序列化JSON文件,并且我想对对象使用不同的名称。我知道如何设置@JsonProperty批注,但这不适用于类名。一个例子:

public class _my_class {

   @JsonProperty("my_variable")
   private String myVariable;
}

我希望将该类命名为MyClass。我也尝试使用@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "MyClass"),但是它也不起作用。有解决办法吗?

编辑

这是我的简化JSON文件:

{
  "CVE_data_type": "CVE",
  "CVE_data_format": "MITRE",
  "CVE_data_version": "4.0",
  "CVE_data_numberOfCVEs": "1",
  "CVE_data_timestamp": "2018-10-26T07:00Z",
  "CVE_Items": [
    {
      "cve": {
        "data_type": "CVE",
        "data_format": "MITRE",
        "data_version": "4.0",
        "CVE_data_meta": {
          "ID": "CVE-2018-0001",
          "ASSIGNER": "my@mail.com"
        },
        "affects": {
          "vendor": {
            "vendor_data": [
              {
                "vendor_name": "myVendorName",
                "product": {
                  "product_data": [
                    {
                      "product_name": "myProductName",
                      "version": {
                        "version_data": [
                          {
                            "version_value": "myVersionValue",
                            "version_affected": "myVersionAffected"
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        "problemtype": {
          "problemtype_data": [
            {
              "description": [
                {
                  "lang": "en",
                  "value": "myProblemtypeDescription"
                }
              ]
            }
          ]
        },
        "references": {
          "reference_data": [
            {
              "url": "http://www.myReferenceDataUrl.com/",
              "name": "myReferenceDataName",
              "refsource": "myReferenceDataRefsource",
              "tags": [
                "myReferenceDataTagOne",
                "myReferenceDataTagTwo"
              ]
            }
          ]
        },
        "description": {
          "description_data": [
            {
              "lang": "en",
              "value": "myDescription"
            }
          ]
        }
      },
      "configurations": {
        "CVE_data_version": "4.0",
        "nodes": [
          {
            "operator": "OR",
            "cpe": [
              {
                "vulnerable": true,
                "cpe22Uri": "cpe:/o:this:is:a:cpe",
                "cpe23Uri": "cpe:2.3:o:this:is:a:cpe:*:*:*:*:*:*",
                "versionStartIncluding": "myVersionStartIncluding",
                "versionStartExcluding": "myVersionStartExcluding",
                "versionEndIncluding": "myVersionEndIncluding",
                "versionEndExcluding": "myVersionEndExcluding"
              },
              {
                "vulnerable": true,
                "cpe22Uri": "cpe:/o:this:is:another:cpe",
                "cpe23Uri": "cpe:2.3:o:this:is:another:cpe:*:*:*:*:*:*"
              }
            ]
          }
        ]
      },
      "impact": {
        "baseMetricV3": {
          "cvssV3": {
            "version": "3.0",
            "vectorString": "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
            "attackVector": "NETWORK",
            "attackComplexity": "LOW",
            "privilegesRequired": "NONE",
            "userInteraction": "NONE",
            "scope": "UNCHANGED",
            "confidentialityImpact": "HIGH",
            "integrityImpact": "HIGH",
            "availabilityImpact": "HIGH",
            "baseScore": 9.8,
            "baseSeverity": "CRITICAL"
          },
          "exploitabilityScore": 3.9,
          "impactScore": 5.9
        },
        "baseMetricV2": {
          "cvssV2": {
            "version": "2.0",
            "vectorString": "(AV:N/AC:L/Au:N/C:P/I:P/A:P)",
            "accessVector": "NETWORK",
            "accessComplexity": "LOW",
            "authentication": "NONE",
            "confidentialityImpact": "PARTIAL",
            "integrityImpact": "PARTIAL",
            "availabilityImpact": "PARTIAL",
            "baseScore": 7.5
          },
          "severity": "HIGH",
          "exploitabilityScore": 10.0,
          "impactScore": 6.4,
          "obtainAllPrivilege": false,
          "obtainUserPrivilege": false,
          "obtainOtherPrivilege": false,
          "userInteractionRequired": false
        }
      },
      "publishedDate": "2018-01-10T22:29Z",
      "lastModifiedDate": "2018-02-23T02:29Z"
    }
  ]
}

现在想要这样的CVE元数据的相应类:

public class CVEDataMeta /* currently it's CVE_Data_Meta */ {

    private String id;

    private String assigner;

    // getter and setters
}

编辑2

这就是我读取json文件的方式:

public CVE_Data deserialize(InputStream jsonStream) {
        CVE_Data cveData = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            cveData = mapper.readValue(jsonStream, CVE_Data.class);
        } catch (...) {
            ...
        }
        return cveData;
    }

0 个答案:

没有答案