Util MyJsonUtil (Gson) (Rev. 1)

Discussion in 'Resources' started by MrAwellstein, Jan 23, 2015.

Thread Status:
Not open for further replies.
  1. Offline


    I feel like this can be useful for some people, and its an easy way to create custom Json objects using the Gson library. If anyone wants to add anything to it, feel free to. The purpose of this is to get a Json object without having to know its class, which allows for a Dev to allow easier Runtime parsing.

    MyJsonUtil.java (open)

    import com.google.gson.Gson;
    import com.google.gson.JsonSyntaxException;
    public class MyJsonUtils {
        public static class JsonObject {
            public String name = this.getClass().getSimpleName();
            public String toString() {return new Gson().toJson(this);}
        public static Object fromString(String s) throws JsonSyntaxException, ClassNotFoundException {return new Gson().fromJson(s, findClass(new Gson().fromJson(s, GSONObjectFinder.class).name));}
        private static Class<?> findClass(String name) throws ClassNotFoundException {return Class.forName(CopyOfMyJsonUtils.class.getPackage().toString().replaceFirst("package ", "")+"."+name);}
        private static class GSONObjectFinder {public String name;}

    How to Use:
    Place this class in whatever package that host your JsonObjects (This uses reflection to make the Json objects smaller). Then extend MyJsonUtils.JsonObject in the class that you want to turn into Json. You can add any methods and fields except the field String name, as it is reserved for the classname (don't ask why I named it name instead of classname or something that no one would use). After that, congratulations, you have created a JsonObject. To turn it into a Json String, call the method JsonObject.toString() and to turn it back call MyJsonUtils.fromString(String s).

    JsonLoginObject (open)

    Serialization String:
    public class JsonLoginObject extends MyJsonUtils.JsonObject{
        public String username;
        public String password;
        public JsonLoginObject(String u, String p) {
            username = u;
            password = p;

    JsonByteObject (open)

    Serialization String:
    {"bytes":[72,101,108,108,111,44,32,87,111,114,108,100,33],"data":"Simple Message","name":"JsonByteObject"}
    public class JsonByteObject {
        public byte[] bytes;
        public String data;
        public JsonByteObject(byte[] b, String d){
            bytes = b;
            data = d;

    Required Libs:
    Last edited: Jan 23, 2015
    ChipDev likes this.
  2. Offline


    @MrAwellstein did you know that Gson comes with CraftBukkit? It's located in the package org.bukkit.craftbukkit.libs.com.google.gson. You should use that to keep your plugin small ;D
    While I don't see any purpose at all for this it's not completly useless like the ofter ressources I have seen this week.
    This could be useful for people who don't want/can deal mit JSON/Gson.
  3. Offline


    I liked the post becuz of your profile pic.
    And, cool post :D
  4. Offline


    Useless ?
  5. Offline


    I don't wanted to be the rude boy saying that but yeah, for people who seriously dealt with java for over a week will find not so much use in this ;D
  6. Offline


    Did you read
  7. Offline


    I don't see much point to this when Gson has a to and from method built into it. No reason to make a class that you extend from all other classes. Also, you're creating a new Gson instance every single time you serialize the object, which seems a bit inefficient. Also, you don't address many common objects in Bukkit that will cause Gson to freak out and error. Things like ItemStacks, Locations, and PotionEffects all don't work with Gson's default serializer methods, which means you should be making TypeAdapaters for those objects.

    I think instead you should make a static class that has one-two Gson instances for your needs and simply use it from that.

    I created a UTIL before and posted it in this section called GsonFactory, a static class that has a pretty printing and a compact printing instance of Gson. They also have TypeAdapters for a few objects that will make their use much much easier.
    MrAwellstein likes this.
  8. Offline


    1: nothing I can do about Gson not having static methods when it really should have them. I know its awful memory management but yeah there isnt much I can do, except create a static Gson object and use it (which I do (I might have forgot to apply that to this release)).
    2: you can store yml or even byte[] in Gson, but thats not the point of this util.
    3: This is a proof of concept of finding classes in the same package without having to know them pre-parse, meaning that you dont have to have as many parsers.
    4: The main use of this was to communicate from bukkit to my client, allowing an easy way of serialization without much dev. work, and without having to use Java's default Serialization which seems to be bloated.
    5: I'll check out your Util.

    Not at all.

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
    Last edited by a moderator: Jan 24, 2015
Thread Status:
Not open for further replies.

Share This Page