下面列出了怎么用com.fasterxml.jackson.annotation.JsonSetter的API类实例代码及写法,或者点击链接到github查看源代码。
public void testNullsAsEmptyUsingDefaults() throws Exception
{
final String JSON = aposToQuotes("{'values':[null]}");
TypeReference<NullContentUndefined<List<Integer>>> listType = new TypeReference<NullContentUndefined<List<Integer>>>() { };
// Let's see defaulting in action
ObjectMapper mapper = afterburnerMapperBuilder()
.changeDefaultNullHandling(n -> n.withContentNulls(Nulls.AS_EMPTY))
.build();
NullContentUndefined<List<Integer>> result = mapper.readValue(JSON, listType);
assertEquals(1, result.values.size());
assertEquals(Integer.valueOf(0), result.values.get(0));
// or configured for type:
mapper = afterburnerMapperBuilder()
.withConfigOverride(List.class,
o -> o.setNullHandling(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY)))
.build();
result = mapper.readValue(JSON, listType);
assertEquals(1, result.values.size());
assertEquals(Integer.valueOf(0), result.values.get(0));
}
@JsonSetter("file")
public void setFile(JsonNode jsonNode) {
if (jsonNode.isObject()) {
try {
this.file = new ObjectMapper().treeToValue(jsonNode, File.class);
} catch (JsonProcessingException e) {
logger.error("Error deserializing json: ", e);
}
} else if (jsonNode.isTextual()) {
this.fileId = jsonNode.asText();
}
}
public void testSkipNullWithDefaults() throws Exception
{
String json = aposToQuotes("{'value':null}");
StringValue result = MAPPER.readValue(json, StringValue.class);
assertNull(result.value);
ObjectMapper mapper = afterburnerMapperBuilder()
.withConfigOverride(String.class,
o -> o.setNullHandling(JsonSetter.Value.forValueNulls(Nulls.SKIP)))
.build();
result = mapper.readValue(json, StringValue.class);
assertEquals("default", result.value);
}
/**
* Set the directory where the agent should put the job working directory.
*
* @param requestedJobDirectoryLocation The location
* @return The builder
*/
@JsonSetter
public Builder withRequestedJobDirectoryLocation(@Nullable final String requestedJobDirectoryLocation) {
this.bRequestedJobDirectoryLocation = requestedJobDirectoryLocation == null
? null
: new File(requestedJobDirectoryLocation);
return this;
}
protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
JsonInclude.Value defIncl,
JsonSetter.Value defSetter,
VisibilityChecker<?> defVisibility,
Boolean defMergeable) {
_overrides = overrides;
_defaultInclusion = defIncl;
_defaultSetterInfo = defSetter;
_visibilityChecker = defVisibility;
_defaultMergeable = defMergeable;
}
@JsonSetter("required")
public void setRequiredProperties(List<String> required) {
if (properties != null) {
for (String s : required) {
Property p = properties.get(s);
if (p != null) {
p.setRequired(true);
}
}
}
}
public void testBeanMergingWithNullDefault() throws Exception
{
// By default `null` should simply overwrite value
ConfigDefault config = MAPPER.readerForUpdating(new ConfigDefault(5, 7))
.readValue(aposToQuotes("{'loc':null}"));
assertNotNull(config);
assertNull(config.loc);
// but it should be possible to override setting to, say, skip
// First: via specific type override
// important! We'll specify for value type to be merged
ObjectMapper mapper = afterburnerMapperBuilder()
.withConfigOverride(AB.class,
o -> o.setNullHandling(JsonSetter.Value.forValueNulls(Nulls.SKIP)))
.build();
config = mapper.readerForUpdating(new ConfigDefault(137, -3))
.readValue(aposToQuotes("{'loc':null}"));
assertNotNull(config.loc);
assertEquals(137, config.loc.a);
assertEquals(-3, config.loc.b);
// Second: by global defaults
mapper = afterburnerMapperBuilder()
.changeDefaultNullHandling(n -> n.withValueNulls(Nulls.SKIP))
.build();
config = mapper.readerForUpdating(new ConfigDefault(12, 34))
.readValue(aposToQuotes("{'loc':null}"));
assertNotNull(config.loc);
assertEquals(12, config.loc.a);
assertEquals(34, config.loc.b);
}
@JsonSetter("channel")
public void setChannel(JsonNode jsonNode) {
if (jsonNode.isObject()) {
try {
this.channel = new ObjectMapper().treeToValue(jsonNode, Channel.class);
} catch (JsonProcessingException e) {
logger.error("Error deserializing json: ", e);
}
} else if (jsonNode.isTextual()) {
this.channelId = jsonNode.asText();
}
}
@Deprecated
@JsonSetter("uri")
public void setUri(String uri) {
if (uri != null && !"".equalsIgnoreCase(uri.trim())) {
this.uris = Collections.singleton(uri);
}
}
@JsonSetter("id")
public void setId(final JsonRpcRequestId id) {
this.id = id;
}
@JsonSetter("params")
public void setParams(final Object params) {
this.params = params;
}
@JsonSetter("mowed_ts")
public void setMowedTimestamp (long mowed_ts_)
{
mowedTimestamp = mowed_ts_;
}
@JsonSetter
public SearchResult<T> setProcessingTimeMS(Long processingTimeMS) {
this.processingTimeMS = processingTimeMS;
return this;
}
@JsonSetter("membership")
public void setMembership(final String membership) {
this.membership = membership;
}
@JsonSetter("device")
public void setDevice (String device_)
{
device = device_;
}
@JsonSetter("dt")
public void setTimestamp(long unixTime) {
this.timestamp = Instant.ofEpochMilli(unixTime * 1000);
}
@JsonSetter ("column")
public void setName(SchemaPath name) {
this.name = name;
}
@JsonSetter("alias")
public void setUsernameAlias(String alias) {
this.alias = alias;
}
@JsonSetter ("rowcount")
public void setCount(long count) {
this.count = count;
}
@JsonSetter ("nonnullrowcount")
public void setNonNullCount(long nonNullCount) {
this.nonNullCount = nonNullCount;
}
@JsonSetter("EnMin")
public void setEndMinute (int endMinute)
{
this.endMinute = endMinute;
}
@JsonSetter ("avgwidth")
public void setAvgWidth(double width) { this.width = width; }
@JsonSetter("Crypto") // older wallet files may have this attribute name
public void setCryptoV1(Crypto crypto) {
setCrypto(crypto);
}
@JsonSetter
public void setOperationName(final String operationName) {
this.operationName = operationName;
}
@JsonSetter
public void setVariables(final Map<String, Object> variables) {
this.variables = variables;
}
@JsonSetter("statistic")
public HistoryTrendItem setStatistic(final Statistic statistic) {
this.data = statistic;
return this;
}
@JsonSetter("names")
public void setNames(Set<String> names) {
this.names = names;
}
@JsonSetter("msgs")
public void setMessageCount(int count) {
this.msgCount = count;
}
@JsonSetter("build")
public void setBuildInfo(ServerBuildInfo info) {
this.buildInfo = info;
}
@JsonSetter("REPORT")
public void setReports(List<Report> reports) {
this.reports = reports;
}