Best way to write/read items from JSON file?

Discussion in 'Plugin Development' started by yourselvs, May 25, 2016.

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

    yourselvs

    I am creating a plugin that will implement custom items, and I know how to deserialize them into a YAML file, but I don't know how to do so for a JSON file.
     
    Last edited: May 25, 2016
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to plugin development
    @yourselvs Why do you need it in JSON?
     
  3. Offline

    yourselvs

    @timtower I want to use a database called MongoDB, which stores objects in "Documents" that use standard JSON.

    As an additional question, is it just as easy to deserialize a whole inventory rather than just a single ItemStack?
     
    Last edited: May 25, 2016
  4. Offline

    timtower Administrator Administrator Moderator

    @yourselvs That the database uses json doesn't mean that you need to know how to write to it.
    You can probably just use string lists etc.
    I have seen a way to convert the ItemStack to a string for database use, not sure where and if I used that.
     
  5. Offline

    mythbusterma

    @yourselvs

    Why? Why not just use configuration files. They work fine.

    Also, if you're using Mongo, the best way to read them would be Mongo.
     
  6. Offline

    yourselvs

    @timtower @mythbusterma Mongo supports a feature where you can pass in a JSON string, and it will convert it do a document. My goal is being able to convert a player's inventory into JSON, and then pass it into the database. If there is a way to convert an inventory to/from a JSON string or JSON object, that would be great.
     
  7. Offline

    timtower Administrator Administrator Moderator

  8. Offline

    mythbusterma

    @yourselvs

    Alternatively, don't use Mongo. It's really just that simple.
     
  9. Offline

    yourselvs

    @mythbusterma Yeah, I'm finding a lot of difficulties working with mongo. For future reference, is this a possible way to read a list of items from the config?

    Code:
    private List<ItemStack> buildItems(String dungeon){
        List<ItemStack> items = new ArrayList<ItemStack>();
        List<Map<String, Object>> itemList = (List<Map<String, Object>>) plugin.getConfig().get(/* path */ v.rewards + "." + dungeon);
        for(Map<String, Object> itemMap : itemList)
            items.add(ItemStack.deserialize(itemMap));
        return items;
    }
     
    Last edited: Jun 1, 2016
  10. Offline

    Sethburster

    https://bukkit.org/threads/tut-bukkits-new-fileconfiguration-api-create-a-yaml-configuration.42775/This thread has alot of good config info in bukkit.
     
  11. Offline

    mythbusterma

    @yourselvs

    If you back them into an Inventory, and are using 1.8 or later, you can store and retrieve that Inventory directly, as it is ConfigurationSerializable
     
  12. Offline

    yourselvs

    @mythbusterma Thanks! So to confirm, is this how to read and write the inventory?

    Write:
    Code:
    plugin.getConfig().set("invetory", inventory);
    Read:
    Code:
    Inventory inventory = (Inventory) plugin.getConfig().get(inventory);
     
  13. Offline

    mythbusterma

    @yourselvs

    Assuming you're using 1.8+, that should work
     
  14. Offline

    yourselvs

    @mythbusterma

    Okay, thanks for your help.

    However, what if I wanted to just store a list of ItemStacks, not in an inventory. Would my previous method for reading and writing work? Sorry for the repetitive questions. I haven't found many definitive resources on this kind of thing.

    Write:
    Code:
    List<ItemStack> items = getItems();
    config.set(/* path */, items;
    Read:
    Code:
    private List<ItemStack> buildItems(String dungeon){
        List<ItemStack> items = new ArrayList<ItemStack>();
        List<Map<String, Object>> itemList = (List<Map<String, Object>>) plugin.getConfig().get(/* path */);
        for(Map<String, Object> itemMap : itemList)
            items.add(ItemStack.deserialize(itemMap));
        return items;
    }
     
    Last edited: Jun 11, 2016
  15. Offline

    mythbusterma

    @yourselvs

    I'm not sure, although I would assume ItemStack is also ConfigurationSerializable, and and can be stored.
     
Thread Status:
Not open for further replies.

Share This Page