Skip to content

Commit

Permalink
Complete code-generation task
Browse files Browse the repository at this point in the history
Add lombok annotations in User.java and Car.java.
Add serialization and deserialization Car object to JSON.
Add saving and extracting Car object data from file.
  • Loading branch information
JackKaif committed Feb 6, 2024
1 parent 99159e4 commit f70ceb5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions java-oop-ru/code-generation/src/main/java/exercise/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@
import java.nio.file.StandardOpenOption;

// BEGIN
public class App {
public static void save(Path filepath, Car car) throws Exception {
Files.writeString(filepath, car.serialize(), StandardOpenOption.CREATE);
}

public static Car extract(Path filepath) throws Exception {
return Car.unserialize(Files.readString(filepath));
}
}
// END
10 changes: 8 additions & 2 deletions java-oop-ru/code-generation/src/main/java/exercise/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

// BEGIN

@Value
// END
class Car {
int id;
Expand All @@ -14,6 +14,12 @@ class Car {
User owner;

// BEGIN

public String serialize() throws Exception {
return new ObjectMapper().writeValueAsString(this);
}

public static Car unserialize(String json) throws Exception {
return new ObjectMapper().readValue(json, Car.class);
}
// END
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Value;

// BEGIN

@Value
// END
class User {
int id;
Expand Down

0 comments on commit f70ceb5

Please sign in to comment.