Skip to content

Instantly share code, notes, and snippets.

@stoneboyindc
Created January 19, 2024 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoneboyindc/79a6a4550280da055c90287e89e2e97b to your computer and use it in GitHub Desktop.
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
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;
}
}
@stoneboyindc
Copy link
Author

Also need to add this in your pom.xml

		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>javax.annotation-api</artifactId>
			<version>1.3.2</version>
	  </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment