Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
support 'size 1 list<map> as map'. for issue #1189
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 9, 2017
1 parent 69df5e9 commit 01d2583
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import java.util.concurrent.ConcurrentMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.*;
import com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask;
import com.alibaba.fastjson.serializer.CollectionCodec;
import com.alibaba.fastjson.util.TypeUtils;

public class MapDeserializer implements ObjectDeserializer {
Expand Down Expand Up @@ -71,7 +73,17 @@ public static Map parseMap(DefaultJSONParser parser, Map<String, Object> map, Ty
}
msg += ", ";
msg += lexer.info();


JSONArray array = new JSONArray();
parser.parseArray(array, fieldName);

if (array.size() == 1) {
Object first = array.get(0);
if (first instanceof JSONObject) {
return (JSONObject) first;
}
}

throw new JSONException(msg);
}

Expand Down
76 changes: 76 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1100/Issue1189.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.alibaba.json.bvt.issue_1100;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import junit.framework.TestCase;

import java.util.Map;

/**
* Created by wenshao on 10/05/2017.
*/
public class Issue1189 extends TestCase {
public void test_for_issue() throws Exception {
String str = new String("{\"headernotificationType\": \"PUSH\",\"headertemplateNo\": \"99\",\"headerdestination\": [{\"target\": \"all\",\"targetvalue\": \"all\"}],\"body\": [{\"title\": \"预约超时\",\"body\": \"您的预约已经超时\"}]}");

JsonBean objeclt = JSON.parseObject(str, JsonBean.class);
Map<String, String> list = objeclt.getBody();
System.out.println(list.get("body"));
}

public static class JsonBean {
private Map<String, String> body;
private int headertemplateno;
private Map<String, String> headerdestination;
private String headernotificationtype;
private String notificationType;



public Map<String, String> getBody() {
return body;
}
public void setBody(Map<String, String> body) {
this.body = body;
}
public int getHeadertemplateno() {
return headertemplateno;
}
public void setHeadertemplateno(int headertemplateno) {
this.headertemplateno = headertemplateno;
}
public Map<String, String> getHeaderdestination() {
return headerdestination;
}
public void setHeaderdestination(Map<String, String> headerdestination) {
this.headerdestination = headerdestination;
}
public String getHeadernotificationtype() {
return headernotificationtype;
}
public void setHeadernotificationtype(String headernotificationtype) {
this.headernotificationtype = headernotificationtype;
}
public String getNotificationType() {
return notificationType;
}
public void setNotificationType(String notificationType) {
this.notificationType = notificationType;
}
public JsonBean(Map<String, String> body, int headertemplateno,
Map<String, String> headerdestination,
String headernotificationtype, String notificationType) {
super();
this.body = body;
this.headertemplateno = headertemplateno;
this.headerdestination = headerdestination;
this.headernotificationtype = headernotificationtype;
this.notificationType = notificationType;
}
public JsonBean() {
super();
// TODO Auto-generated constructor stub
}

}
}

0 comments on commit 01d2583

Please sign in to comment.