在杰克逊中反序列化可选的可空字段

时间:2019-02-18 19:01:49

标签: java json jackson

我有一个Employee类,我想像这样序列化:

class Employee {
  String name;
  @JsonUnwrapped
  EmployeeId id; 
  @JsonUnwrapped(prefix = "previousEmployee")
  EmployeeId previousEmployeeId;
}

final class EmployeeId{
 @JsonProperty("id")
 private final long value;

 @JsonCreator
 public EmployeeId(@JsonProperty("id") long value) {
    this.value = value;
 }
}

序列化Employee会给出以下json:

{
  "name":"foo",
  "id" : 123,
  "previousEmployeeid" : 456
}

但是,previousEmployeeId是可选的。因此,如果我尝试反序列化此json,则会将id放入previousEmployeeId字段中。我在做什么错了?

{
  "name":"foo",
  "id" : 123
}

我希望字段previousEmployeeidnull,但为它分配了值123

1 个答案:

答案 0 :(得分:0)

等等!

private void btnExport_Click(object sender, EventArgs e)
    {
        int i = 1,j=0,k=1;

        using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xlsx", ValidateNames = true }) 

        {
            if (sfd.ShowDialog() == DialogResult.OK) 
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

                /*Workbook wb = app.Workbooks.Add(XlSheetType.xlWorksheet);
                Worksheet ws = (Worksheet)app.ActiveSheet;*/
                app.Visible = true;
                Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet ws = ((Microsoft.Office.Interop.Excel.Worksheet)app.ActiveSheet);


                // inserting colums names into cells

                foreach (ColumnHeader header in lst.Columns)
                {
                    ws.Cells[1, i] = header.Text;
                    i++;


                }
                i = 2;

                foreach (ListViewItem item in lst.Items) 
                {
                    j = 1;
                    foreach (ListViewItem.ListViewSubItem lvs in item.SubItems)
                    {




                             ws.Cells[i, j] = lvs.Text;
                             j++;



                    }

                        i++;  

                }
               /* wb.SaveAs(sfd.FileName, XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange,
                    XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);*/
                wb.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);
                wb.Close(false, Type.Missing, Type.Missing);
                app.Quit();
                app.Quit();
                   MessageBox.Show("Your Data has been Exported Successfully","Message", MessageBoxButtons.OK, MessageBoxIcon.Information);



            }

        }
    }

final class EmployeeId { @JsonProperty("id") private final long value; @JsonCreator public EmployeeId(@JsonProperty("whatever") long value) { this.value = value; } } 作为whatever的值。您可以输入不为空为空,但不能@JsonProperty为空的内容。
顺便说一句,这一定是某种错误。


请记住,它不能是id,因为内部null是原始类型。而且包装对象也不会为空。


valueJsonCreator一起使用,就不能拥有JsonUnwrapped null

enter image description here


我总是测试我的代码。可以。

enter image description here