Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🚀 The Shared Preferences API for Java

License

NotificationsYou must be signed in to change notification settings

omegaui/json-data-storage

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

DataStoragestorage =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

DataStoragestorage =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 builderform or caching multiple objects.

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

Example:

publicclassPreferences {publicstaticvoidsave(){HashMap<String,Integer>map =newHashMap<>();map.put("Simon",99);map.put("Alex",96);map.put("Sofia",89);DataStoragestorage =DataStorage.getStorage(".config","settings.json");storage.put("students",map);// Auto-SaveSystem.out.println(storage.query("students","Simon"));// Displays 99DataStoragestorage2 =DataStorage.getStorage(".config","settings.json");storage2.put("teachers",18);// Auto-Save// storage2 is the same storage object with no object redundancy 😎    }publicstaticvoidmain(String[]args) {save();    }}

Handling Multiple References

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

Example:

publicclassPreferences{publicvoidsaveTheme(){DataStoragestorage =DataStorage.getStorage(".config","settings.json");storage.put("theme","dark");    }publicvoidsaveUserInfo(){DataStoragestorage =DataStorage.getStorage(".config","settings.json");storage.put("username","iron-man");storage.put("password","!@#$%^&*^");    }}

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

its Like

storage1 =storage2 =the_shared_object

[8]ページ先頭

©2009-2026 Movatter.jp