杰克逊:将非初始化的集合字段序列化为空(Jackson: serialize non-initialized collection field as empty)
我有一个带有字段或属性的POJO,包含对象集合,如下所示:
public class Box { public List<Items> items; }
默认情况下,
items
值为null, 我不想用空列表初始化它 。现在,如果我尝试用Jackson序列化它,我会得到
NullPointerException
。 有没有一种简单的方法可以让杰克逊不打破这种价值并将其序列化为空集合:[ ]
?注意。 这个类只是一个简化的例子。 实际上,每个类中都有一百个类和许多具有不同名称的字段,有时在代码中的某处设置为
null
,从而破坏运行时的序列化。I have a POJO with a field or property, containing collection of objects, something like this:
public class Box { public List<Items> items; }
By default, value of
items
is null, and I do not want to initialize it with empty list.Now, if I try to serialize it with Jackson, I get
NullPointerException
. Is there a simple way to make Jackson not break on such value and serialize it as an empty collection:[ ]
?Note. This class is just a simplified example. In reality, there are a hundred of classes and a number of fields with different names in each of them, which are occasionally set to
null
sometimes somewhere in the code, breaking serialization in runtime.
原文:https://stackoverflow.com/questions/35432133
最满意答案
问题是由数据框的stringsAsFactors参数默认为TRUE。 首次声明数据帧时,将此参数设置为FALSE,并且rbind()函数将无错误地执行。
#Declare some initial vectors: students = c("Mary", "Jane", "Eva","Leon") testScores = c(80, 90, 70, 95) birthDates = c("1990-08-01","1995-07-23","1993-12-13","1990-08-09")
声明您的数据框,并将stringsAsFactors参数设置为FALSE:
df1 <- data.frame (students ,testScores ,birthDates , stringsAsFactors = FALSE) df1 <- rbind(df1, c("Mike", 87, "2014-02-23"))
现在新行应该附加到数据框df1:
DF1 [5,1]
结果是:
students test1 birth 5 Mike 87 2014-02-23
The issue results from the data frame's stringsAsFactors parameter being TRUE by default. When you first declare your data frame, set this parameter to FALSE, and the rbind() function will execute without error.
#Declare some initial vectors: students = c("Mary", "Jane", "Eva","Leon") testScores = c(80, 90, 70, 95) birthDates = c("1990-08-01","1995-07-23","1993-12-13","1990-08-09")
Declare your data frame, and set the stringsAsFactors parameter to FALSE:
df1 <- data.frame (students ,testScores ,birthDates , stringsAsFactors = FALSE) df1 <- rbind(df1, c("Mike", 87, "2014-02-23"))
The new row should now be attached to the data frame df1:
df1[5,]
results in:
students test1 birth 5 Mike 87 2014-02-23
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
问题是由数据框的stringsAsFactors参数默认为TRUE。 首次声明数据帧时,将此参数设置为FALSE,并且rbind()函数将无错误地执行。 #Declare some initial vectors: students = c("Mary", "Jane", "Eva","Leon") testScores = c(80, 90, 70, 95) birthDates = c("1990-08-01","1995-07-23","1993-12-13","1990-08-09") 声明您的数 ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
什么是“标准明确的日期”格式?(What are the “standard unambiguous date” formats for string-to-date conversion in R?)[2022-03-04]
这是记录的行为。 从?as.Date : 格式:一个字符串。 如果没有指定,它将尝试在第一个非"NA“元素上的”%Y-%m-%d“'然后'”%Y /%m /%d“',如果两者都不起作用的话会给出错误。 as.Date("01 Jan 2000")产生错误,因为格式不是上面列出的两个之一。 as.Date("01/01/2000")产生错误的答案,因为日期不是上面列出的两种格式之一。 我认为“标准明确”是指“ISO-8601”(即使as.Date不是那么严格,因为“%m /%d /%Y”不是ISO-8601) ... -
对于您正在使用的write函数(例如write.csv ),使用如下的na选项设置NA字符 write.csv(manifest_final, your_output_file, na = '') 您当然可以在write.csv包含您正在使用的任何其他选项。 此选项也应该在其他write.*函数中可用。 EDITS 这个问题 manifest_final$MIN_SAIL_DATE[is.na(manifest_final$MIN_SAIL_DATE)] <- '' 是您尝试将Date向量元素设置为字符 ...
-
我们需要提取data和. 表示数据对象 Z %>% filter(d == 1) %>% lm(y ~ t, data = .) #Call: #lm(formula = y ~ t, data = .) #Coefficients: #(Intercept) t # 2 1 在summary summarise/mutate/group_by和其他tidyverse函数中,我们可以简单地传递列名。 在这里,要么我们需要从 ...
-
使用purrr , magrittr和plyr ld %>% map(~as.matrix(.) %>% t %>% as.data.frame) %>% do.call(plyr::rbind.fill, .) 结果: X10 X11 X13 X14 X12 X15 1 7 1 6 8 NA NA 2 NA 5 8 NA 8 9 using purrr, magrittr and plyr ld %>% map(~as.matrix(.) %>% t % ...
-
也许这样的事情会有所帮助: w <- xts(a[,c(-1,-2)],order.by=as.Date(as.character(a[,2]),"%Y%m%d")) Maybe something like this will help: w <- xts(a[,c(-1,-2)],order.by=as.Date(as.character(a[,2]),"%Y%m%d"))
-
根据vector和rbind解析多个XML文件(parse multiple XML files based on a vector and rbind in a dataframe)[2021-03-04]
你的主要问题是使用sapply不是lapply() ,后者返回一个列表,以前尝试简化为一个向量或矩阵,这里是一个矩阵。 major_test <- lapply(xml_url_test, XML_parser) 当然, sapply是sapply的包装器,也可以返回一个列表: sapply(..., simplify=FALSE) : major_test <- sapply(xml_url_test, XML_parser, simplify=FALSE) 然而,其他一些项目出现了: 在开始时,您并 ... -
SAS特定的观察格式(SAS specific observation format)[2022-10-18]
我假设'8个数字'你的意思是8个数字。 对于8个单独的数字,这将是不同的。 所以像下面的代码可能会有所帮助。 修饰符'kd'意味着COMPRESS函数中的KEEP DIGITS在这里具有魔力: data indata; length TEXT $20; input TEXT; datalines; a 123 12345678 A12345678 ; run; data outdata; set indata; length TEXT_DIGITS $20 _8_DIGIT_INDICATOR 3; TE ...