GSON on Google App Engine throws a Security Exception

后端 未结 4 632
忘掉有多难
忘掉有多难 2020-12-18 00:24

I am trying to convert an object into JSON using the GSON library on Google App Engine. For some reason, it throws this exception and I don\'t understand how to solve this.

4条回答
  •  盖世英雄少女心
    2020-12-18 01:17

    If App Engine does not support Reflection, then we are pretty much left to write our own toJSON method. This can be done as follows (not a big deal but someone might find it useful):

    public SampleObject {
    
      //...
    
      /**
       * Convert this object to a JSON object for representation
       */
      public JSONObject toJSON() {
        try {
           JSONObject jsonobj = new JSONObject();
           jsonobj.put("id", this.id);
           jsonobj.put("name", this.name);
           return jsonobj;
        } catch(Exception e) {
           return null;
        }
      }
    }
    

    Then, you can use a toString method on this object to print out the JSON representation. Not the best I agree but some workaround for now.

提交回复
热议问题