下面列出了com.fasterxml.jackson.databind.InjectableValues#Std ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void configureObjectMapper(ObjectMapper mapper) {
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setConfig(mapper.getSerializationConfig().withView(ApiOnly.class));
// add all sub types of CloudAccount
for (var type : REFLECTIONS_SUBTYPE_SCANNER.getSubTypesOf(CloudAccount.class)) {
mapper.registerSubtypes(type);
}
// set injectable value to null
var values = new InjectableValues.Std();
values.addValue("hash", null);
mapper.setInjectableValues(values);
}
public FragmentRoot readFragmentOperator(com.google.protobuf.ByteString json, FragmentCodec codec) throws JsonProcessingException, IOException {
final InjectableValues.Std injectableValues = new InjectableValues.Std(new HashMap<>(injectables));
PhysicalOperator op = readValue(mapper.readerFor(PhysicalOperator.class).with(injectableValues), json, codec);
if(op instanceof FragmentRoot){
return (FragmentRoot) op;
}else{
throw new UnsupportedOperationException(String.format("The provided json fragment doesn't have a FragmentRoot as its root operator. The operator was %s.", op.getClass().getCanonicalName()));
}
}
public static ObjectMapper objectMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());
mapper.registerModule(new JacksonTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// InjectableValues makes @JacksonInject work which is used at io.digdag.client.config.Config.<init>
InjectableValues.Std injects = new InjectableValues.Std();
injects.addValue(ObjectMapper.class, mapper);
mapper.setInjectableValues(injects);
return mapper;
}
public PhysicalPlanReader(
SabotConfig config,
ScanResult scanResult,
LogicalPlanPersistence lpPersistance,
final NodeEndpoint endpoint,
final Provider<CatalogService> catalogService,
SabotContext context) {
this.lpPersistance = lpPersistance;
final ObjectMapper lpMapper = lpPersistance.getMapper();
// automatic serialization of protobuf.
lpMapper.registerModule(new ProtobufModule());
final SimpleModule deserModule = new SimpleModule("CustomSerializers")
.addSerializer(MajorType.class, new MajorTypeSerDe.Se())
.addSerializer(ByteString.class, new ByteStringSer())
.addDeserializer(ByteString.class, new ByteStringDeser())
.addDeserializer(MajorType.class, new MajorTypeSerDe.De());
ProtoSerializers.registerSchema(deserModule, SourceConfig.getSchema());
lpMapper.registerModule(deserModule);
ConnectionConf.registerSubTypes(lpMapper, context.getConnectionReaderProvider().get());
Set<Class<? extends PhysicalOperator>> subTypes = PhysicalOperatorUtil.getSubTypes(scanResult);
for (Class<? extends PhysicalOperator> subType : subTypes) {
lpMapper.registerSubtypes(subType);
}
// store this map so that we can use later for fragment plan reader
this.injectables = new HashMap<>();
this.injectables.put(CatalogService.class.getName(), catalogService.get());
this.injectables.put(ConnectionReader.class.getName(), context.getConnectionReaderProvider().get());
this.injectables.put(SabotContext.class.getName(), context);
this.injectables.put(NodeEndpoint.class.getName(), endpoint);
InjectableValues.Std injectableValues = new InjectableValues.Std(this.injectables);
this.injectables.forEach(injectableValues::addValue);
this.mapper = lpMapper;
this.physicalPlanReader = mapper.readerFor(PhysicalPlan.class).with(injectableValues);
this.optionListReader = mapper.readerFor(OptionList.class).with(injectableValues);
this.operatorReader = mapper.readerFor(PhysicalOperator.class).with(injectableValues);
}
/**
* @param type Optional value type.
* @param str String to convert.
* @return Converted value.
* @throws IgniteCheckedException If failed to convert.
*/
private Object convert(@Nullable String type, @Nullable String str) throws IgniteCheckedException {
if (F.isEmpty(type) || str == null)
return str;
try {
switch (type.toLowerCase()) {
case "boolean":
case "java.lang.boolean":
return Boolean.valueOf(str);
case "byte":
case "java.lang.byte":
return Byte.valueOf(str);
case "short":
case "java.lang.short":
return Short.valueOf(str);
case "int":
case "integer":
case "java.lang.integer":
return Integer.valueOf(str);
case "long":
case "java.lang.long":
return Long.valueOf(str);
case "float":
case "java.lang.float":
return Float.valueOf(str);
case "double":
case "java.lang.double":
return Double.valueOf(str);
case "date":
case "java.sql.date":
return Date.valueOf(str);
case "time":
case "java.sql.time":
return Time.valueOf(str);
case "timestamp":
case "java.sql.timestamp":
return Timestamp.valueOf(str);
case "uuid":
case "java.util.uuid":
return UUID.fromString(str);
case "igniteuuid":
case "org.apache.ignite.lang.igniteuuid":
return IgniteUuid.fromString(str);
case "string":
case "java.lang.string":
return str;
}
// Creating an object of the specified type, if its class is available.
Class<?> cls = U.classForName(type, null);
if (cls != null)
return jsonMapper.readValue(str, cls);
// Creating a binary object if the type is not a class name or it cannot be loaded.
InjectableValues.Std prop = new InjectableValues.Std()
.addValue(IgniteBinaryObjectJsonDeserializer.BINARY_TYPE_PROPERTY, type)
.addValue(IgniteBinaryObjectJsonDeserializer.CACHE_NAME_PROPERTY, cacheName);
return jsonMapper.reader(prop).forType(BinaryObject.class).readValue(str);
}
catch (Throwable e) {
throw new IgniteCheckedException("Failed to convert value to specified type [type=" + type +
", val=" + str + ", reason=" + e.getClass().getName() + ": " + e.getMessage() + "]", e);
}
}