1

I need to convert JSON data in my applet program. Am using a signed applet. I have signed gson-1.4.jar and java class files, but unable to convert the JSON data. Kindly advise me with samples.

Code:--

import com.google.gson.Gson;

import java.applet.*;

  public class MyApplet extends Applet implements Runnable {

        private Gson JSON;

        public void init() {
         JSON = new Gson();
        }

        public void start()
        {
        String json =  "{\"menu\": {\"id\": \"1\", \"value\": \"test\"} }";
        Gson gson = new Gson();
        System.out.println("start");  
        MenuWrapper m = gson.fromJson(json, MenuWrapper.class);
        System.out.println(m.getMenu().getId());
        System.out.println(m.getMenu().getValue());
        System.out.println("end");  
        }

        public void run(){
        }
}

class Menu {

    String id;
    String value;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String toString() {
        return String.format("id: %s, value: %d", id, value);
    }
}

class MenuWrapper {

    Menu menu;
    public Menu getMenu() { 
        return menu; 
    }
    public void setMenu(Menu m) { 
        menu = m; 
    }
}

No error found, output comes "start"

Kindly advise me how to solve this issue.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Maria
  • 297
  • 1
  • 5
  • 18
  • 2
    1) I see no reason why a [Gson](http://code.google.com/p/google-gson/) applet would need to be digitally signed. Why is your applet signed? 2) Don't include things like "It's urgent" in posts. It will not get help faster, and in fact is more likely to get you ignored. 3) *"No error found, output comes "start""* I understand the part before the comma, but what does *output comes "start"* mean? DYM you see 'start' in the output? Something else? 4) *"Kindly ... with samples."* SO is not a code generation factory. 5) Please check in the preview that code samples format as you'd expected. – Andrew Thompson Jan 10 '12 at 08:14
  • there are similar questions and answers here http://stackoverflow.com/questions/3850859/java-using-gson-in-an-applet-causes-securityexception – al. Mar 06 '12 at 09:27

2 Answers2

0

I would put aside the "signed jars" thing. In fact, I would put aside the whole Applet thing, and concentrate on you JSON conversion stuff. So, first of all try to run your code that makes an actual conversion of your JSON into MenuWrapper class. Run it and you'll see one of the following :

  • the MenuWrapper was created as expected, and everything is ok In this case it should be something wrong with configuration (you should observe exception in java console or in this case)
  • some kind of Exception is thrown during the conversion. In this case, please post it here, it won't be related to Applets but a question about JSON and using a GSON.

Hope this helps

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Thanks for your response. I got the following error. java.security.AccessControlException: access denied (java.lang.reflect.ReflectPermission suppressAccessChecks) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374) at java.security.AccessController.checkPermission(AccessController.java:546) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:74) at com.google.gson.MappedObjectConstructor.getNoArgsConstructor(MappedObjectConstructor.java:85) – Maria Jan 10 '12 at 11:16
  • This looks like your json converter dooesn't work properly. Probably you don't have a default constructor, or valid getter/setter. Its exactly the second case that I was talking about. You should fix the GSON stuff and only then it will run in the applet. – Mark Bramnik Jan 10 '12 at 11:57
  • Thanks. But if i run the program without using applet its working correctly using same json jar file. – Maria Jan 10 '12 at 12:57
0

There are similar questions here:

Java: Using Gson in an Applet causes SecurityException

It seems that the applet must be signed in order for the reflection in the deserialize library to work.

Community
  • 1
  • 1
al.
  • 484
  • 11
  • 30