Skip to content

Commit

Permalink
Java reading file including field timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Oliveira authored and Alexandre Oliveira committed Dec 12, 2023
1 parent eb4f5d8 commit c194333
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
5 changes: 5 additions & 0 deletions java/course/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version> <!-- Verifique a versão mais recente no repositório Maven -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import com.example.devsuperior.course.entities.Lesson;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class Main {
public static void main(String[] args) {

ObjectMapper objectMapper = new ObjectMapper();

// Registrar o módulo JavaTime para suporte a java.time.Instant
objectMapper.registerModule(new JavaTimeModule());

try {
File jsonFile = new File("/Users/alexandreoliveira/Documents/ws-projects/project-json/java/course/src/main/resources/file.json");
List<Course> courses = objectMapper.readValue(jsonFile, new TypeReference<List<Course>>() {});
Expand All @@ -29,6 +33,7 @@ public static void main(String[] args) {
System.out.println(" id: " + lesson.getId());
System.out.println(" title: " + lesson.getTitle());
System.out.println(" media: " + lesson.getMedia());
System.out.println(" timestamp: " + lesson.getTimestamp());
}
System.out.println();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ public class Lesson {
private Long id;
private String title;
private String media;
// private Instant timestamp;
private Instant timestamp;

public Lesson() {
}

// public Lesson(Long id, String title, String media, Instant timestamp) {
public Lesson(Long id, String title, String media) {
public Lesson(Long id, String title, String media, Instant timestamp) {
this.id = id;
this.title = title;
this.media = media;
// this.timestamp = timestamp;
this.timestamp = timestamp;
}

public Long getId() {
Expand All @@ -44,17 +43,16 @@ public void setMedia(String media) {
this.media = media;
}

// public Instant getTimestamp() {
// return timestamp;
// }
public Instant getTimestamp() {
return timestamp;
}

// public void setTimestamp(Instant timestamp) {
// this.timestamp = timestamp;
// }
public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}

@Override
public String toString() {
// return "Lesson [id=" + id + ", title=" + title + ", media=" + media + ", timestamp=" + timestamp + "]";
return "Lesson [id=" + id + ", title=" + title + ", media=" + media + "]";
return "Lesson [id=" + id + ", title=" + title + ", media=" + media + ", timestamp=" + timestamp + "]";
}
}
18 changes: 12 additions & 6 deletions java/course/src/main/resources/file.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
{
"id": 54,
"title": "Como funciona o curso",
"media": "f60b0450-96d0-4244-8e4f-bde367b21bfc"
"media": "f60b0450-96d0-4244-8e4f-bde367b21bfc",
"timestamp": "2023-12-06T16:34:56Z"
},
{
"id": 74,
"title": "Orientações importantes",
"media": "sadf876f-8d7f-2864-d6f6-89sad7f7lads"
"media": "sadf876f-8d7f-2864-d6f6-89sad7f7lads",
"timestamp": "2023-12-02T17:45:00Z"
},
{
"id": 65,
"title": "O que você vai aprender",
"media": "lklk1213-92j3-9747-9o47-dkljjasd98ff"
"media": "lklk1213-92j3-9747-9o47-dkljjasd98ff",
"timestamp": "2023-12-08T09:20:00Z"
}
]
},
Expand All @@ -27,17 +30,20 @@
{
"id": 38,
"title": "Criando o projeto",
"media": "kjhfjh28-2864-2836-hf76-8d8f8dfhfdf"
"media": "kjhfjh28-2864-2836-hf76-8d8f8dfhfdf",
"timestamp": "2023-12-04T08:00:00Z"
},
{
"id": 149,
"title": "Implementando as funcionalidades",
"media": "djheruyr3-8fjy-8276-47kh-kdfhhfi273"
"media": "djheruyr3-8fjy-8276-47kh-kdfhhfi273",
"timestamp": "2023-12-09T08:25:30Z"
},
{
"id": 61,
"title": "Testando tudo",
"media": "kdjf9274df-43df-9874-df55-teowncr846"
"media": "kdjf9274df-43df-9874-df55-teowncr846",
"timestamp": "2023-12-10T10:35:00Z"
}
]
}
Expand Down

0 comments on commit c194333

Please sign in to comment.