Skip to content
Snippets Groups Projects
Commit b4afa4cd authored by Alex's avatar Alex
Browse files

Merge remote-tracking branch 'origin/master'

Task 8
parents b26aab05 54fce33e
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
......
......@@ -11,7 +11,7 @@ import java.util.HashMap;
public class Event {
public static final String TYPE = "events";
public String id;
public HashMap<String,String> names;
public HashMap<String, String> names;
public String description;
public String url;
public int duration;
......@@ -21,11 +21,12 @@ public class Event {
public String[] categories;
public GeoShape[] geometries;
public Event(){
public Event() {
}
public Event(String id, HashMap<String, String> names, String description, String url, boolean isFree, String startDate, String endDate, String[] categories, GeoShape[] geometries) {
public Event(String id, HashMap<String, String> names, String description, String url, int duration, boolean isFree,
String startDate, String endDate, String[] categories, GeoShape[] geometries) {
this.id = id;
this.names = names;
this.description = description;
......@@ -38,7 +39,7 @@ public class Event {
this.geometries = geometries;
}
public static ArrayList<Event> getEventsFromJSON(String json){
public static ArrayList<Event> getEventsFromJSON(String json) {
ArrayList<Event> events = new ArrayList<>();
JsonReader jsonReader = Json.createReader(new StringReader(json));
Jsonb jsonb = JsonbBuilder.create();
......@@ -48,18 +49,22 @@ public class Event {
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
public String toString() {
return "Event{" +
"id='" + id + '\'' +
", names=" + names +
", description='" + description + '\'' +
", url='" + url + '\'' +
", isFree=" + isFree +
", startDate='" + startDate + '\'' +
", endDate='" + endDate + '\'' +
", categories=" + Arrays.toString(categories) +
", geometries=" + Arrays.toString(geometries) +
'}';
return "Event{" + "id='" + id + '\'' + ", names=" + names + ", description='" + description + '\'' + ", url='"
+ url + '\'' + ", isFree=" + isFree + ", startDate='" + startDate + '\'' + ", endDate='" + endDate
+ '\'' + ", categories=" + Arrays.toString(categories) + ", geometries=" + Arrays.toString(geometries)
+ '}';
}
}
\ No newline at end of file
......@@ -5,28 +5,18 @@ public class GeoShape {
public String type;
public double[][] coordinates;
public GeoShape(String type, double[] coordinates1,double[] coordinates2) {
public GeoShape(String type, double[] coordinates1, double[] coordinates2) {
this.type = type;
this.coordinates = new double[2][];
this.coordinates[0] = coordinates1;
this.coordinates[1] = coordinates2;
}
public GeoShape() {
}
@Override
public String toString() {
return "GeoShape{" +
"type='" + type + '\'' +
", coordinates=" + Arrays.toString(coordinates) +
'}';
return "GeoShape{" + "type='" + type + '\'' + ", coordinates=" + Arrays.toString(coordinates) + '}';
}
}
......@@ -2,68 +2,54 @@
import javax.json.*;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
public static void main(String[] args) throws IOException {
//Task 2
JsonReader reader = Json.createReader(new FileReader("src/main/resources/testarray.json"));
JsonArray jobj = reader.readArray();
// Task 2
JsonReader reader = Json.createReader(new FileReader("src/main/resources/test.json"));
JsonObject jobj = reader.readObject();
System.out.println(jobj);
//Task 3
JsonObject writing = Json.createObjectBuilder()
.add("type", "events")
// Task 3
JsonObject writing = Json.createObjectBuilder().add("type", "events")
.add("id", "ACE69C22C9DD41E6B74123BF64725F12")
.add("name",
Json.createObjectBuilder()
.add("due","Braunsbergbühne Lana: The Orchestra")
.add("ita","Comedia: The orchestra").build())
.add("description",
Json.createObjectBuilder().build())
.add("url", JsonValue.NULL)
.add("duration",120)
.add("isFree",Boolean.FALSE)
.add("startDate","2077-11-17T00: 00: 00.000Z")
.add("endDate","2077-11-17T00: 00: 00.000Z")
Json.createObjectBuilder().add("due", "Braunsbergbühne Lana: The Orchestra")
.add("ita", "Comedia: The orchestra").build())
.add("description", Json.createObjectBuilder().build()).add("url", JsonValue.NULL).add("duration", 120)
.add("isFree", Boolean.FALSE).add("startDate", "2077-11-17T00: 00: 00.000Z")
.add("endDate", "2077-11-17T00: 00: 00.000Z")
.add("categories",
Json.createArrayBuilder()
.add("odh:theatre-cinema-and-performances")
.add("schema:TheaterEvent")
.build()
)
.add("geometries",Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("type","geoShapes")
.add("coordinates",Json.createArrayBuilder()
.add(Json.createArrayBuilder()
.add(11.309652)
.add(46.863848)
.add(1298)
.build())
.add(Json.createArrayBuilder()
.add(11.309296)
.add(46.861944)
.add(1342)
.build()
).build()).build()).build())
Json.createArrayBuilder().add("odh:theatre-cinema-and-performances").add("schema:TheaterEvent")
.build())
.add("geometries", Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "geoShapes")
.add("coordinates", Json.createArrayBuilder()
.add(Json.createArrayBuilder().add(11.309652).add(46.863848).add(1298).build())
.add(Json.createArrayBuilder().add(11.309296).add(46.861944).add(1342).build()).build())
.build()).build())
.build();
System.out.println(writing);
//JSONB
// JSONB
Jsonb jsonb = JsonbBuilder.create();
//Task 4
GeoShape geoShape = new GeoShape("geoShapes", new double[]{1000d,223d,121d},new double[]{1000d,223d,121d} );
// Task 4
GeoShape geoShape = new GeoShape("geoShapes", new double[] { 1000d, 223d, 121d },
new double[] { 1000d, 223d, 121d });
System.out.println(jsonb.toJson(geoShape));
//Task 6
System.out.println(Event.getEventsFromJSON(jobj.toString()));
// Task 6
Event eve1 = jsonb.fromJson(jobj.toString(), Event.class);
System.out.println(eve1);
// Task 8
System.out.println(Event.getEventsFromJSON(Files.readString(Path.of("src/main/resources/testarray.json"))));
}
......
......@@ -4,12 +4,15 @@ import java.util.HashMap;
public class Task5 {
public static void main(String[] args) {
GeoShape geometries = new GeoShape("geoShapes", new double[]{1000d,223d,121d},new double[]{1000d,223d,121d});
GeoShape[] geometries = new GeoShape[] {
new GeoShape("geoShapes", new double[] { 1000d, 223d, 121d }, new double[] { 1000d, 223d, 121d }) };
HashMap<String, String> names = new HashMap<>();
names.put("deu","Braunsbergbühne Lana: The Orchestra");
names.put("ita","Comedia: The orchestra");
names.put("deu", "Braunsbergbühne Lana: The Orchestra");
names.put("ita", "Comedia: The orchestra");
Event event = new Event("ACE69C22C9DD41E6B74123BF64725F12", names, "","",120,false,"2077-11-17T00: 00: 00.000Z","2077-11-17T00: 00: 00.000Z", new String[]{"odh:theatre-cinema-and-performances","schema:TheaterEvent"}, geometries);
Event event = new Event("ACE69C22C9DD41E6B74123BF64725F12", names, "", "", 120, false,
"2077-11-17T00: 00: 00.000Z", "2077-11-17T00: 00: 00.000Z",
new String[] { "odh:theatre-cinema-and-performances", "schema:TheaterEvent" }, geometries);
Jsonb json = JsonbBuilder.create();
String result = json.toJson(event);
......
......@@ -8,12 +8,15 @@ public class Task7 {
public static void main(String[] args) {
Set<Event> events = new HashSet<>();
GeoShape geometries = new GeoShape("geoShapes", new double[]{1000d,223d,121d},new double[]{1000d,223d,121d});
GeoShape[] geometries = new GeoShape[] {
new GeoShape("geoShapes", new double[] { 1000d, 223d, 121d }, new double[] { 1000d, 223d, 121d }) };
HashMap<String, String> names = new HashMap<>();
names.put("deu","Braunsbergbühne Lana: The Orchestra");
names.put("ita","Comedia: The orchestra");
names.put("deu", "Braunsbergbühne Lana: The Orchestra");
names.put("ita", "Comedia: The orchestra");
Event event = new Event("ACE69C22C9DD41E6B74123BF64725F12", names, "","",120,false,"2077-11-17T00: 00: 00.000Z","2077-11-17T00: 00: 00.000Z", new String[]{"odh:theatre-cinema-and-performances","schema:TheaterEvent"}, geometries);
Event event = new Event("ACE69C22C9DD41E6B74123BF64725F12", names, "", "", 120, false,
"2077-11-17T00: 00: 00.000Z", "2077-11-17T00: 00: 00.000Z",
new String[] { "odh:theatre-cinema-and-performances", "schema:TheaterEvent" }, geometries);
for (int i = 0; i < 10; i++) {
events.add(event);
......
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import java.io.FileReader;
import java.io.IOException;
......@@ -8,7 +10,8 @@ public class Task9 {
public static void main(String[] args) throws IOException {
JsonReader reader = Json.createReader(new FileReader("src/main/resources/task9.json"));
JsonObject jobj = reader.readObject();
Event event = Event.parseJSON(jobj);
Jsonb jsonb = JsonbBuilder.create();
Event event = jsonb.fromJson(jobj.toString(), Event.class);
System.out.println(event);
}
}
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