下面列出了com.fasterxml.jackson.databind.DeserializationContext#findRootValueDeserializer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Map<String, Object> readValue(ObjectMapper mapper, String json) throws IOException {
Map<String, Object> args = new LinkedHashMap<>();
JsonParser jp = mapper.getFactory().createParser(json);
DeserializationContext deserializationContext = ObjectMapperUtils.createDeserializationContext(mapper, jp);
jp.nextToken();
for (String fieldName = jp.nextFieldName(); fieldName != null; fieldName = jp.nextFieldName()) {
jp.nextToken();
ArgInfo argInfo = argInfos.get(fieldName);
if (argInfo == null) {
continue;
}
if (argInfo.deserializer == null) {
argInfo.deserializer = deserializationContext.findRootValueDeserializer(argInfo.javaType);
}
args.put(fieldName, argInfo.deserializer.deserialize(jp, deserializationContext));
}
return args;
}
@Override
public PersistentMap<?, ?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer deser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructRawMapType(Map.class));
Object o = deser.deserialize(p, ctxt);
if(HashMap.class.isAssignableFrom(mapType))
return HashMap.fromMap((Map)o);
if(TreeMap.class.isAssignableFrom(mapType))
return TreeMap.fromMap((Comparator)Comparator.naturalOrder(),(Map)o);
/** if(TrieMap.class.isAssignableFrom(mapType))
return TrieMap.fromMap((Map)o);
if(LinkedMap.class.isAssignableFrom(mapType))
return LinkedMap.fromMap((Map)o);
**/
Optional<Method> m = streamMethod.computeIfAbsent(mapType, c-> Stream.of(c.getMethods())
.filter(method -> "fromMap".equals(method.getName()))
.filter(method -> method.getParameterCount()==1)
.filter(method -> method.getParameterTypes()[0].isAssignableFrom(Map.class)).findFirst()
.map(m2->{ m2.setAccessible(true); return m2;}));
PersistentMap<?,?> x = m.map(mt -> (PersistentMap)new Invoker().executeMethod(o, mt, mapType)).orElse(null);
return x;
}
@Override
public LazyEither5<?,?,?,?,?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer ser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructSimpleType(LazyEitherBean.class, new JavaType[0]));
LazyEitherBean x = (LazyEitherBean)ser.deserialize(p, ctxt);
if(x.left1!=null){
return LazyEither5.left1(x.left1);
}
if(x.left2!=null){
return LazyEither5.left2(x.left2);
}
if(x.left3!=null){
return LazyEither5.left3(x.left2);
}
if(x.left4!=null){
return LazyEither5.left4(x.left2);
}
return LazyEither5.right(x.right);
}
private Object readEncoded(JsonParser p, DeserializationContext ctxt) throws IOException {
if (!p.isExpectedStartArrayToken()) {
throw new JsonParseException(p, "Expected array for encoded object got " + p.currentToken());
}
Object value = null;
JsonToken next = p.nextToken();
if (next != JsonToken.VALUE_STRING) {
throw new JsonParseException(p, "Encoded Class value not present " + next);
}
String typeName = p.getText();
p.nextToken(); // fwd to next
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = getClass().getClassLoader();
}
try {
Class<?> type = loader.loadClass(typeName);
if (Collection.class.isAssignableFrom(type)) {
value = readCollection(type, p, ctxt);
} else if (Map.class.isAssignableFrom(type)) {
value = readMap(type, p, ctxt);
} else {
JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(type));
value = deser.deserialize(p, ctxt);
}
} catch (ClassNotFoundException e) {
throw new JsonParseException(p, "Type name encoded " + typeName + " could not be loaded", e);
}
if (p.nextToken() != JsonToken.END_ARRAY) {
throw new JsonParseException(p, "Encoded expected end of ARRAY marker " + p.currentToken());
}
return value;
}
@Override
public ValueProvider<?> deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonDeserializer dser =
ctxt.findRootValueDeserializer(
checkNotNull(
innerType, "Invalid %s: innerType is null. Serialization error?", getClass()));
Object o = dser.deserialize(jp, ctxt);
return StaticValueProvider.of(o);
}
@Override
public LazyEither3<?,?,?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer ser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructSimpleType(LazyEitherBean.class, new JavaType[0]));
LazyEitherBean x = (LazyEitherBean)ser.deserialize(p, ctxt);
if(x.left1!=null){
return LazyEither3.left1(x.left1);
}
if(x.left2!=null){
return LazyEither3.left2(x.left2);
}
return LazyEither3.right(x.right);
}
@Override
public Ior<?,?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer ser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructSimpleType(IorBean.class, new JavaType[0]));
IorBean x = (IorBean)ser.deserialize(p, ctxt);
if(x.left!=null && x.right==null){
return Ior.left(x.left);
}
if(x.left==null && x.right!=null){
return Ior.right(x.right);
}
return Ior.both(x.left,x.right);
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
if (valueType.isContainerType()) {
deser = ctxt.findRootValueDeserializer(valueType.getContentType());
}else{
deser = ctxt.findRootValueDeserializer(valueType.containedTypeOrUnknown(0));
}
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
if (valueType.isContainerType()) {
deser = ctxt.findRootValueDeserializer(valueType.getContentType());
}else{
deser = ctxt.findRootValueDeserializer(valueType.containedTypeOrUnknown(0));
}
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
if (valueType.isContainerType()) {
deser = ctxt.findRootValueDeserializer(valueType.getContentType());
}else{
deser = ctxt.findRootValueDeserializer(valueType.containedTypeOrUnknown(0));
}
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
if (valueType.isContainerType()) {
deser = ctxt.findRootValueDeserializer(valueType.getContentType());
}else{
deser = ctxt.findRootValueDeserializer(valueType.containedTypeOrUnknown(0));
}
}
@Override
public Either<?,?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer ser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructSimpleType(EitherBean.class, new JavaType[0]));
EitherBean x = (EitherBean)ser.deserialize(p, ctxt);
if(x.left!=null){
return Either.left(x.left);
}
return Either.right(x.right);
}
@Override
public LazyEither4<?,?,?,?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer ser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructSimpleType(LazyEitherBean.class, new JavaType[0]));
LazyEitherBean x = (LazyEitherBean)ser.deserialize(p, ctxt);
if(x.left1!=null){
return LazyEither4.left1(x.left1);
}
if(x.left2!=null){
return LazyEither4.left2(x.left2);
}
if(x.left3!=null){
return LazyEither4.left3(x.left2);
}
return LazyEither4.right(x.right);
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
if (valueType.isContainerType()) {
deser = ctxt.findRootValueDeserializer(valueType.getContentType());
}else{
deser = ctxt.findRootValueDeserializer(valueType.containedTypeOrUnknown(0));
}
}
@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException {
if (valueType.isContainerType()) {
deser = ctxt.findRootValueDeserializer(valueType.getContentType());
}else{
deser = ctxt.findRootValueDeserializer(valueType.containedTypeOrUnknown(0));
}
}
@Override
public LazyEither<?,?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer ser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructSimpleType(LazyEitherBean.class, new JavaType[0]));
LazyEitherBean x = (LazyEitherBean)ser.deserialize(p, ctxt);
if(x.left!=null){
return LazyEither.left(x.left);
}
return LazyEither.right(x.right);
}
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonDeserializer deser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructArrayType(Object.class));
Object[] o = (Object[])deser.deserialize(p, ctxt);
if(o.length==0)
return Tuple.empty();
if(o.length==1)
return Tuple.tuple(o[0]);
if(o.length==2)
return Tuple.tuple(o[0],o[1]);
if(o.length==3)
return Tuple.tuple(o[0],o[1],o[2]);
if(o.length==4)
return Tuple.tuple(o[0],o[1],o[2],o[3]);
if(o.length==5)
return Tuple.tuple(o[0],o[1],o[2],o[3],o[4]);
if(o.length==6)
return Tuple.tuple(o[0],o[1],o[2],o[3],o[4],o[5]);
if(o.length==7)
return Tuple.tuple(o[0],o[1],o[2],o[3],o[4],o[5],o[6]);
if(o.length==8)
return Tuple.tuple(o[0],o[1],o[2],o[3],o[4],o[5],o[6],o[7]);
throw new ArrayIndexOutOfBoundsException("Max tuple length exceeded");
}
private Object readObject(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {
MarshallingType<?> type = field.marshallingType();
switch (p.currentToken()) {
case VALUE_FALSE:
case VALUE_TRUE: {
if (type.equals(MarshallingType.BOOLEAN)) {
return p.getBooleanValue();
}
throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got boolean field value");
}
case VALUE_NUMBER_FLOAT:
case VALUE_NUMBER_INT: {
if (type.equals(MarshallingType.INTEGER)) {
return p.getIntValue();
} else if (type.equals(MarshallingType.LONG)) {
return p.getLongValue();
} else if (type.equals(MarshallingType.FLOAT)) {
return p.getFloatValue(); // coerce should work
} else if (type.equals(MarshallingType.DOUBLE)) {
return p.getDoubleValue(); // coerce should work
} else if (type.equals(MarshallingType.BIG_DECIMAL)) {
return p.getDecimalValue(); // coerce should work
} else if (type.equals(MarshallingType.INSTANT)) { // we serialize as BigDecimals
JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(Instant.class));
return deser.deserialize(p, ctxt);
}
throw new JsonMappingException(p,
"Type mismatch, expecting " + type + " got int/float/double/big_decimal/instant");
}
case VALUE_STRING: {
if (type.equals(MarshallingType.STRING)) {
return p.getText();
} else if (type.equals(MarshallingType.SDK_BYTES)) {
byte[] bytes = p.getBinaryValue();
return SdkBytes.fromByteArray(bytes);
}
throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got string/bytes");
}
case START_OBJECT: {
if (type.equals(MarshallingType.MAP)) {
return readMap(field, p, ctxt);
} else if (type.equals(MarshallingType.SDK_POJO)) {
return readPojo(field.constructor().get(), p, ctxt);
}
throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo");
}
case START_ARRAY: {
if (type.equals(MarshallingType.LIST)) {
return readList(field, p, ctxt);
}
throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got List type");
}
case VALUE_NULL:
return null;
default:
throw new JsonMappingException(p, "Can not map type " + type + " Token = " + p.currentToken());
}
}
@Override
public LogicalOperator deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
ObjectIdGenerator<Integer> idGenerator = new ObjectIdGenerators.IntSequenceGenerator();
JsonLocation start = jp.getCurrentLocation();
JsonToken t = jp.getCurrentToken();
LogicalOperator parent = null;
LogicalOperator first = null;
LogicalOperator prev = null;
Integer id = null;
while (true) {
String fieldName = jp.getText();
t = jp.nextToken();
switch (fieldName) { // switch on field names.
case "@id":
id = _parseIntPrimitive(jp, ctxt);
break;
case "input":
JavaType tp = ctxt.constructType(LogicalOperator.class);
JsonDeserializer<Object> d = ctxt.findRootValueDeserializer(tp);
parent = (LogicalOperator) d.deserialize(jp, ctxt);
break;
case "do":
if (!jp.isExpectedStartArrayToken()) {
throwE(
jp,
"The do parameter of sequence should be an array of SimpleOperators. Expected a JsonToken.START_ARRAY token but received a "
+ t.name() + "token.");
}
int pos = 0;
while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
// logger.debug("Reading sequence child {}.", pos);
JsonLocation l = jp.getCurrentLocation(); // get current location
// first so we can
// correctly reference the
// start of the object in
// the case that the type
// is wrong.
LogicalOperator o = jp.readValueAs(LogicalOperator.class);
if (pos == 0) {
if (!(o instanceof SingleInputOperator) && !(o instanceof SourceOperator)) {
throwE(
l,
"The first operator in a sequence must be either a ZeroInput or SingleInput operator. The provided first operator was not. It was of type "
+ o.getClass().getName());
}
first = o;
} else {
if (!(o instanceof SingleInputOperator)) {
throwE(l, "All operators after the first must be single input operators. The operator at position "
+ pos + " was not. It was of type " + o.getClass().getName());
}
SingleInputOperator now = (SingleInputOperator) o;
now.setInput(prev);
}
prev = o;
pos++;
}
break;
default:
throwE(jp, "Unknown field name provided for Sequence: " + jp.getText());
}
t = jp.nextToken();
if (t == JsonToken.END_OBJECT) {
break;
}
}
if (first == null) {
throwE(start, "A sequence must include at least one operator.");
}
if ((parent == null && first instanceof SingleInputOperator)
|| (parent != null && first instanceof SourceOperator)) {
throwE(start,
"A sequence must either start with a ZeroInputOperator or have a provided input. It cannot have both or neither.");
}
if (parent != null && first instanceof SingleInputOperator) {
((SingleInputOperator) first).setInput(parent);
}
// set input reference.
if (id != null) {
ReadableObjectId rid = ctxt.findObjectId(id, idGenerator, null);
rid.bindItem(prev);
// logger.debug("Binding id {} to item {}.", rid.id, rid.item);
}
return first;
}
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(ctxt.constructType(byte[].class));
byte[] bytes = (byte[]) deserializer.deserialize(p, ctxt);
return deserialize(bytes);
}