Skip to content

Commit 0a2e672

Browse files
authored
Add fast-json module
1 parent 39ebdee commit 0a2e672

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

‎fast-json/pom.xml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>fast-json</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>fast-json</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.12</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.alibaba</groupId>
26+
<artifactId>fastjson</artifactId>
27+
<version>1.2.13</version>
28+
</dependency>
29+
</dependencies>
30+
</project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.fast_json;
2+
3+
import java.util.Date;
4+
5+
import com.alibaba.fastjson.annotation.JSONField;
6+
7+
public class Person {
8+
9+
@JSONField(name = "AGE", serialize = false, deserialize = false)
10+
private int age;
11+
12+
@JSONField(name = "LAST NAME", ordinal = 2)
13+
private String lastName;
14+
15+
@JSONField(name = "FIRST NAME", ordinal = 1)
16+
private String firstName;
17+
18+
@JSONField(name = "DATE OF BIRTH", format = "dd/MM/yyyy", ordinal = 3)
19+
private Date dateOfBirth;
20+
21+
public Person() {
22+
23+
}
24+
25+
public Person(int age, String lastName, String firstName, Date dateOfBirth) {
26+
super();
27+
this.age = age;
28+
this.lastName = lastName;
29+
this.firstName = firstName;
30+
this.dateOfBirth = dateOfBirth;
31+
}
32+
33+
public int getAge() {
34+
return age;
35+
}
36+
37+
public void setAge(int age) {
38+
this.age = age;
39+
}
40+
41+
public String getLastName() {
42+
return lastName;
43+
}
44+
45+
public void setLastName(String lastName) {
46+
this.lastName = lastName;
47+
}
48+
49+
public String getFirstName() {
50+
return firstName;
51+
}
52+
53+
public void setFirstName(String firstName) {
54+
this.firstName = firstName;
55+
}
56+
57+
public Date getDateOfBirth() {
58+
return dateOfBirth;
59+
}
60+
61+
public void setDateOfBirth(Date dateOfBirth) {
62+
this.dateOfBirth = dateOfBirth;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "Person [age=" + age + ", lastName=" + lastName + ", firstName="
68+
+ firstName + ", dateOfBirth=" + dateOfBirth + "]";
69+
}
70+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.baeldung.fast_json;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.text.ParseException;
6+
import java.util.ArrayList;
7+
import java.util.Date;
8+
import java.util.List;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import com.alibaba.fastjson.JSON;
14+
import com.alibaba.fastjson.JSONArray;
15+
import com.alibaba.fastjson.JSONObject;
16+
import com.alibaba.fastjson.serializer.BeanContext;
17+
import com.alibaba.fastjson.serializer.ContextValueFilter;
18+
import com.alibaba.fastjson.serializer.NameFilter;
19+
import com.alibaba.fastjson.serializer.SerializeConfig;
20+
21+
public class FastJsonTests {
22+
private List<Person> listOfPersons = new ArrayList<Person>();
23+
24+
@Before
25+
public void setUp() {
26+
listOfPersons.add(new Person(15, "John", "Doe", new Date()));
27+
listOfPersons.add(new Person(20, "Janette", "Doe", new Date()));
28+
}
29+
30+
@Test
31+
public void convertObjectToJsonTest() {
32+
String personJsonFormat = JSON.toJSONString(listOfPersons);
33+
assertEquals(
34+
personJsonFormat,
35+
"[{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"John\",\"DATE OF BIRTH\":"
36+
+ "\"24/07/2016\"},{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"Janette\",\"DATE OF BIRTH\":"
37+
+ "\"24/07/2016\"}]");
38+
}
39+
40+
@Test
41+
public void convertJsonFormatToObject() {
42+
String personJsonFormat = JSON.toJSONString(listOfPersons.get(0));
43+
Person newPerson = JSON.parseObject(personJsonFormat, Person.class);
44+
assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute
45+
assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
46+
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
47+
}
48+
49+
@Test
50+
public void createJsonObjectTest() throws ParseException {
51+
JSONArray jsonArray = new JSONArray();
52+
for (int i = 0; i < 2; i++) {
53+
JSONObject jsonObject = new JSONObject();
54+
jsonObject.put("FIRST NAME", "John" + i);
55+
jsonObject.put("LAST NAME", "Doe" + i);
56+
jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
57+
jsonArray.add(jsonObject);
58+
}
59+
assertEquals(
60+
jsonArray.toString(),
61+
"[{\"LAST NAME\":\"Doe0\",\"DATE OF BIRTH\":"
62+
+ "\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John0\"},{\"LAST NAME\":\"Doe1\","
63+
+ "\"DATE OF BIRTH\":\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John1\"}]");
64+
}
65+
66+
@Test
67+
public void convertObjectToJsonUsingContextFilterTest() {
68+
ContextValueFilter valueFilter = new ContextValueFilter() {
69+
public Object process(BeanContext context, Object object,
70+
String name, Object value) {
71+
if (name.equals("DATE OF BIRTH")) {
72+
return "NOT TO DISCLOSE";
73+
}
74+
if (value.equals("John") || value.equals("Doe")) {
75+
return ((String) value).toUpperCase();
76+
} else {
77+
return null;
78+
}
79+
}
80+
};
81+
JSON.toJSONString(listOfPersons, valueFilter);
82+
}
83+
84+
@Test
85+
public void convertObjectToJsonUsingSerializeConfig() {
86+
NameFilter formatName = new NameFilter() {
87+
public String process(Object object, String name, Object value) {
88+
return name.toLowerCase().replace(" ", "_");
89+
}
90+
};
91+
SerializeConfig.getGlobalInstance().addFilter(Person.class, formatName);
92+
String jsonOutput = JSON.toJSONStringWithDateFormat(listOfPersons,
93+
"yyyy-MM-dd");
94+
assertEquals(
95+
jsonOutput,
96+
"[{\"first_name\":\"Doe\",\"last_name\":\"John\","
97+
+ "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":"
98+
+ "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
99+
}
100+
}

0 commit comments

Comments
 (0)