YAML vs JSON (General storage in Spigot plugins)

Discussion in 'Spigot Plugin Development' started by Mee8a, May 15, 2019.

  1. I have been looking into some flat file storage options, and have been heavily looking into GSON, I was wondering are there any downsides to using JSON over YAML? I have read from multiple sources that JSON is faster than YAML and with a basic knowledge of Java using GSON is fairly simple so as of currently I am seeing no reason to use YAML over JSON.
     
    • Useful Useful x 1
  2. If you're gonna do config stuff, use YAML.
     
  3. Its mainly for data storage, config I know to go with YAML
     
  4. Tau

    Tau

    vs xml.
    Worth considering imo.
     
    • Optimistic Optimistic x 1
  5. Most plugins however use one of the following:
    YAML
    MySQL
    SQLite
     
  6. If you wanna go for flatfile storage, use JSON.
     
    • Agree Agree x 1
  7. Mmm possibly, I like how GSON works and the documentation is clear.

    I think I'm going to look into Redis rather than MySQL, I've looked at Jedis in the past and it seems that it wouldn't be too complex to integrate Jedis into a Spigot plugin.
    EDIT: And Redis is also a whole lot faster than MySQL
     
    #7 Mee8a, May 15, 2019
    Last edited: May 15, 2019
  8. JSON is very useful for mass data storage of arrays of Custom Data Types.

    If you created a Custom Class for some kind of awesome thing your plugin does, and then have an Array of those Instances, it can be quite a pain to store in YAML, having to write some FileManager to convert each to a new instance etc.

    YAML is great for static configs, where no dynamic information is ever really stored, or great when Admins/Owners need to change things.

    JSON is perfect for storing large datasets where you don't want to have Database connections, or the performance impact of making DB requests, or the complexity of having to write SQL queries for a local SQLite File based DB.

    In JSON, you can even use the org.json.simple library included in Java to Parse and "Stringify" classes.

    Here is an example converting a Custom Class fromJson, and toJson.
    Code (Java):

    public static AwesomeClass fromJson(String json) {
      try {
        JSONParser parser = new JSONParser();
        JSONObject awesomeObject = (JSONObject) parser.parse(json);
        UUID playerId = UUID.fromString(awesomeObject.get("owner").toString());
        int x = Integer.parseInt(awesomeObject.get("x").toString());
        int y = Integer.parseInt(awesomeObject.get("y").toString());
        int z = Integer.parseInt(awesomeObject.get("z").toString());
        World world = Bukkit.getWorld(awesomeObject.get("worldName").toString());
        AwesomeClass awesomeclass = new AwesomeClass(playerId, new Location(world, x, y, z));
        return awesomeclass;
      } catch (ParseException e) {}
      return null;
    }
     
    Code (Java):

    // Surpress Unchecked Map.put
    @SuppressWarnings("unchecked")
    public String toJson() {
      String owner = this.getOwner().toString();
      JSONObject obj = new JSONObject();
      obj.put("owner", owner);
      obj.put("x", this.getLocation().getBlockX());
      obj.put("y", this.getLocation().getBlockY());
      obj.put("z", this.getLocation().getBlockZ());
      obj.put("worldName", this.getLocation().getWorld().getName());
     
      return obj.toJSONString();
    }
     
    This allows you to convert a JSON Object into your class easily, and simply, without the need for messing with YAML trees - and if you decide in the future to want to show that Data Somewhere, you can easily take the file and since it's JSON it's well supported by pretty much every language out there.

    Both of these code snippets can go into your AwesomeClass and would enable to turn an instance into a JSON String, or from a JSON String, using AwesomeClass.fromJson(jsonString), allow you to get a new instance of your class.

    JSON has a lot of advantages, speaking from a programming background, and might also prevent people using your plugin from messing with data, since if it's a YAML file then most MC Admins will understand it quite easily and be able to change stuff which might have bad consequences for your plugin.
     
    • Useful Useful x 1
  9. json simple is not in java, it was inside spigot, in 1.14 md_5 removed it. gson is better
     
    • Like Like x 2
  10. Well that would be my mistake, thank you for correcting me!

    Gson is useful, but simple JSON is better in my experience for serialising classes into a single string where you have full control. I have used Gson in the past for Plugin Development, but didn't like it too much. Normally I stick to NodeJS though for development, which is easier to pretty much do everything :p
     
  11. Also if you use GSON, it would be super easy to switch over to using MongoDB or something similar for data storage, as their java driver uses gson objects itself.
     
  12. Wrong. In json simple you cast everything and use parsers, while with gson you just do jsonobject.get("something").getAsString(). Also can you serialize and deserialize full classes without need of doing tons of stuff with json simple? Its possible in gson.
     
  13. I'm not looking to get into an argument regarding my personal opinion on different Java Libraries. I would expect Gson to be able to do things in shorter amounts of code, since the former is called "JSON Simple". Also I would expect Gson or any other Json library to be able to do the same things, because their all libraries that have the same goal. It would be a pretty bad library if it couldn't convert to JSON and back...

    I'll say again, in my experience, I prefer simple json over Gson, because I have full control over every single step. I have used both in the past, I chose which I preferred. I just gave my opinion on which I thought was better and how to use it. It's not a fact that Gson is better, it's your own opinion of which you are completely entitled to hold.
     
  14. Redis is an flat in-memory key-value database. It has severe downsides compared to MySQL, one being that Redis should not be used as persistent data storage as it's simply not as robust as something like MySQL or MongoDB. While you can use Redis to persist data, it is not as robust as MySQL or MongoDB. Just my personal recommendation.

    @Sweepyoface mentioned more about this a week ago, which you can read here.

    If you want to persist Json objects, I'd suggest MongoDB. If you need to query things more specifically, you might want to use MySQL 8+ which supports a Json column type, which can be used in your queries; though at that point MongoDB should be more than fine. I use it myself for my own project and (in combination with Gson) and it works fantastic.

    I'm confused by this. What issues do you have with Gson? It works fantastic. Just use Gson to deserialize into and serialize from classes. In some rare occasions (when there's empty constructor for example) you may need to create a simple adapter class which implements 2 functions to help GSON serialize / deserialize things. There is nothing you cannot do with Gson. If you're looking for alternatives, you may also want to look into Jackson, which is also extremely commonly used amongst Java projects (such as Spring).
     
    #14 MrDienns, May 16, 2019
    Last edited: May 16, 2019
    • Agree Agree x 1
  15. Consider using Jackson, which is way better then GSON (faster & more extensive etc).
     
  16. I'm not sure exactly what I don't like about it, just that I preferred using simple json. I'll look into Jackson as well. I mostly develop in Typescript and C++, so the simplicity and granularity of Node is something that shaped how I develop things. I just felt better using simple json than Gson. Both do the same thing, sure it can't serialize an entire class in a single line of code, but it works well for how I use it. Personal opinion though /shrug

    This might be a bad thing to say here, but I'd rather not use Java at all, this is the only thing I use it for.
     
    • Like Like x 1
  17. Your thread title is a bit misleading. You're not comparing two different serialization types (they're not in fact different - YAML is a subset of JSON and any YAML parser should be able to load JSON just fine), you're comparing different java libraries based on how easy it is to serialize or deserealize data with them.

    If you need to dump some data in plain text, I think using Gson is the easiest way since it can pretty much do all the stuff for you.
     
    • Agree Agree x 1
  18. As already said json files are much better for storing large amounts of data, but YAML files are much more readable for humans.

    I think that JSON files are a very good alternative to MySQL databases.

    I wrote a library myself (link in my signature) that allows you to use files in both JSON and YAML, so if you know how to use bukkit-config, you know how to use the LightningStorage(name of my library). And then LightningStorage has some productivity boosters, like setPathPrefix, setDefault or getOrSetDefault, which make my daily work easier. Some of them are also used by professional Minecraft servers to make my work easier. So maybe LightningStorage is something for you:)

    MfG
    Leonhard|EsGibtKeineMitte
     
  19. There is honestly no practical difference that would make it clearly better to use one over another. Wherever JSON works, you can make YAML work as well and vice versa. It comes down to preference or whichever library you like using better.
     
  20. I have to admit I am bias against MongoDB due to an experience I had in the past with it, I swore to never touch it again after I had a hell of a time trying to use it with NodeJS, however having read this I may investigate the Java wrapper for it.

    Also, you mention using MySQL 8+, I could've sworn I read something on these forums the other day saying that Spigot has issues regarding newer versions of MySQL? Correct me if I am wrong I cannot find the source I am referring too.

    EDIT: Another advantage to using Redis over MySQL/MongoDB is the fact Jedis offers a threadsafe method of accessing the resource, JedisPools.

    I don't agree that JSON is a good alternative to MySQL at all. MySQL and JSON are completely different, JSON is a flat file storage type/storage notation, whereas MySQL is a relational database. JSON files could never be used across a network as they are stored locally and could not be accessed remotely, a MySQL database allows access remotely allowing many servers to connect and pull/push data. They serve completely different purposes.

    I also don't agree here. YAML is natively supported by Spigot and is clearly the common preference, and clearly from a post above it seems that md_5 is moving away from supporting JSON as he removed the JSON simple library in a recent 1.14 Spigot release. However, from my own research it seems that JSON is better suited to serialization.
     

Share This Page