小编Yah*_*adi的帖子

Jackson多态性:如何将多个子类型映射到同一个类

我正在使用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()的实现).

我看到的问题是否有解决方法?

java json jackson

10
推荐指数
3
解决办法
1万
查看次数

NSInvocation nil参数

我如何(或者甚至可以)将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)

cocoa objective-c

8
推荐指数
1
解决办法
5483
查看次数

标签 统计

cocoa ×1

jackson ×1

java ×1

json ×1

objective-c ×1