开发者

Issue : Serializing using Jersey, Deserializing JSON using gson

I have been using Jersey for REST API and returning JSON. On client side I am using google-gson. While deserializing JSON, I am getting following error.

com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {} given the type interface java.util.Map
        at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
        at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
        at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:116)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonDeserializationCo

The Object which is being serialized to JSON by Jersey is having non-generic(no type specified) Map and List

public class Dealer implements Serializable
{
  private String serviceURL;
  private Map hoursService;
  List dealerAttributes;  
}

Here is the JSON generated by Jersey

{"serviceURL":www.google.com,"hoursService":{"3":{"dayOfWeek":3,"closeTime":"6:30 PM","openTime":"7:30 AM"},"2":{"dayOfWeek":2,"closeTime":"6:30 PM","openTime":"7:30 AM"},"1":{"dayOfWeek":1,"closeTime":"6:30 PM","openTime":"7:30 AM"}},"dealerAttributes":[{"language":"English","dealerAttributeName":"Spanish Speaking","updateDate":1174971061000},{"language":"English","updateDate":1103003316000}]}

What could be the reason. Please help me to resolve this issue. Is there any othe开发者_StackOverflowr utility which is better than gson? Please suggest.

Thanks.


The example JSON is invalid. http://jsonlint.com

www.google.com must be enclosed with quotes. Then it is valid.

With the JSON corrected, the following deserialization example outputs

[Dealer: serviceURL=www.google.com, hoursService={
1=[Hours: dayOfWeek=1, closeTime=6:30 PM, openTime=7:30 AM], 
2=[Hours: dayOfWeek=2, closeTime=6:30 PM, openTime=7:30 AM], 
3=[Hours: dayOfWeek=3, closeTime=6:30 PM, openTime=7:30 AM]}, 
dealerAttributes=[
[DealerAttributes: language=English, dealerAttributeName=Spanish Speaking, updateDate=1174971061000], 
[DealerAttributes: language=English, dealerAttributeName=null, updateDate=1103003316000]]]
public class Foo
{
  static String jsonInput = 
    "{" + 
      "\"serviceURL\":\"www.google.com\"," + 
      "\"hoursService\":" +
      "{" + 
        "\"1\":" +
        "{" + 
          "\"dayOfWeek\":1," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}," + 
        "\"2\":" +
        "{" + 
          "\"dayOfWeek\":2," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}," + 
        "\"3\":" +
        "{" + 
          "\"dayOfWeek\":3," + 
          "\"closeTime\":\"6:30 PM\"," + 
          "\"openTime\":\"7:30 AM\"" + 
        "}" + 
      "}," + 
      "\"dealerAttributes\":" +
      "[" + 
        "{" + 
          "\"language\":\"English\"," + 
          "\"dealerAttributeName\":\"Spanish Speaking\"," + 
          "\"updateDate\":1174971061000" + 
        "}," + 
        "{" + 
          "\"language\":\"English\"," + 
          "\"updateDate\":1103003316000" + 
        "}" + 
      "]" + 
    "}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    Dealer dealer = gson.fromJson(jsonInput, Dealer.class);
    System.out.println(dealer);
  }
}

class Dealer
{
  private String serviceURL;
  private Map<String, Hours> hoursService;
  private List<DealerAttributes> dealerAttributes;

  @Override
  public String toString()
  {
    return String.format(
      "[Dealer: serviceURL=%1$s, hoursService=%2$s, dealerAttributes=%3$s]",
      serviceURL, hoursService, dealerAttributes);
  }
}

class Hours
{
  private int dayOfWeek;
  private String closeTime;
  private String openTime;

  @Override
  public String toString()
  {
    return String.format(
      "[Hours: dayOfWeek=%1$d, closeTime=%2$s, openTime=%3$s]",
      dayOfWeek, closeTime, openTime);
  }
}

class DealerAttributes
{
  private String language;
  private String dealerAttributeName;
  private long updateDate;

  @Override
  public String toString()
  {
    return String.format(
      "[DealerAttributes: language=%1$s, dealerAttributeName=%2$s, updateDate=%3$d]",
      language, dealerAttributeName, updateDate);
  }
}

This does use generic types, which I understand you said you weren't using to serialize, but are you stuck with the same limitation when deserialization? If so, then you'll have to implement some more "manual" deserialization parsing, because, as the docs say, "[w]hile deserializing, Collection must be of a specific generic type".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜