Java: Using Gson in an Applet causes SecurityException

前端 未结 3 1259
醉酒成梦
醉酒成梦 2021-01-21 18:27

I\'m trying to use Google Gson in my Java Applet, but when I do I get

Exception in thread \"Thread-19\" java.security.AccessControlException: access deni

3条回答
  •  佛祖请我去吃肉
    2021-01-21 19:16

    I solved this problem using a custom deserializer.

    I had a class with two members, a timestamp and a list of doubles. This is the code that worked for me.

    GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(TimestampedValueList.class, new JsonDeserializer() {
            @Override
            public TimestampedValueList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                long timestampMs = json.getAsJsonObject().get("timestampMs").getAsLong();
                double[] valueList = context.deserialize(json.getAsJsonObject().get("valueList"), double[].class);
                return new TimestampedValueList(timestampMs, valueList);
            }
        });
    gson = gsonBuilder.create();
    

    Hope this can help anyone!

提交回复
热议问题