Json write

Discussion in 'Spigot Plugin Development' started by Stevie212, May 3, 2017.

  1. Hey guys, I am having problems with writing objects from a class to a Json. Here is my code

    Code (Text):
    public void Write(){
        Person p = new Person();
        Gson json = new Gson();

        try {
            json.toJson(Person, new FileWriter(getDataFolder() + "person.data"));
        } catch (JsonIOException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

       
    }
    it gives me this error

    Code (Text):
    Caused by: java.lang.NoClassDefFoundError: com/google/gson/JsonSyntaxException
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_121]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[spigot.jar:git-Spigot-952179b-0cf233d]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-952179b-0cf233d]
            ... 14 more
    Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonSyntaxException
            at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot.jar:git-Spigot-952179b-0cf233d]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-952179b-0cf233d]
    is there a way so that I could fix it? Thank you
     
  2. Mas

    Mas

    The gson library isn't available at runtime, hence why the class cannot be found.
     
  3. First of all you are giving an Class (Not object) as argument and is there a method that uses a Filewriter as second argument?
     
  4. Choco

    Moderator

    Sure it is. Spigot shades GSON in the server jar
     
  5. Mas

    Mas

    Well that class is definitely contained within Spigot, so I'm not sure what's gone wrong.
    [​IMG]
     
  6. So what is the problem then?
     
  7. That's wired. In my spigot.jar don't have Json
     
  8. Do you have like PaperSpigot or something? Make sure to download from this site
     
    • Agree Agree x 1
  9. What kinda spigot are you using....
     
  10. spigot.1.10.2

    Edit: Ok guys I found the problem. I was running my minecraft server on 1.8.1 and spigot 1.10.2 was imported into eclipse. So I think 1.8.1 do not support gson. Version 1.10 and up supports Gson. But is there a way for 1.8.1 to support to gson?
     
    #11 Stevie212, May 3, 2017
    Last edited: May 3, 2017
  11. You can shade the gson library yourself into your plugin (however I don't think 1.8+ servers would appreciate this as it makes your plugin a lot bigger) using maven (look it up)
     
    • Agree Agree x 1
  12. Am having another problem. When I write the objects into the File nothing shows up but on Sytem.out.PrintIn this shows up

    {"age":14,"name":"build"}

    Code (Text):
        public void Write() {
            Person pt = new Person();
            Gson json = new Gson();
            String json1 = json.toJson(pt);

            try {
                json.toJson(pt, new FileWriter(getDataFolder() + "/person.data"));
             
            } catch (JsonIOException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(json1);
        }

    Code (Text):
    public class Person
    {
      private String name;
      private int age;
     
     
  13. Does it throw any exceptions?
     

  14. Nope it does not.
     
  15. Try Filewriter(new File(getDataFolder(), "player.data");
     
  16. Code (Java):

    try (FileWriter writer = new FileWriter(file)) {
     writer.write(gson.toJson(object);
    }
     
     
  17. Thanks. I am having another problem with reading the objects from the class. I use json.fromJson(file, Person.class); but it still doesn't work...
     
  18. Are you registering your serializer ?
     
  19. What do u mean when u say registering My serializer ?
     
    #20 Stevie212, May 7, 2017
    Last edited: May 8, 2017