Skip to content

Latest commit

 

History

History
53 lines (51 loc) · 1.83 KB

README.md

File metadata and controls

53 lines (51 loc) · 1.83 KB

JSON4Java

My implementation of methods handling JSON data in Java. The parser and lexer used here were appropriated and translated from Eaton Phil's awesome pj library found at https://github.com/eatonphil/pj/blob/master/pj/lexer.py. Pull requests fixing anything are always appreciated, as is testing and the reporting of issues. This library is not bug free by any means yet. If you don't want to spend time dealing with some downright weird bugs, you might want to find another solution to your problem.

Usage

Grab a JAR from the release page or clone this project and insert it into your project. To make a JSON object that contains your information, create a new instance of the JSONObject class like this:

JSONObject json = new JSONObject();

Adding information

json.addRule("name", "Mweya");
json.addRule("age", 20);
json.addRule("bored", true);

This even works for arrays!

int[] ages = {12, 23, 55};
json.addRule("ages", ages);

Documents and arrays of rules can be added in a similar fashion:

JSONObject location = newJSONObject();
location.addRule("lat", 31.445363);
location.addRule("long", 23.343567);
json.addRule("location", location);

Searching for rules

JSONRule rule = json.searchSelector("name");

Deleting rules

json.deleteRule("name");
json.removeRule("location");

Rules at a certain index can be deleted in a similar fashion:

json.deleteRule(2);

Writing to a file

json.toFile("file.json");

Reading from a file

⚠ Warning:

This feature is currently very buggy and should probably not be used yet. As always, pull requests would be appreciated!

json.fromFile("path/to/file.json");

If you need to save a JSONObject, try using serialization for now.