to sum it up before the wall of text below :-)
在下面的文本墙前总结:-)
I need help with how to deserialize a Dictionary using Jackson and a custom deserializer.
我需要有关如何使用Jackson和自定义反序列化器反序列化字典的帮助。
Right now I have an Android app communication with a .NET (C#) server. They use JSON to communicate.
现在我有一个与.NET(C#)服务器的Android应用程序通信。他们使用JSON进行通信。
On the JAVA-side, I am using Jackson to handle the JSON and on the .NET-side I am using the built in DataContractSerializer (I know, ppl will start commenting I should use something else, but Im not so... ;-) )
在JAVA方面,我使用Jackson来处理JSON,在.NET端我使用内置的DataContractSerializer(我知道,ppl会开始评论我应该使用别的东西,但我不是这样......; - ))
The problem is that I am sending Dictionaries from C# and I want that to be deserialized to HashMaps om the JAVA-side, but I havent found a good resource for how to do that.
问题是我从C#发送字典,我希望将其反序列化为JAVA端的HashMaps,但我还没有找到一个很好的资源来解决这个问题。
One example of a Dictionary I am sending from C#:
我从C#发送的词典的一个例子:
// Here, the object Equipment is the key, and the int following indicates the amount
[DataMember]
public Dictionary<Equipment, int> EquipmentList { get; set; }
And just for reference, the Equipment object in C#:
仅供参考,C#中的Equipment对象:
[DataContract]
public class Equipment
{
[DataMember]
public uint Id { get; set; }
[DataMember]
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType())
return false;
Equipment e = (Equipment)obj;
return e.Id == this.Id;
}
}
Its serialized correctly into decent JSON on the C#-side, the Dictionary looks like this:
它在C#-side上正确地序列化为合适的JSON,字典看起来像这样:
//....
"EquipmentList":[
{
"Key":{
"EquipmentId":123,
"Name":"MyName"
},
"Value":1
}
//....
I have added a custom serializer (CustomMapSerializer), like this:
我添加了一个自定义序列化程序(CustomMapSerializer),如下所示:
public static ObjectMapper mapper = new ObjectMapper();
private static SimpleDeserializers simpleDeserializers = new SimpleDeserializers();
private static StdDeserializerProvider sp = new StdDeserializerProvider();
public static void InitSerialization()
{
simpleDeserializers.addDeserializer(String.class, new CustomStringDeserializer());
simpleDeserializers.addDeserializer(Map.class, new CustomMapDeserializer());
sp.withAdditionalDeserializers(simpleDeserializers);
mapper.setDeserializerProvider(sp);
}
And decorated the field like this:
并装饰这样的领域:
@JsonDeserialize(using=CustomMapDeserializer.class)
public Map<Equipment, Integer> EquipmentList;
And finally, when I run it I do get a break in the custom deserializer class, but I am not sure how to proceed from here:
最后,当我运行它时,我确实在自定义反序列化器类中休息,但我不知道如何从这里继续:
public class CustomMapDeserializer extends JsonDeserializer<Map> {
@Override
public Map deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException, JsonProcessingException
{
return new HashMap<Object, Object>(); // <-- I can break here
}
}
So, what I would like is some input on how to create a HashMap with the correct values in it, ie a deserialized Equipment as Key and an Int as value.
所以,我想要的是关于如何创建具有正确值的HashMap的一些输入,即反序列化设备作为键和Int作为值。
Anyone out there who can assist? =)
谁有人可以提供协助? =)
1 个解决方案
#1
0
Ok, after a while testing and researching, this is what I came up with.
好的,经过一段时间的测试和研究,这就是我想出来的。
The custom deserializer looks like this:
自定义反序列化器如下所示:
public class CustomMapCoTravellerDeserializer extends JsonDeserializer<Map> {
@Override
public Map deserialize(JsonParser jp, DeserializationContext arg1) throws IOException, JsonProcessingException
{
HashMap<CoTravellers, Integer> myMap = new HashMap<CoTravellers, Integer>();
CoTravellers ct = new CoTravellers();
jp.nextToken(); // {
jp.nextToken(); // Key
jp.nextToken(); // {
jp.nextToken(); // "CoTravellerId"
jp.nextToken(); // CoTravellerId Id
int coTravellerValue = jp.getIntValue();
jp.nextToken(); // Name
jp.nextToken(); // Name Value
String coTravellerName = jp.getText();
jp.nextToken(); // }
jp.nextToken(); // "Value"
jp.nextToken(); // The value
int nbr = jp.getIntValue();
ct.CoTravellerId = coTravellerValue;
ct.Name = coTravellerName;
myMap.put(ct, nbr);
return myMap;
}
}
I think this will work, if I can only figure out the JsonMappingException I am getting... but I will post on that separately =)
我认为这将有效,如果我只能弄清楚我得到的JsonMappingException ...但我将单独发布=)
#1
0
Ok, after a while testing and researching, this is what I came up with.
好的,经过一段时间的测试和研究,这就是我想出来的。
The custom deserializer looks like this:
自定义反序列化器如下所示:
public class CustomMapCoTravellerDeserializer extends JsonDeserializer<Map> {
@Override
public Map deserialize(JsonParser jp, DeserializationContext arg1) throws IOException, JsonProcessingException
{
HashMap<CoTravellers, Integer> myMap = new HashMap<CoTravellers, Integer>();
CoTravellers ct = new CoTravellers();
jp.nextToken(); // {
jp.nextToken(); // Key
jp.nextToken(); // {
jp.nextToken(); // "CoTravellerId"
jp.nextToken(); // CoTravellerId Id
int coTravellerValue = jp.getIntValue();
jp.nextToken(); // Name
jp.nextToken(); // Name Value
String coTravellerName = jp.getText();
jp.nextToken(); // }
jp.nextToken(); // "Value"
jp.nextToken(); // The value
int nbr = jp.getIntValue();
ct.CoTravellerId = coTravellerValue;
ct.Name = coTravellerName;
myMap.put(ct, nbr);
return myMap;
}
}
I think this will work, if I can only figure out the JsonMappingException I am getting... but I will post on that separately =)
我认为这将有效,如果我只能弄清楚我得到的JsonMappingException ...但我将单独发布=)