Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson.toJson()"Exception java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits"

Tags:

java

json

gson

I am trying to make a JSON file to use it as a database. my classes are: 1. Sheep: includes some info about every sheep 2. Farm: includes an array of Sheeps 3. DB: here I am trying to parse the Farm object into JSON

I have already tried to add "transient" to all the fields from the date type but it did not work.

to give you a better idea of what I am trying to do: it is a farm manager where the user can store the data about his sheeps

public class Sheep implements Serializable {

    @SerializedName("localId")
    @Expose
    private int localId;
    @SerializedName("globalId")
    @Expose
    private int globalId;
    @SerializedName("birthDate")
    @Expose
    private transient Date birthDate = null;
    @SerializedName("sellingDate")
    @Expose
    private transient Date sellingDate = null;
    @SerializedName("deathDate")
    @Expose
    private transient Date deathDate = null;
    @SerializedName("lastpregnancyDate")
    @Expose
    private transient Date lastpregnancyDate = null;
    @SerializedName("lastDoctorCheck")
    @Expose
    private transient Date lastDoctorCheck = null;
    @SerializedName("alive")
    @Expose
    private boolean alive;
    @SerializedName("sold")
    @Expose
    private boolean sold;
    @SerializedName("pregnant")
    @Expose
    private boolean pregnant;
    @SerializedName("mother")
    @Expose
    private Sheep mother = null;
    @SerializedName("father")
    @Expose
    private Sheep father = null;
    @SerializedName("allDoctorChecks")
    @Expose
    private transient List<Date> allDoctorChecks = null;
    @SerializedName("allpregnancies")
    @Expose
    private transient List<Date> allpregnancies = null;
    @SerializedName("children")
    @Expose
    private List<Sheep> children = null;
    @SerializedName("allHusbands")
    @Expose
    private List<Sheep> allHusbands = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
}
public class Farm implements Serializable {

    @SerializedName("allSheeps")
    @Expose
    private List<Sheep> allSheeps = null;
}
public class DataBase {
    private static Farm farm;

public static void main(String[] args){
        ...

        Gson g = new GsonBuilder().setPrettyPrinting().create();
**/*40*/  String strGsn = g.toJson(farm);/*this is line 40*/** 

        FileWriter writer = null;
        try{
            writer = new FileWriter("farm.json");
            writer.write(strGsn);
        }catch (Exception e){
            System.out.println(e.fillInStackTrace());
        }finally {
            if(writer != null)
                try{
                    writer.close();
                }catch (Exception e){
                    System.out.println(e.fillInStackTrace());
                }
        }


    }

}
"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\lib\idea_rt.jar=53834:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\saleh\Desktop\farm\out\production\farm;C:\Users\saleh\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar com.company.DB.DataBase
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.gson.internal.reflect.UnsafeReflectionAccessor (file:/C:/Users/saleh/.m2/repository/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar) to field java.text.SimpleDateFormat.serialVersionOnStream
WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.reflect.UnsafeReflectionAccessor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.Gson.toJson(Gson.java:704)
    at com.google.gson.Gson.toJson(Gson.java:683)
    at com.google.gson.Gson.toJson(Gson.java:638)
    at com.google.gson.Gson.toJson(Gson.java:618)
    at com.company.DB.DataBase.main(DataBase.java:40)
like image 696
Saleh Abusalah Avatar asked Mar 08 '26 00:03

Saleh Abusalah


1 Answers

The exception

java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits

is due to the field sdf of Sheep is being serialized. It is questionable on why SimpleDateFormat can not be serialized to json, but to solve the problem, since you are using @Expose annotation to control which field to be serialized, change the line
Gson g = new GsonBuilder().setPrettyPrinting().create();
to
Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
in order to avoid sdf to be serialized.

like image 168
samabcde Avatar answered Mar 09 '26 14:03

samabcde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!