Skip to content
Snippets Groups Projects
Commit b26aab05 authored by Marco Sciacovelli's avatar Marco Sciacovelli Committed by Alex
Browse files

Merge remote-tracking branch 'origin/master'

parents 1b99ac86 e9436a1c
Branches master
No related tags found
No related merge requests found
import jdk.security.jarsigner.JarSigner; import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue; import javax.json.JsonValue;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
...@@ -17,19 +19,13 @@ public class Event { ...@@ -17,19 +19,13 @@ public class Event {
public String startDate; public String startDate;
public String endDate; public String endDate;
public String[] categories; public String[] categories;
public GeoShape geometries; public GeoShape[] geometries;
public Event(){
}
public Event(String id, public Event(String id, HashMap<String, String> names, String description, String url, boolean isFree, String startDate, String endDate, String[] categories, GeoShape[] geometries) {
HashMap<String, String> names,
String description,
String url,
int duration,
boolean isFree,
String startDate,
String endDate,
String[] categories,
GeoShape geometries
) {
this.id = id; this.id = id;
this.names = names; this.names = names;
this.description = description; this.description = description;
...@@ -42,41 +38,28 @@ public class Event { ...@@ -42,41 +38,28 @@ public class Event {
this.geometries = geometries; this.geometries = geometries;
} }
public static Event parseJSON(JsonObject jobj) { public static ArrayList<Event> getEventsFromJSON(String json){
String j_id = jobj.getString("id"); ArrayList<Event> events = new ArrayList<>();
JsonObject j_names = jobj.getJsonObject("name"); JsonReader jsonReader = Json.createReader(new StringReader(json));
String j_description = jobj.getString("description"); Jsonb jsonb = JsonbBuilder.create();
JsonValue j_url = jobj.get("url"); for (JsonValue jsonValue : jsonReader.readArray()) {
boolean j_isFree = jobj.getBoolean("isFree"); events.add(jsonb.fromJson(jsonValue.toString(), Event.class));
String j_startDate = jobj.getString("start");
String j_endDate = jobj.getString("end");
JsonArray j_categories = jobj.getJsonArray("categories");
JsonArray j_geometries = jobj.getJsonArray("geometries");
HashMap<String, String> names = new HashMap<>();
for (String key : j_names.keySet()) {
names.put(key, j_names.getString(key));
} }
return events;
String[] categories = j_categories.stream().map(JsonValue::toString).toArray(String[]::new);
JsonArray arr = j_geometries.getJsonObject(0).getJsonArray("coordinates");
double[] coordinates1 = arr.getJsonArray(0).stream().mapToDouble(value -> Double.parseDouble(value.toString())).toArray();
double[] coordinates2 = arr.getJsonArray(0).stream().mapToDouble(value -> Double.parseDouble(value.toString())).toArray();
return new Event(j_id,names,j_description,j_url.toString(),0,j_isFree,j_startDate,j_endDate,categories,new GeoShape("",coordinates1,coordinates2));
} }
@Override @Override
public String toString() { public String toString() {
return "Event with the id: " + id + return "Event{" +
"id='" + id + '\'' +
", names=" + names + ", names=" + names +
", description=" + description + ", description='" + description + '\'' +
", url=" + url + ", url='" + url + '\'' +
", isFree=" + isFree + ", isFree=" + isFree +
", startDate=" + startDate + ", startDate='" + startDate + '\'' +
", endDate='" + endDate + ", endDate='" + endDate + '\'' +
", categories=" + Arrays.toString(categories) + ", categories=" + Arrays.toString(categories) +
", geoshape=" + geometries.toString(); ", geometries=" + Arrays.toString(geometries) +
'}';
} }
} }
\ No newline at end of file
...@@ -12,6 +12,11 @@ public class GeoShape { ...@@ -12,6 +12,11 @@ public class GeoShape {
this.coordinates[1] = coordinates2; this.coordinates[1] = coordinates2;
} }
public GeoShape() {
}
@Override @Override
public String toString() { public String toString() {
return "GeoShape{" + return "GeoShape{" +
...@@ -19,5 +24,9 @@ public class GeoShape { ...@@ -19,5 +24,9 @@ public class GeoShape {
", coordinates=" + Arrays.toString(coordinates) + ", coordinates=" + Arrays.toString(coordinates) +
'}'; '}';
} }
} }
import javax.json.Json; import javax.json.*;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import javax.json.bind.Jsonb; import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder; import javax.json.bind.JsonbBuilder;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
...@@ -12,8 +9,8 @@ public class Main { ...@@ -12,8 +9,8 @@ public class Main {
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
//Task 2 //Task 2
JsonReader reader = Json.createReader(new FileReader("src/main/resources/test.json")); JsonReader reader = Json.createReader(new FileReader("src/main/resources/testarray.json"));
JsonObject jobj = reader.readObject(); JsonArray jobj = reader.readArray();
System.out.println(jobj); System.out.println(jobj);
//Task 3 //Task 3
...@@ -55,10 +52,19 @@ public class Main { ...@@ -55,10 +52,19 @@ public class Main {
.build(); .build();
System.out.println(writing); System.out.println(writing);
//JSONB
Jsonb jsonb = JsonbBuilder.create();
//Task 4 //Task 4
GeoShape geoShape = new GeoShape("geoShapes", new double[]{1000d,223d,121d},new double[]{1000d,223d,121d} ); GeoShape geoShape = new GeoShape("geoShapes", new double[]{1000d,223d,121d},new double[]{1000d,223d,121d} );
Jsonb jsonb = JsonbBuilder.create();
System.out.println(jsonb.toJson(geoShape)); System.out.println(jsonb.toJson(geoShape));
//Task 6
System.out.println(Event.getEventsFromJSON(jobj.toString()));
} }
} }
[{
"type":"events",
"id":"ACE69C22C9DD41E6B74123BF64725F12",
"name":{
"deu":"Braunsbergbühne Lana: The Orchestra",
"ita":"Comedia: The orchestra"
},
"description":{
},
"url":null,
"duration":120,
"isFree":false,
"startDate":"2077-11-17T00: 00: 00.000Z",
"endDate":"2077-11-17T00: 00: 00.000Z",
"categories":[
"odh:theatre-cinema-and-performances",
"schema:TheaterEvent"
],
"geometries":[
{
"type":"geoShapes",
"coordinates":[
[
11.309652,
46.863848,
1298
],
[
11.309296,
46.861944,
1342
]
]
}
]
},
{
"type":"events",
"id":"ACE69C22C9DD41E6B74123BF64725412",
"name":{
"deu":"Braunsbergbühne Lana: The Orchestra",
"ita":"Comedia: The orchestra"
},
"description":{
},
"url":null,
"duration":120,
"isFree":false,
"startDate":"2077-11-17T00: 00: 00.000Z",
"endDate":"2077-11-17T00: 00: 00.000Z",
"categories":[
"odh:theatre-cinema-and-performances",
"schema:TheaterEvent"
],
"geometries":[
{
"type":"geoShapes",
"coordinates":[
[
11.309652,
46.863848,
1298
],
[
11.309296,
46.861944,
1342
]
]
}
]
}
]
\ No newline at end of file
{ {
"type":"events", "type":"events",
"id":"ACE69C22C9DD41E6B74123BF64725F12", "id":"ACE69C22C9DD41E6B74123BF64725F12",
"name":{ "name":{
"deu":"Braunsbergbühne Lana: The Orchestra", "deu":"Braunsbergbühne Lana: The Orchestra",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment