I am trying to serialize and deserialize a list of JSON objects
Below is my JSON file
[
{
"id": "01",
"Status": "Open",
"siteId": "01",
"siteName": "M1"
},
{
"id": "02",
"Status": "Open",
"siteId": "02",
"siteName": "M2"
},
{
"id": "03",
"Status": "Open",
"siteId": "03",
"siteName": "M3"
}
]
code :
public static void main(String args[]) throws IOException {
//creating schema
Schema schema = ReflectData.get().getSchema(document.class);
System.out.println("schema created: "+ schema.toString());
//get JSON list
List<document> list = getJsonList();
File fileR = new File("<<path>>/jsonlist.avro");
//Serialize objects to file
DatumWriter<document> writerR = new ReflectDatumWriter(document.class); // Serialize objects to in-memory binary data
DataFileWriter<document> outR = new DataFileWriter(writerR).create(schema, fileR); // Write binary data to file
for(int i = 0 ;i<list.size();i++)
{
outR.append((document) list.get(i));
}
outR.close();
System.out.println("Serialize objects to file...\n");
//Deserialize objects from file
DatumReader<document> readerR = new ReflectDatumReader(document.class);
DataFileReader<document> inR = new DataFileReader(fileR, readerR);
System.out.println("Deserialize objects from file...");
for(document doc : inR) {
System.out.println(doc.toString());
}
inR.close();
}
private static List<document> getJsonList() throws IOException {
final ObjectMapper objectMapper = new ObjectMapper();
String fileName = "jsonList.json";
ClassLoader classLoader = <<className>>.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
System.out.println("File Found : " + file.exists());
//list of json objects
List<document> list = objectMapper.readValue(file,new TypeReference<List<document>>(){});
returnlist;
}
When I am trying to run the above code the file is getting serialized to check the deseralization the output is coming as
document@3246fb96
document@2e222612
I am not sure if the code for serialization is correct or where I am going wrong in the deserialization code
Referred : https://liyanxu.blog/2018/02/07/apache-avro-examples/
Please suggest!!