简体   繁体   English

使用 Jackson 反序列化从 .NET 生成的 JSON 字符串

[英]Deserialize JSON string generated from .NET using Jackson

I refer to this question for my classes and code, but this time with another error.我在我的课程和代码中提到了这个问题,但这次又出现了另一个错误。

I have since the above question added an empty constructor to ProtocolContainer.自从上述问题以来,我已经向 ProtocolContainer 添加了一个空的构造函数。

The situation is now that a .NET app (C#) is creating the JSON string using DataContractJsonSerializer.现在的情况是 .NET 应用程序 (C#) 正在使用 DataContractJsonSerializer 创建 JSON 字符串。 The ProtocolContaier has a "SubPacket" defined as a DataPacket, and then there are subclasses to DataPacket (so this is a polymorphic problem). ProtocolContaier 有一个定义为 DataPacket 的“SubPacket”,然后有 DataPacket 的子类(所以这是一个多态问题)。

The string looks like this:字符串如下所示:

{"DataPacketJSONString":null,"DataPacketType":"MyPackage.DataPackets.LoginRequestReply","MessageId":6604,"SenderUsername":null,"SubPacket":{"__type":"LoginRequestReply:#MyPackage.DataPackets","Reason":"Wrong pass or username","Success":false,"Username":"User1"}} {"DataPacketJSONString":null,"DataPacketType":"MyPackage.DataPackets.LoginRequestReply","MessageId":6604,"SenderUsername":null,"SubPacket":{"__type":"LoginRequestReply:#MyPackage.DataPackets"," Reason":"错误的密码或用户名","Success":false,"Username":"User1"}}

Note the __type in the JSON above.请注意上面 JSON 中的__type That is something that .NET created to keep track of what type of object the "SubPacket" is.这是 .NET 创建的用于跟踪“SubPacket”是什么类型的对象的东西。

The problem is how to deserialize it on the Java side, using Jackson?问题是如何在 Java 端使用 Jackson 反序列化它?

It gives me the following error:它给了我以下错误:

org.codehaus.jackson.map.JsonMappingException: Could not resolve type id 'LoginRequestReply:#MyPackage.DataPackets' into a subtype of [simple type, class MyPackage.DataPacket] at [Source: java.io.StringReader@40561798; org.codehaus.jackson.map.JsonMappingException:无法将类型 id 'LoginRequestReply:#MyPackage.DataPackets' 解析为 [简单类型,类 MyPackage.DataPacket] 的子类型 [来源:java.io.StringReader@40561798; line: 1, column: 168] (through reference chain: MyPackage.ProtocolContainer["SubPacket"])行:1,列:168](通过参考链:MyPackage.ProtocolContainer["SubPacket"])

And my guess is that Jackson can't interpret the __type in the JSON string above.我的猜测是 Jackson 无法解释上面 JSON 字符串中的__type And I think that is dues to its format.我认为这是由于它的格式。 The question is: how can it be resolved?问题是:如何解决?

DataPacket.java - the superclass, defined in ProtocolContainer (see link above): DataPacket.java - 超类,在 ProtocolContainer 中定义(见上面的链接):

package MyPackage;

import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;

import MyPackage.DataPackets.*;

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="__type")
@JsonSubTypes(
{
    @JsonSubTypes.Type(value = LoginRequest.class, name = "LoginRequest"),
    @JsonSubTypes.Type(value = LoginRequestReply.class, name = "LoginRequestReply")
})
public class DataPacket 
{
    
}

So, one solution (that I find ugly) is to adapt the JsonSubTypes so it looks the same as what is created on the C# end:因此,一种解决方案(我觉得很丑)是调整 JsonSubTypes,使其看起来与在 C# 端创建的相同:

@JsonSubTypes(
{
    @JsonSubTypes.Type(value = LoginRequest.class, name = "LoginRequest:#MyPackage.DataPackets"),
    @JsonSubTypes.Type(value = LoginRequestReply.class, name = "LoginRequestReply:#MyPackage.DataPackets")
})

I havent found any better way.我还没有找到更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM