Skip to content

Documentation

João Dias edited this page May 5, 2024 · 4 revisions

This page contains the JSONutils Documentation.

Table of Contents

Class
JSON
JSONBuilder
JSONReader
Message

JSON

The base JSON class contains all basic functions to deal with multi-lined JSONs. Such as creating new files, values in it, reading the JSON and getting all JSON's objects as one HashMap.

⚠️ Notice: All code examples will have the JSON class instance named as json, it is not mandatory to use that name in your code.

Constructor

To create a JSON class instance, it only needs a single input, the JSON desired file.

As File

File jFile = new File("example.json"); // creates a Java File instance
JSON jInstance = new JSON(jFile); // creates a new JSON instance with the example file

As Directory

JSON jInstance = new JSON("/example/dir/file.json"); // creates a new JSON instance using directory as String

Parse a JSON file

JSONutils automatically searches for the file indicated at the class constructor and parses it if the file exists. But you can always parse it manually, using the read() function:

JSON parsedJSON = json.read();

What this funcion does? It basically parses the multi-lined JSON file to a HashMap, for easier management of objects. You can retrieve a parsed JSON object in the Hash Map using the getValue(String).

Getting objects from HashMap

You have two ways of getting a parsed JSON object of the Hash Map, you can either use the getValue(String) function of the JSON class, or you can manually get the object in the HashMap using the toMap() function, and getting the key's value using the Java HashMap methods.

Using getValue()

json.getValue("example"); //returns the value of the "example" object if exists in HashMap

Using Java's native HashMap

Map<String, Object> jsonMap = json.toMap();
jsonMap.get("example"); //returns the value of the "example" object using the hashmap `get()` function

Built-in JSONReader instance

The JSON class has a built-in JSONReader class instance, meaning you don't need to create another instance to manipulate the file parsing. You can get this instance using the getReader() function:

JSONReader jReader = json.getReader();

Built-in JSONBuilder instance

Just like the Built-in JSONReader instance, there's a built-in instance of JSONBuilder in the JSON class. You can use that for building and manipulating the JSON file creation, see JSONBuilder section for more information.

JSONBuilder jBuilder = json.getBuilder();

Adding new object to JSON

You can do that by using the newVariable(String, Object) method, this adds the desired variable to a JSON objects queue, for later on building the final result using the write() function.

json.newVariable("stringObjectName", "ObjectValue");
json.newVariable("intObjectName", 123);
json.newVariable("booleanObjectName", true);

Writing a JSON file

It's finally the time to build your JSON file with your queued objects! You can put all the objects that were in queue to a file using the JSON class' write() function. It writes you a JSON with all the queued objects in it. Plus, it automatically reads the file for you after it finished writing. Nice!

/* adding example objects to JSON queue */
json.newVariable("isLibraryNice", true);
json.newVariable("launchedIn", "Feb 2, 2024");

json.write();

JSONBuilder

⚠️ Notice: this is not ready yet.

JSONReader

⚠️ Notice: this is not ready yet.

Message

⚠️ Notice: this is not ready yet.