Created
January 19, 2024 21:14
-
-
Save stoneboyindc/79a6a4550280da055c90287e89e2e97b to your computer and use it in GitHub Desktop.
This is a refactor of PersonRepository to load the csv and also add "getPersons()" method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.jsoniter.JsonIterator; | |
import com.jsoniter.any.Any; | |
import com.oc.projectone.dataminer.model.Person; | |
import org.springframework.stereotype.Service; | |
import javax.annotation.PostConstruct; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Service | |
public class PersonRepository { | |
private static List<Person> persons; | |
@PostConstruct | |
public void init() throws IOException { | |
String filePath = "src/main/resources/data.json"; | |
byte[] bytesFile = Files.readAllBytes(Paths.get(filePath)); | |
JsonIterator iter = JsonIterator.parse(bytesFile); | |
Any any = iter.readAny(); | |
Any personAny = any.get("persons"); | |
//Persons Mapping | |
persons = new ArrayList<>(); | |
personAny.forEach(a -> persons.add(new Person.PersonBuilder().firstName(a.get("firstName").toString()) | |
.address(a.get("address").toString()) | |
.city(a.get("city").toString()) | |
.lastName(a.get("lastName").toString()) | |
.phone(a.get("phone").toString()) | |
.zip(a.get("zip").toString()) | |
.email(a.get("email").toString()) | |
.build())); | |
persons.forEach(p -> System.out.println((p.firstName + ',' + ' ').concat(p.lastName + ',' + ' ').concat(p.address + ',' + ' ').concat(p.city + ',' + ' ').concat(p.phone + ',' + ' ').concat(p.zip + ',' + ' ').concat(p.email + ',' + ' '))); | |
} | |
public static List<Person> getPersons() { | |
return persons; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also need to add this in your pom.xml