Word reserved as variable name

-3

I'm building a program that integrates an online store with another program that controls the physical inventory 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's a reserved word in the Java language and I can not use it as a variable name, but I need to consume this JSON and make it an object.

I'm using the Gson library to help me make this "transformation". But you can not do without solving the variable name problem, and that's what I want help with. Is it possible to 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 Executing (Yes, the string is passed by HTTP request, this is just an example to illustrate how I am 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
    
asked by anonymous 23.11.2018 / 17:56

1 answer

3

You can do something like:

package com.javacreed.examples.gson.part1;

import com.google.gson.annotations.SerializedName;

public class Box {

  @SerializedName("w")
  private int width;

  @SerializedName("h")
  private int height;

  @SerializedName("d")
  private int depth;

  // Methods removed for brevity
}

Using the @SerializedName annotation to make the attribute name in your java object recognized by another in your JSON.

    
23.11.2018 / 19:27