Boon – @JsonIgnore

Boon – @JsonIgnore ”; Previous Next @JsonIgnore is used at field level to mark a property or list of properties to be ignored. Example – @JsonIgnore Following example is for @JsonIgnore − import org.boon.json.JsonFactory; import org.boon.json.ObjectMapper; import org.boon.json.annotations.JsonIgnore; public class BoonTester { public static void main(String args[]) { ObjectMapper mapper = JsonFactory.create(); Student student = new Student(1,11,”1ab”,”Mark”); String jsonString = mapper.writeValueAsString(student); System.out.println(jsonString); } } class Student { public int id; @JsonIgnore public String systemId; public int rollNo; public String name; Student(int id, int rollNo, String systemId, String name) { this.id = id; this.systemId = systemId; this.rollNo = rollNo; this.name = name; } } Output You will see the following output − {“id”:1,”rollNo”:11,”name”:”Mark”} Print Page Previous Next Advertisements ”;

Boon – Generating Date

Boon – Generating Date ”; Previous Next ObjectMapper class can be used to work with different date formats in JSON. It can be used to generate date object as well. By default ObjectMapper generates Date in long milliseconds version. Using ObjectMapper returned by JsonFactory.createUseJSONDates() method, we can get a string version of date during parsing. Example Following example is using ObjectMapper class to generate a Date string by parsing JSON. 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.createUseJSONDates(); String jsonString = “{“name”:”Mahesh”, “age”:21, “dateOfBirth”:”1998-08-11T11:31:00.034Z” }”; //mapper converts String to date automatically Student student = mapper.readValue(jsonString, Student.class); System.out.println(student.dateOfBirth); //Mapper converts date to date string now 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 You will receive the following output − Tue Aug 11 17:01:00 IST 1998 {“name”:”Mahesh”,”age”:21,”dateOfBirth”:”1998-08-11T11:31:00.034Z”} Print Page Previous Next Advertisements ”;

Boon – From Map

Boon – From Map ”; Previous Next ObjectMapper class can be used to generate a json string from a Map. Example Following example is using ObjectMapper class to generate a JSON string from a Map Object. import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.boon.json.JsonFactory; import org.boon.json.ObjectMapper; public class BoonTester { public static void main(String args[]){ ObjectMapper mapper = JsonFactory.create(); Map<String, String> student = new HashMap<>(); student.put(“Name”, “Mahesh”); student.put(“RollNo”, “21”); Map<String, String> student1 = new HashMap<>(); student1.put(“Name”, “Suresh”); student1.put(“RollNo”, “22”); List<Map<String,String>> studentList = new ArrayList<>(); studentList.add(student); studentList.add(student1); Map<String, List> studentMap = new HashMap<String, List>(); studentMap.put(“students”, studentList); String jsonString = mapper.writeValueAsString(studentMap); System.out.println(jsonString); } } Output When you execute the above code, you should see the following output − {“students”:[{“RollNo”:”21″,”Name”:”Mahesh”},{“RollNo”:”22″,”Name”:”Suresh”}]} Print Page Previous Next Advertisements ”;