简体   繁体   English

无法使用 gson.toJson(Element) 获取 JSON 字符串,{模块 java.base 不会“打开 java.util”到模块 com.google.gson]

[英]Can't get JSON String by using gson.toJson(Element), {module java.base does not "opens java.util" to module com.google.gson]

I am developing a minecraft-forge mod that collecst data from the game and converts the whole data as a JSON string.我正在开发一个 minecraft-forge mod,它从游戏中收集数据并将整个数据转换为 JSON 字符串。 The mod was working fine while my class had just these fields:当我的班级只有以下字段时,该模块运行良好:

public class MyInfo {

    public int ID;
    public float Health;
    public double X;
    public double Y;
    public double Z;

    public MyInfo(Player play) {
        ID = play.getId();
        Health = play.getHealth();
        X = play.getX();
        Y = play.getY();
        Z = play.getZ();
    }

Player is an instance of net.minecraft.world.entity.player.Player where I can retrieve some data I want. Playernet.minecraft.world.entity.player.Player的一个实例,我可以在其中检索一些我想要的数据。

It was working fine and the line where I make the JSON string was not facing any issue.它工作正常,我制作 JSON 字符串的行没有遇到任何问题。

Gson _Json = new GsonBuilder().setPrettyPrinting().create();
MyInfo pers = new MyInfo(player);
String Data = _Json.toJson(pers);

THE ISSUE:问题:

As soon as I add an ArrayList field inside my MyInfo class, as I will show now:在我的MyInfo类中添加一个ArrayList字段后,我现在将展示:

public class MyInfo {

    public int ID;
    public float Health;
    public double X;
    public double Y;
    public double Z;
    public ArrayList<MobEffectInstance> ActivePotionEffects;

    public MyInfo(Player play) {
        ID = play.getId();
        Health = play.getHealth();
        X = play.getX();
        Y = play.getY();
        Z = play.getZ();
        ActivePotionEffects = new ArrayList<>(play.getActiveEffects()); //the method .getActiveEffects returns a Collection<MobEffectIstance>
    }

The thread encounters an exception as soon as it reaches the line where i use _Json.toJson(pers) , the exception says:线程一到达我使用_Json.toJson(pers)的行就会遇到异常,异常说:

Exception in thread "Thread-6" java.lang.reflect.InaccessibleObjectException: 
Unable to make field private final java.util.concurrent.atomic.AtomicLong java.util.Random.seed accessible: module java.base does not "opens java.util" to module com.google.gson
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) 
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) 
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)        
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.getAdapter(Gson.java:489)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.toJson(Gson.java:727)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.toJsonTree(Gson.java:628)
at MC-BOOTSTRAP/com.google.gson@2.8.9/com.google.gson.Gson.toJsonTree(Gson.java:607)

If I instantiate an ArrayList<Integer> in the same class where _Json is instantiated and filling it thanks to FOR loop, and then using toJson() , the problem about accessing to java.lang for Gson doesn't appear如果我在实例化_Json的同一个类中实例化一个ArrayList<Integer>并通过 FOR 循环填充它,然后使用toJson() ,则不会出现有关访问java.langGson的问题

ie: IE:

ArrayList<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 10; i++) {
       numbers.add(i);
}
Gson _Json = new GsonBuilder().setPrettyPrinting().create();
MyInfo playr = new MyInfo(player); 
String Data = _Json.toJson(numbers); //<-- not passing *playr* anymore, but *numbers*  

EDIT:编辑:

I found a workaround, it mainly works for my purpose, and is to have the type MobEffectInstance decomposed into primitive data types (ie String or Integer ) and put these values ​​inside a collection我找到了一个解决方法,它主要是为了我的目的,并且是将类型MobEffectInstance分解为原始数据类型(即StringInteger )并将这些值放在一个集合中

public class MyInfo {

    public int ID;
    public float Health;
    public double X;
    public double Y;
    public double Z;
    public HashMap<String,Integer> ActivePotionEffects;

    public MyInfo(Player play) {
        ID = play.getId();
        Health = play.getHealth();
        X = play.getX();
        Y = play.getY();
        Z = play.getZ();
        ActivePotionEffects = new HashMap<>();
        ArrayList<MobEffectInstance> Temp = new ArrayList<>(play.getActiveEffects());
        for (MobEffectInstance mobEffectInstance : Temp) {
            ActivePotionEffects.put(mobEffectInstance.getDescriptionId(), mobEffectInstance.getDuration());
        }
    }

在初始化数组列表的地方,你有空的 <> 括号,它们应该有 MobEffectInstance 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使字段最终瞬态 java.lang.Class java.util.EnumSet.elementType 可访问:模块 java.base 不会“打开 java.util”到未命名 - Unable to make field final transient java.lang.Class java.util.EnumSet.elementType accessible: module java.base does not "opens java.util" to unnamed 为什么 java.util 可以从多个模块访问:<unnamed> , java.base</unnamed> - Why java.util is accessible from more than one module : <unnamed>, java.base Spring 无法在 JDK 17 上启动:模块 java.base 没有“打开 java.lang”到未命名的模块 - Spring Boot can't start on JDK 17 : module java.base does not "opens java.lang" to unnamed module 如何使用com.google.gson类创建json对象 - How to create json object using com.google.gson class 使用com.google.gson进行数据输入 - Data entry using com.google.gson java maven 导入问题:错误:com.google.gson 包不存在 - java maven import issue: error: package com.google.gson does not exist Google gson.toJson(List)以字符串而不是数组的形式返回响应 - Google gson.toJson(List) returning response as string instead of array 如何修复 Android Studio 中的“module java.base does not "opens java.io" to unnamed module'' 错误? - How to fix the ''module java.base does not "opens java.io" to unnamed module '' error in Android Studio? 将 Key 值添加到从 GSON.toJSON 生成的 Json String - Add a Key value to Json String generated from GSON.toJSON 为什么Google Gson.toJson会丢失数据 - Why Google Gson.toJson loses data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM