我正在使用Jackson 1.9.x. 坚持动物的例子,这就是我想做的事情:
假设我有一个Animal类:
public class Animal {
private String type;
// accessors
}
public class Mammal extends Animal {
private String diet;
// accessors
}
public class Bird extends Animal {
private boolean tropical;
// accessors
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情(我将一些子类型映射到一个类,然后将一些子类型映射到另一个类):
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Mammal.class, name = "Dog"),
@JsonSubTypes.Type(value = Mammal.class, name = "Cat"),
@JsonSubTypes.Type(value = Bird.class, name = "Dodo"},
@JsonSubTypes.Type(value = Bird.class, name = "Cockatoo"})
public class Animal {
}
Run Code Online (Sandbox Code Playgroud)
我现在看到的是杰克逊只会识别狗对哺乳动物和渡渡鸟对鸟的映射.这是因为StdSubtypeResolver._collectAndResolve()只允许同一个类注册一次(由于NamedType.equals()的实现).
我看到的问题是否有解决方法?
我如何(或者甚至可以)将nil参数传递给NSInvocation对象?
我试着这样做:
NSMethodSignature* signature = [AClass instanceMethodSignatureForSelector:@selector(aMethod:theOtherArg:)];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
[invocation setTarget: aTargetObj];
[invocation setSelector: @selector(aMethod:theOtherArg:)];
/* I've tried both this way */
AnObj* arg1 = nil;
AnotherObj* arg2 = nil;
[invocation setArgument: &arg1 atIndex:2];
[invocation setArgument: &arg2 atIndex:3];
/* and this way */
//[invocation setArgument: nil atIndex:2];
//[invocation setArgument: nil atIndex:3];
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
//opQueue is an NSOperationQueue object
[opQueue addOperation:operation];
Run Code Online (Sandbox Code Playgroud)
第一种方法会因此消息而崩溃:
Thread 0 Crashed:
0 libSystem.B.dylib 0x927c1f10 strlen + 16
1 com.apple.CoreFoundation …Run Code Online (Sandbox Code Playgroud)