Git Product home page Git Product logo

json-data-storage's Introduction

๐Ÿš€ The Shared Preferences API for Java ๐Ÿš€

json-data-storage

A tiny json based application data utility for Java.

Features

  • No Redundancy
  • Easily Handles multiple references of same Storage
  • Real-Time Write i.e Automatically Saves file on changes
  • Auto constructs the entire path

Usage

All we need is a DataStorage object.

Basic Call

DataStorage storage = DataStorage.getStorage("settings"); // auto adds ".json suffix", the call is equivalent to "settings.json"
storage.put("theme", "dark");
// When put(String, Object) is called 
// The above line will finish off writing settings.json in the current working directory

Advanced Call

DataStorage storage = DataStorage.getStorage(".config", "settings.json");
storage.put("theme", "dark");
// When put(String, Object) is called 
// The above line will finish off writing settings.json in the .config working directory

Note: DataStorage.getStorage() auto-constructs the path if it doesn't already exist

Querying nested data

DataStorage also allows you to directly access nested objects without needing to use a builder form or caching multiple objects.

To do so, you just need to call DataStorage.query() that requires the ordered arrangement of the hierarchy.

Example:

public class Preferences {
    public static void save(){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("Simon", 99);
        map.put("Alex", 96);
        map.put("Sofia", 89);

        DataStorage storage = DataStorage.getStorage(".config", "settings.json");
        storage.put("students", map); // Auto-Save

        System.out.println(storage.query("students", "Simon"));
        // Displays 99


        DataStorage storage2 = DataStorage.getStorage(".config", "settings.json");
        storage2.put("teachers", 18); // Auto-Save
        // storage2 is the same storage object with no object redundancy ๐Ÿ˜Ž

    }

    public static void main(String[] args) {
        save();
    }
}

Handling Multiple References

Now, if there are multiple cases requesting the reference to the same DataStorage object, so instead of creating multiple instances pointing to the same storage location, the same instance is referenced at every call.

Example:

public class Preferences{
    public void saveTheme(){
        DataStorage storage = DataStorage.getStorage(".config", "settings.json");
        storage.put("theme", "dark");
    }
    
    public void saveUserInfo(){
        DataStorage storage = DataStorage.getStorage(".config", "settings.json");
        storage.put("username", "iron-man");
        storage.put("password", "!@#$%^&*^");
    }
}

Although, the storage objects request the same json file, so instead of creating multiple references of the class, a single instance is shared to both.

its Like

storage1 = storage2 = the_shared_object

json-data-storage's People

Contributors

omegaui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.