Resource Fast (and useless) Json utility class

Discussion in 'Spigot Plugin Development' started by crazyhoorse961, Apr 19, 2017.

  1. Hello!
    I've quite never understand how to use Json, after 2 hours of googling and working i made 2 methods for parse a json string and encode one for then storing and retrieving data as Json is a 'language' used for storing data and you can finally stop using YAML for storing data.
    These are the methods (they require the org.json package, find it on maven by clicking here)!
    Code (Java):
        public static Object decode(String json, String string, String jsonObject){
            JSONObject jso = new JSONObject(json);
            Object element;
            if(jsonObject == null){
                element = jso.get(string);
            }else{
                element = jso.getJSONObject(jsonObject).get(string);
            }
            return element;
        }
       
        public static String encode(Map<String,Object> values){
            JSONObject jso = new JSONObject();
            for(String str : values.keySet()){
                jso.put(str, values.get(str));
            }
           
            return jso.toString();
        }
    Here a test:
    Code (Java):
        public static void main(String[] args) {
            Map<String,Object> values = new HashMap<String,Object>();
            values.put("kebab", 12);
            values.put("pizza", 15);
            String encoded = encode(values);
            System.out.println(encoded);
            Object decoded = decode(encoded, "pizza", null);
            System.out.println(decoded);
        }
    And the output of the test:
    Code (Text):
    {"pizza":15,"kebab":12}
    15
     
    Disclaimer
    I wrote this resource as i wasn't understanding well how to use json, so i tried writing an utility class for helping me. I'm sure this can be improved, if you want to, tell me below in the comments.
    Hope it could help some new developer, i feel that the code is understandable and you don't need comments.
     
  2. you do realise that Gson is already provided by CraftBukkit, right :p?
     
    • Agree Agree x 5
  3. electronicboy

    IRC Staff

    ^ Because my browser crashed I missed the shot, but gson is a really handy library that means that you don't have to care about parsing json manually, define a class, throw it through gson, get pretty json. Worse case scenario you get the odd few edge cases where you actually do have to write a custom de/ serializer for a few bukkit objects (e.g. Location), but other than that it is pretty seamless.
     
    • Agree Agree x 1
  4. Yes, but for shortening (just a bit) the work.
    I'm aware it isn't really useful
     
  5. What's the method to parse a random object?
     
  6. The from/toJson methods which accept Object
     
    • Agree Agree x 1
  7. FrostedSnowman

    Resource Staff

    yup. Gson is damn awesome! just remember to register adapters when needed ( i went through frustration when i didnt know this)
     
    • Agree Agree x 1