我有一个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
}
我希望字段previousEmployeeid
为null
,但为它分配了值123
。
答案 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
是原始类型。而且包装对象也不会为空。
将value
与JsonCreator
一起使用,就不能拥有JsonUnwrapped
null
我总是测试我的代码。可以。