tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > SOJO > Serialize and Exclude Properties

Serialize and Exclude Properties 

SOJO (Simplified Old Java Objects) is a Java framework which converts object graph into a specific structure or representation. This requires the library sojo-1.0.0.jar to be in classpath. The following example shows using ObjectSerializer class. It serializes the given object by excluding specified properties.

File Name  :  
com/bethecoder/tutorials/sojo/common/Student.java 
   
package com.bethecoder.tutorials.sojo.common;

public class Student {

  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/sojo/tests/SerializeExclude.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.sojo.tests;

import java.io.IOException;

import net.sf.sojo.interchange.object.ObjectSerializer;

import com.bethecoder.tutorials.sojo.common.Student;

public class SerializeExclude {

  /**
   @param args
   @throws ClassNotFoundException 
   @throws IOException 
   */
  public static void main(String[] argsthrows ClassNotFoundException, IOException {
    
    Student student = new Student("Sriram"2"Chess");
    System.out.println(student);

    //Serialize to byte array (Exclude hobby property)
    ObjectSerializer serializer = new ObjectSerializer();
    Object serBytes = serializer.serialize(student, new String [] { "hobby" });
    
    //Deserialize from byte array
    student = (Studentserializer.deserialize(serBytes);
    System.out.println(student);
  }
}
   

It gives the following output,
Student[name = Sriram, age = 2, hobby = Chess]
Student[name = Sriram, age = 2, hobby = null]


                    Print

   



Books Recommendation

 
 
bl  br