google / google/gson

Path $ is not provided when there is JsonSyntaxException with Custom Deserializer

Open
#1,135 2 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

With the latest version of the Gson library I do get detailed path to the json node that is causing the error.
However, this path $ information seems be absent when there is JsonSyntaxException in the custom deserializer

Following is the close representation of the deserializer i have implemented

```java
public class CustDeserializer implements JsonDeserializer {

Map mapType;

public CustDeserializer() {

mapType = new HashMap<>();
mapType.put("a", A.class);
mapType.put("b", B.class);
mapType.put("c", C.class);

}

@Override
public Wrapper deserialize(JsonElement jsonElement,
Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

JsonObject sJsonObj = jsonElement.getAsJsonObject();
String sTypeValue = sJsonObj.getAsJsonPrimitive("type").getAsString();
JsonElement sData = sJsonObj.get("data");

Type sType = mapType.get(sTypeValue);
if(sType == null || sData.isJsonNull()) {
return null;
}
SType sTypeEnum = jsonDeserializationContext.deserialize(sJsonObj.get("sType"), SType.class);
Object sDataObj = jsonDeserializationContext.deserialize(sData, sType);
return new Wrapper(sTypeEnum, sDataObj);
}
}

//Exception thrown when the Json string doesnt match with the object being constructed
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
```

Gson Version: 2.8.+

Thanks in advance

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.