-3
I’m building a program that integrates an online store with another program that controls the physical stock of a store. Communication is done through HTTP requests that respond in JSON format.
In one of the response JSON, I have a variable named: default. I know it is a reserved word in the Java language and I cannot use it as a variable name, but I need to consume this JSON and turn it into an object.
I’m using the Gson library to help me make this "transformation". But it is not possible to do without solving the variable name problem, and that is what I want help with. Can you create a "nickname" for the default variable so that Gson can consume it? Or is there another better solution?
JSON
[{
“warehouses”: [
{
“id”: 1,
“createdAt”: “2016-12-27T10:58:13-02:00”,
“updatedAt”: “2016-12-27T14:57:39-02:00”,
“erpId”: “Armazém 1”,
“name”: “Armazém Revenda”,
“priority”: 1,
“branch”: {
“id”: 1,
“erpId”: “Filial 1”,
“name”: “Filial 1”,
“documentId”: “”,
“createdAt”: “2016-12-27T10:58:13-02:00”,
“updatedAt”: “2016-12-27T10:58:13-02:00”
},
“default”: true,
“quantity”: 1
}
]
}]
Warehouses
import java.util.Date;
public class Warehouses {
private int id;
private Date createdAt;
private Date updateAt;
private String erpId;
private String name;
private int priority;
private Branch branch;
private boolean default;
private int quantity;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdateAt() {
return updateAt;
}
public void setUpdateAt(Date updateAt) {
this.updateAt = updateAt;
}
public String getErpId() {
return erpId;
}
public void setErpId(String erpId) {
this.erpId = erpId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public Branch getBranch() {
return branch;
}
public void setBranch(Branch branch) {
this.branch = branch;
}
public boolean isDefault() {
return default;
}
public void setDefault(boolean default) {
this.default = default;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Branch
import java.util.Date;
public class Branch {
private int id;
private String erpId;
private String name;
private String documentId;
private Date createdAt;
private Date updateAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getErpId() {
return erpId;
}
public void setErpId(String erpId) {
this.erpId = erpId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDocumentId() {
return documentId;
}
public void setDocumentId(String documentId) {
this.documentId = documentId;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdateAt() {
return updateAt;
}
public void setUpdateAt(Date updateAt) {
this.updateAt = updateAt;
}
}
Consuming JSON
public static List<Warehouses> JSONtoList(String strJson) {
Type type = new TypeToken<List<Warehouses>>() {
}.getType();
Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy").create();
List<Warehouses> lista = gson.fromJson(strJson, type);
return lista;
}
Calling and Running (Yes, the string is passed by HTTP request, this is just an example to illustrate how I’m doing)
String strJson = "[{\r\n" +
"“warehouses”: [\r\n" +
"{\r\n" +
"“id”: 1,\r\n" +
"“createdAt”: “2016-12-27T10:58:13-02:00”,\r\n" +
"“updatedAt”: “2016-12-27T14:57:39-02:00”,\r\n" +
"“erpId”: “Armazém 1”,\r\n" +
"“name”: “Armazém Revenda”,\r\n" +
"“priority”: 1,\r\n" +
"“branch”: {\r\n" +
"“id”: 1,\r\n" +
"“erpId”: “Filial 1”,\r\n" +
"“name”: “Filial 1”,\r\n" +
"“documentId”: “”,\r\n" +
"“createdAt”: “2016-12-27T10:58:13-02:00”,\r\n" +
"“updatedAt”: “2016-12-27T10:58:13-02:00”\r\n" +
"},\r\n" +
"“default”: true,\r\n" +
"“quantity”: 1\r\n" +
"}\r\n" +
"]\r\n" +
"}]";
List<Warehouses> lista = JSONUtils.JSONtoList(strJson);
for(Warehouses w: lista) {
System.out.println(w.getId());
System.out.println(w.isDefault());
}
Error
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Failed to invoke public model.Warehouses() with no args
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:118)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at util.JSONUtils.JSONtoList(JSONUtils.java:168)
at view.PrincipalView.<init>(PrincipalView.java:69)
at view.LoginView$1.actionPerformed(LoginView.java:35)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.Error: Unresolved compilation problems:
Syntax error on token "default", invalid VariableDeclarator
This method must return a result of type boolean
Syntax error on token "default", delete this token
Syntax error on token "default", invalid VariableDeclaratorId
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete Statement
Syntax error, insert "}" to complete MethodBody
at model.Warehouses.<init>(Warehouses.java:14)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:110)
... 46 more
Explain the context better. What is the Java code that you are using to decode this JSON? Is there an error? Which one?
– bfavaretto
@bfavaretto The code is irrelevant, it works with any other JSON I use to turn into object. The problem is exactly what I said: in the JSON in question, it has a variable name (default) that is reserved word in the Java language and I don’t know how to get around this situation.
– lys
The problem is that the decoding of this JSON is trying to use the reserved word in a context where it is not allowed. That is why we need to know how you are doing it. And if it gives any error message. And what error.
– bfavaretto
@bfavaretto I’ve added some of the project’s code and the bug log that it displays when I run. See if it gets a little clearer. Thank you.
– lys
https://stackoverflow.com/a/6258849/825789
– bfavaretto
@Have you tried the above link solution? It seems plausible to me.
– user28595
@Articunol I tried the solution and presented a new error. I changed the question to a simple and executable example so you can see what is happening.
– lys
For me Oce completely changed the initial doubt, q was about using reserved word. I recommend you do not do this because you have completely disqualified your colleague’s answer below. If you have created a new problem, ask a new question.
– user28595
@Articunol But the variable name in JSON is default. Why would I use another nickname?
– lys
@Right Articunol. I will return the question to the state that the answer is acceptable.
– lys
I have already done that, the solution to the problem of using reserved words has been solved, so much that the error has stopped occurring it is not even? Now it’s another completely different problem
– user28595
@Articunol You’re right. Thank you.
– lys