I've read a lot of answers about this error and I don't find the solution for mine maybe the class object isn't the same
My error is :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
So, I just trying to get an Object from JSON like this :
JsonObject mS1SystemStatusJsonObject = parameterEntries.getValue().getAsJsonObject();
MS1SystemStatusResponse mS1SystemStatusResponse = gson.fromJson(mS1SystemStatusJsonObject, MS1SystemStatusResponse.class);
my JsonObject is :
{
"WARNING_CONDITION_LIST":
{
"warningConditionList": {"warningCondition":"BLACK_POSTAL_INK_WF_COND"}
},
"DISABLING_CONDITION_LIST": {"disablingConditionList":""},
"CurrentErrorList":"",
"LockState":"lockspend",
"SystemReadyState":true
}
My differents POJO (i don't put get and set for short the code) :
public class MS1SystemStatusResponse{
@SerializedName("LockState")
String lockState;
@SerializedName("SystemReadyState")
Boolean systemReadyState;
@SerializedName("WARNING_CONDITION_LIST")
WARNING_CONDITION_LIST warningConditionListMy;
@SerializedName("DISABLING_CONDITION_LIST")
DISABLING_CONDITION_LIST disablingConditionListMy;
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
@SerializedName("CurrentErrorList")
CurrentErrorList currentErrorList;
public class WARNING_CONDITION_LIST {
@SerializedName(value = "warningConditionList")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
WarningConditionList warningConditionList;
public class DISABLING_CONDITION_LIST {
@SerializedName(value = "disablingConditionList")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
DisablingConditionList disablingConditionList;
public class CurrentErrorList {
@SerializedName(value = "Error")
private Error error;
public class WARNING_CONDITION_LIST {
@SerializedName(value = "warningConditionList")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
WarningConditionList warningConditionList;
public class WarningConditionList {
@SerializedName(value = "warningCondition")
String warningCondition;
Here is the full stack trace and another class that i've forgot to add before :
Stacktrace :
java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
com.google.gson.internal.bind.JsonTreeReader.nextString(JsonTreeReader.java:154)
com.pb.marqueo.connect.plus.api.common.model.response.EmptyStringAsNullTypeAdapter.read(EmptyStringAsNullTypeAdapter.java:24)
com.pb.marqueo.connect.plus.api.common.model.response.EmptyStringAsNullTypeAdapter.read(EmptyStringAsNullTypeAdapter.java:10)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
com.google.gson.Gson.fromJson(Gson.java:861)
com.google.gson.Gson.fromJson(Gson.java:926)
com.google.gson.Gson.fromJson(Gson.java:899)
com.pb.marqueo.connect.plus.api.core.service.impl.ConnectPlusServicesImpl.getMS1SystemStatus(ConnectPlusServicesImpl.java:410)
com.pb.marqueo.connect.plus.api.core.facade.impl.ConnectPlusBusinessServicesImpl.startProduction(ConnectPlusBusinessServicesImpl.java:1236)
com.pb.marqueo.connect.plus.rest.services.impl.ConnectPlusControllerImpl.startProd(ConnectPlusControllerImpl.java:280)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.pb.marqueo.common.filter.OriginFilter.doFilter(OriginFilter.java:70)
Class Adapter :
public class EmptyStringAsNullTypeAdapter extends TypeAdapter<String> {
@Override
public void write(JsonWriter out, String value) throws IOException {
out.value(value);
}
@Override
public String read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
String result = in.nextString();
if ("".equals(result)) {
return null;
}
return result;
}
}
I turn around since 1 week :(
Ok, so from the conversation in the comments I understood that you want to be able to deserialize both of these cases:
{
"WARNING_CONDITION_LIST": {
"warningConditionList": {"warningCondition":"BLACK_POSTAL_INK_WF_COND"}
},
...
}
and
{
"WARNING_CONDITION_LIST": ""
...
}
In the first scenario, you want the object and in the 2nd scenario, you want null
.
The first thing I noticed is that you have the @JsonAdapter
annotation in the wrong place. It should actually be in the WARNING_CONDITION_LIST
field. Something like this:
public class MS1SystemStatusResponse {
// ...
@SerializedName("WARNING_CONDITION_LIST")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
WARNING_CONDITION_LIST warningConditionListMy;
// ...
}
However, this alone will not solve your problem. The problem is within the EmptyStringAsNullTypeAdapter
. It expects the field to just be a string and doesn't handle the case when it can be an object. For this, we can write the following adapter:
public class EmptyStringAsEmptyWarningConditionlTypeAdapter extends TypeAdapter<WARNING_CONDITION_LIST> {
@Override
public void write(JsonWriter out, WARNING_CONDITION_LIST value) throws IOException {
out.beginObject();
out.name("warningConditionList");
out.value(value.warningConditionList.warningCondition);
out.endObject();
}
@Override
public WARNING_CONDITION_LIST read(JsonReader in) throws IOException {
WARNING_CONDITION_LIST result = new WARNING_CONDITION_LIST();
result.warningConditionList = new WarningConditionList();
result.warningConditionList.warningCondition = "";
if (in.peek() == JsonToken.BEGIN_OBJECT) {
in.beginObject();
in.nextName();
in.beginObject();
in.nextName();
result.warningConditionList.warningCondition = in.nextString();
in.endObject();
in.endObject();
} else {
in.nextString();
return null;
}
return result;
}
}
Essentially it will check if you have a string or an object and return null
or the object respectively.
This works only for these 2 cases that I've shown you with the json and should be enough to help you out figuring the rest.
You'll probably face a similar issue with the CurrentErrorList
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.