Files
ASDV-WebDev/Semester 3/Assignments/Json1/src/main/java/jason/JsonSupplier.java
2024-04-14 12:22:45 -05:00

160 lines
5.9 KiB
Java

package jason;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.json.JsonWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class JsonSupplier {
/**
* Creates a JsonObject by traversing the arrayList of LinkedHashMap
*
* @param suppliers ArrayList of LinkedHashMap. Each element of the
* arrayList is a row of suppliers table. The key of the LinkedHashMap is
* the attribute name and the value of the attribute Example ( One liked
* hash map is one row from the table) LinkedHashMap 1 : key: snumber,
* value: s1 key: sname, value: white key: status, value: 20 key: city,
* value London LinkedHashMap 2 : key: snumber, value: s2 key: sname, value:
* black key: status, value: 30 key: city, value Paris The value of the map
* @return JsonObject
*/
public static JsonObject createJsonObjectForSuppliers(ArrayList< LinkedHashMap<String, String>> suppliers) {
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
int counter = 1;
for (LinkedHashMap<String, String> supplier : suppliers)
{
// create an array of key-value pairs
JsonArrayBuilder arraySupplierBld = Json.createArrayBuilder();
// create each key-value pair as seperate object and add it to the array
Set<Map.Entry<String, String>> entrySet = supplier.entrySet();
for (Map.Entry<String, String> entry : entrySet)
{
arraySupplierBld.add(Json.createObjectBuilder().add(entry.getKey(),
entry.getValue()).build());
}
// add supplier-array to object
String objectID = "+" + (counter++);
jsonBuilder.add(objectID, arraySupplierBld);
}
JsonObject allSuppliersJsonObject = jsonBuilder.build();
return allSuppliersJsonObject;
}
/**
* Creates a JsonObject by reading the String data in JSON format
*
* @param jsonData Json format data
* @return JsonObject
*/
public static JsonObject createJsonObjectFromString(String jsonData) {
JsonReader jsonReader = Json.createReader(new StringReader(jsonData));
JsonObject o = jsonReader.readObject();
return o;
}
/**
* Creates a LinkedHashMap of 4 ENTRYies.Keys name: snumber, sname, status
* and city. Values of map are the parameters.
*
* @param snumber value of map
* @param sname value of map
* @param status value of map
* @param city value of map
* @return
*/
public static LinkedHashMap<String, String> createMapOfSupplier(
String snumber,
String sname,
String status,
String city
)
{
LinkedHashMap<String, String> mapSupplier
= new LinkedHashMap<String, String>();
mapSupplier.put("snumber", snumber);
mapSupplier.put("sname", sname);
mapSupplier.put("status", status);
mapSupplier.put("city", city);
return mapSupplier;
}
public static String getSuppliersJsonObject(){
ArrayList< LinkedHashMap<String, String>> suppliers = new ArrayList();
for (int i = 1; i <= 2; ++i)
{
LinkedHashMap<String, String> oneSupplierRowOfTable
= createMapOfSupplier(
"s1" + i,
"Johnson" + i,
Integer.toString(20 + i),
"london" + i);
suppliers.add(oneSupplierRowOfTable);
}
JsonObject j = createJsonObjectForSuppliers(suppliers);
return j.toString();
}
public static void main(String a[]) {
ArrayList< LinkedHashMap<String, String>> suppliers = new ArrayList<>();
for (int i = 1; i <= 2; ++i)
{
LinkedHashMap<String, String> oneSupplierRowOfTable
= createMapOfSupplier(
"s1" + i,
"Johnson" + i,
Integer.toString(20 + i),
"london" + i);
suppliers.add(oneSupplierRowOfTable);
}
JsonObject j = createJsonObjectForSuppliers(suppliers);
StringWriter strWtr = new StringWriter();
JsonWriter jsonWtr = Json.createWriter(strWtr);
jsonWtr.writeObject(j);
String s = strWtr.toString();
System.out.println(s);
jsonWtr.close();
/*
JSONobj.readJASONdataUsingParser(strWtr.toString());
JsonObject o = createJsonObjectFromString(strWtr.toString());
System.out.println("------------------------------------------------");
JSONobj.writeObjectModelToStream(o);
*/
}
public static void writeJsonToTextFile(JsonObject jo) throws FileNotFoundException {
String jsonString = Jason.getJsonString(jo);
PrintWriter output = new PrintWriter(new FileOutputStream("jason.json", true));
output.print(jsonString);
output.close();
}
public static JsonObject readFileIntoJson(String fileName) throws FileNotFoundException {
File file = new java.io.File(fileName);
Scanner input = new Scanner(file);
String joString = "";
while (!input.hasNext()) {
joString += input.next();
}
input.close();
JsonObject jo = createJsonObjectFromString(joString);
return jo;
}
}