Boon – Long To Data

  • Post author:
  • Post category:Boon
  • Post comments:1 Comment
long of data

ObjectMapper class can be used to work with different date formats in JSON. It can be used to parse/generate a long of data.

Example

The following example is using ObjectMapper class to generate a Date string from a long version.

import java.util.Date;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

public class BoonTester {
   public static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.create();      
      String jsonString = "{\"name\":\"Zafrul\", \"age\":21, \"dateOfBirth\":917327917730}";
      
      //mapper converts long to date automatically
      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student.dateOfBirth);    
      
      //by default mapper converts date to long
      jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   public String name;
   public int age;
   public Date dateOfBirth;
   
   public Student(String name, int age, Date dateOfBirth) {
      this.name = name;
      this.age = age;
      this.dateOfBirth = dateOfBirth;
   }
}

Output

Given below is the output of the code −

Tue Dec 12 00:00:00 IST 2000
{"name":"Zafrul","age":21,"dateOfBirth":917327917730}

Next Topic:-Click Here

This Post Has One Comment

Leave a Reply