1package com.example.graphics.util;
3import com.example.graphics.Drawing;
4import com.example.graphics.factory.ShapeFactory;
5import com.example.graphics.model.Shape;
6import org.json.JSONArray;
7import org.json.JSONObject;
10import java.nio.file.Files;
11import java.nio.file.Paths;
12import java.util.ArrayList;
35 try (ObjectOutputStream oos =
new ObjectOutputStream(
new FileOutputStream(filePath))) {
37 List<Shape> shapes =
new ArrayList<>(drawing.getShapes());
38 oos.writeObject(shapes);
49 @SuppressWarnings(
"unchecked")
51 try (ObjectInputStream ois =
new ObjectInputStream(
new FileInputStream(filePath))) {
52 return (List<Shape>) ois.readObject();
62 public void saveDrawingJson(String jsonData, String filePath)
throws IOException {
63 Files.write(Paths.get(filePath), jsonData.getBytes());
73 String content =
new String(Files.readAllBytes(Paths.get(filePath)));
74 JSONObject jsonObject =
new JSONObject(content);
75 JSONArray shapesArray = jsonObject.getJSONArray(
"shapes");
77 List<Shape> shapes =
new ArrayList<>();
79 for (
int i = 0; i < shapesArray.length(); i++) {
80 JSONObject shapeJson = shapesArray.getJSONObject(i);
81 String type = shapeJson.getString(
"type");
87 shapeJson.getInt(
"x"),
88 shapeJson.getInt(
"y"),
89 shapeJson.getInt(
"radius")
94 shapeJson.getInt(
"x"),
95 shapeJson.getInt(
"y"),
96 shapeJson.getInt(
"width"),
97 shapeJson.getInt(
"height")
102 shapeJson.getInt(
"x1"),
103 shapeJson.getInt(
"y1"),
104 shapeJson.getInt(
"x2"),
105 shapeJson.getInt(
"y2")
110 shapeJson.getInt(
"x1"),
111 shapeJson.getInt(
"y1"),
112 shapeJson.getInt(
"x2"),
113 shapeJson.getInt(
"y2"),
114 shapeJson.getInt(
"x3"),
115 shapeJson.getInt(
"y3")
135 Files.write(Paths.get(filePath), xmlData.getBytes());
final ShapeFactory shapeFactory
void saveDrawingBinary(Drawing drawing, String filePath)
void saveDrawingXml(String xmlData, String filePath)
void saveDrawingJson(String jsonData, String filePath)
List< Shape > loadDrawingBinary(String filePath)
List< Shape > loadDrawingJson(String filePath)