What is FlexJSON?
Flexjson is a lightweight java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format.What's different about Flexjson is it's control over what gets serialized allowing both deep and shallow copies of objects.
You can download FlexJson jar from http://sourceforge.net/projects/flexjson/
In this example we can see
1. How to convert Java object to Json
2. How to convert List of object to Json
3. How to convert Map to Json
4. How to convert Json to Java object
5. How to use pretty print.
FlexJSON Serialization Example
1. Add below dependency in your pom.xml
<groupId>net.sf.flexjson</groupId>
<artifactId>flexjson</artifactId>
<version>2.0</version>
</dependency>
Below is one simple example to serialize Java Object Json String
2. Create a simple java pojo
public class Customer {
public Customer(String name, String address) {
super();
this.name = name;
this.address = address;
}
public Customer() {}
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Customer [name=" + name + ", address=" + address + "]";
}
}
3. Create a Java class to do Flex Json operations
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
public class FlexJsonExample {
public static void main(String[] args) {
// Java object to Json
JSONSerializer serializer = new JSONSerializer();
serializer.prettyPrint(true);
Customer customer = new Customer("Vinod", "Bangalore");
String flexJsonString = serializer.serialize(customer);
System.out.println(flexJsonString);
// Java Map key value pair to Json
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME", "vinod");
data.put("LOCATION", "Bangalore");
data.put("ADDRESS", customer);
String mapstring = serializer.serialize(data);
System.out.println(mapstring);
// Java list of object to Json String
Customer customer1 = new Customer("Vinod", "Bangalore");
Customer customer2 = new Customer("Raghava", "Bangalore");
Customer customer3 = new Customer("Krishna", "Bangalore");
List<Customer> pcs = Arrays.asList(customer1, customer2, customer3);
String listString = serializer.serialize(pcs);
System.out.println(listString);
// Deserialization
Customer custo = (Customer) new JSONDeserializer().use(null, Customer.class).deserialize(flexJsonString);
System.out.println(custo);
}
}
4. Output
"address": "Bangalore",
"class": "com.vinod.test.Customer",
"name": "Vinod"
}
{
"LOCATION": "Bangalore",
"ADDRESS": {
"address": "Bangalore",
"class": "com.vinod.test.Customer",
"name": "Vinod"
},
"NAME": "vinod"
}
[
{
"address": "Bangalore",
"class": "com.vinod.test.Customer",
"name": "Vinod"
},
{
"address": "Bangalore",
"class": "com.vinod.test.Customer",
"name": "Raghava"
},
{
"address": "Bangalore",
"class": "com.vinod.test.Customer",
"name": "Krishna"
}
]
Customer [name=Vinod, address=Bangalore]
5. Done!!.. Download example
https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-java-mapping
Reference
http://flexjson.sourceforge.net/
No comments:
Post a Comment