Decode
lets asume you parsed your file and you do a translation between the formats using objects
25654 Gilberto Holms gibaholms@hotmail.com
likely will translate to
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Person {
private int id;
private String fullName;
private String email;
public Person(int id, String fullName, String email) {
this.id = id;
this.fullName = fullName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public class Test {
public static void main(String[] args) {
final String fileName = "test.txt";
ObjectMapper objectMapper = new ObjectMapper();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
final List<String> jsons = stream.map(line -> {
return line.split(" ");
}).map(set -> {
return new Person(
Integer.parseInt(set[0]),
set[1],
set[2]
);
}).map(person -> {
try {
return objectMapper.writeValueAsString(person);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
}).filter(jsonString -> {
return !jsonString.isEmpty();
}).collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
}
}
something like that?
this is just a theoretical way to do it. important is
.map(person -> {
try {
return objectMapper.writeValueAsString(person);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
})
this transforms all Person objects to Json strings that's what the object mapper does via reflections.
does this help / make sense?
Neng Channa
South Korea/Web Developer/Billing
j Can you help me with this question?