Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.49 KB | None | 0 0
  1. package hibernate.serialization;
  2.  
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.SerializationFeature;
  6. import com.fasterxml.jackson.dataformat.xml.XmlMapper;
  7. import com.fasterxml.jackson.datatype.joda.JodaModule;
  8. import hibernate.creator.ModelObjectsCreator;
  9. import hibernate.model.Address;
  10. import hibernate.model.Client;
  11.  
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.util.List;
  16.  
  17. public class JacksonSerialization {
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.  
  21.         ObjectMapper jsonMapper = new ObjectMapper();
  22.         XmlMapper xmlMapper = new XmlMapper();
  23.         jsonMapper.registerModule(new JodaModule());
  24.         xmlMapper.registerModule(new JodaModule());
  25.         //serializeDemo(jsonMapper, "json");
  26.         serializeListDemo(jsonMapper, "json");
  27.         serializeListDemo(xmlMapper, "xml");
  28.         serializeAddressesList(jsonMapper, "json");
  29.         //deserializeDemo(jsonMapper, "json");
  30.     }
  31.  
  32.     private static void serializeDemo(ObjectMapper mapper, String fileSuffix) throws IOException {
  33.  
  34.        /* //Set mapper to pretty-print
  35.         mapper.enable(SerializationFeature.INDENT_OUTPUT);
  36.  
  37.         //Create objects to serialize
  38.         ModelObjectsCreator objectsCreator = new ModelObjectsCreator();
  39.         Client client = objectsCreator.getClient();
  40.  
  41.         //Serialize to file and string
  42.         mapper.writeValue(new File("result." + fileSuffix), client);
  43.         String jsonString = mapper.writeValueAsString(client);
  44.         System.out.println("original client json");
  45.         System.out.println(jsonString);
  46.  
  47.         //Deserialize from file
  48.         Client deserializedEmployee = mapper.readValue(
  49.                 new File("result." + fileSuffix), Client.class);
  50.  
  51.         //Give a rise
  52.         deserializedEmployee.setClientName("tttttt");
  53.  
  54.         //Serialize back
  55.         mapper.writeValue(new File("result-modified." + fileSuffix), deserializedEmployee);
  56.         String modifiedJsonString = mapper.writeValueAsString(deserializedEmployee);
  57.         System.out.println(modifiedJsonString);*/
  58.  
  59.     }
  60.  
  61.     private static void serializeListDemo(ObjectMapper mapper, String fileSuffix) throws IOException {
  62.  
  63.         //Set mapper to pretty-print
  64.         mapper.enable(SerializationFeature.INDENT_OUTPUT);
  65.  
  66.         //Create objects to serialize
  67.         ModelObjectsCreator objectsCreator = new ModelObjectsCreator();
  68.         List<Client> clients = objectsCreator.getClient();
  69.  
  70.         String stringToWriteToFile = mapper.writeValueAsString(clients);
  71.         System.out.println(stringToWriteToFile);
  72.  
  73.         //Serialize to file and string
  74.         mapper.writeValue(new File("result." + fileSuffix), clients);
  75.  
  76.         //Deserialize from file
  77.         List<Client> deserializedEmployee = mapper.readValue(
  78.                 new File("result." + fileSuffix), new TypeReference<List<Client>>() { } );
  79.     }
  80.  
  81.     private static void serializeAddressesList(ObjectMapper mapper, String fileSuffix) throws IOException {
  82.  
  83.         //Set mapper to pretty-print
  84.         mapper.enable(SerializationFeature.INDENT_OUTPUT);
  85.  
  86.         //Create objects to serialize
  87.         ModelObjectsCreator objectsCreator = new ModelObjectsCreator();
  88.         List<Address> addresses = objectsCreator.getAddresses();
  89.  
  90.         String stringToWriteToFile = mapper.writeValueAsString(addresses);
  91.         System.out.println(stringToWriteToFile);
  92.  
  93.         //Serialize to file and string
  94.         mapper.writeValue(new File("result." + fileSuffix), addresses);
  95.  
  96.         //Deserialize from file
  97.         List<Address> deserializedEmployee = mapper.readValue(
  98.                 new File("result." + fileSuffix), new TypeReference<List<Address>>() { } );
  99.     }
  100.  
  101.     public static void deserializeDemo(ObjectMapper mapper, String fileSuffix) throws IOException {
  102.         //Deserialized employee object from employees.* file in resources
  103.         InputStream employeeIs = JacksonSerialization.class.getClassLoader().
  104.                 getResourceAsStream("clients." + fileSuffix);
  105.  
  106.         //Read value - set class type of serialization
  107.         Client deserializedEmployee = mapper.readValue(employeeIs, Client.class);
  108.  
  109.         //Give eployee big salary
  110.         deserializedEmployee.setClientName("zmianaaaa");
  111.  
  112.         String modifiedSerialzied = mapper.writeValueAsString(deserializedEmployee);
  113.  
  114.         System.out.println(modifiedSerialzied);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement