Skip to content

Commit 7cbf987

Browse files
Merge branch 'pr/528-hamza'
2 parents 11241ea + daa3a5d commit 7cbf987

6 files changed

Lines changed: 220 additions & 31 deletions

File tree

‎json/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=========
2+
3+
## Fast-Json
4+
5+
### Relevant Articles:
6+
- [Introduction to JSON Schema in Java](http://www.baeldung.com/introduction-to-json-schema-in-java)
7+
- [A Guide to FastJson](http://www.baeldung.com/????????)

‎json/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<version>1.3.0</version>
1414
</dependency>
1515

16+
<dependency>
17+
<groupId>com.alibaba</groupId>
18+
<artifactId>fastjson</artifactId>
19+
<version>1.2.13</version>
20+
</dependency>
21+
1622
<dependency>
1723
<groupId>junit</groupId>
1824
<artifactId>junit</artifactId>

json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java renamed to json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
package org.baeldung.json.schema;
2-
3-
import org.everit.json.schema.Schema;
4-
import org.everit.json.schema.loader.SchemaLoader;
5-
6-
import org.json.JSONObject;
7-
import org.json.JSONTokener;
8-
import org.junit.Test;
9-
10-
public class JSONSchemaTest {
11-
12-
@Test
13-
public void givenInvalidInput_whenValidating_thenInvalid() {
14-
15-
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
16-
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
17-
18-
Schema schema = SchemaLoader.load(jsonSchema);
19-
schema.validate(jsonSubject);
20-
}
21-
22-
@Test
23-
public void givenValidInput_whenValidating_thenValid() {
24-
25-
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
26-
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
27-
28-
Schema schema = SchemaLoader.load(jsonSchema);
29-
schema.validate(jsonSubject);
30-
}
31-
}
1+
package com.baeldung.json.schema;
2+
3+
import org.everit.json.schema.Schema;
4+
import org.everit.json.schema.ValidationException;
5+
import org.everit.json.schema.loader.SchemaLoader;
6+
7+
import org.json.JSONObject;
8+
import org.json.JSONTokener;
9+
import org.junit.Test;
10+
11+
public class JSONSchemaTest {
12+
13+
@Test(expected = ValidationException.class)
14+
public void givenInvalidInput_whenValidating_thenInvalid() {
15+
16+
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
17+
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
18+
19+
Schema schema = SchemaLoader.load(jsonSchema);
20+
schema.validate(jsonSubject);
21+
}
22+
23+
@Test
24+
public void givenValidInput_whenValidating_thenValid() {
25+
26+
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
27+
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
28+
29+
Schema schema = SchemaLoader.load(jsonSchema);
30+
schema.validate(jsonSubject);
31+
}
32+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package fast_json;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONArray;
5+
import com.alibaba.fastjson.JSONObject;
6+
import com.alibaba.fastjson.serializer.BeanContext;
7+
import com.alibaba.fastjson.serializer.ContextValueFilter;
8+
import com.alibaba.fastjson.serializer.NameFilter;
9+
import com.alibaba.fastjson.serializer.SerializeConfig;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import java.text.ParseException;
14+
import java.util.ArrayList;
15+
import java.util.Calendar;
16+
import java.util.List;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
public class FastJsonTests {
21+
private List<Person> listOfPersons;
22+
23+
@Before
24+
public void setUp() {
25+
listOfPersons = new ArrayList<Person>();
26+
Calendar calendar = Calendar.getInstance();
27+
calendar.set(2016, 6, 24);
28+
listOfPersons.add(new Person(15, "John", "Doe", calendar.getTime()));
29+
listOfPersons.add(new Person(20, "Janette", "Doe", calendar.getTime()));
30+
}
31+
32+
@Test
33+
public void whenJavaList_thanConvertToJsonCorrect() {
34+
String personJsonFormat = JSON.toJSONString(listOfPersons);
35+
assertEquals(
36+
personJsonFormat,
37+
"[{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"John\",\"DATE OF BIRTH\":"
38+
+ "\"24/07/2016\"},{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"Janette\",\"DATE OF BIRTH\":"
39+
+ "\"24/07/2016\"}]");
40+
}
41+
42+
@Test
43+
public void whenJson_thanConvertToObjectCorrect() {
44+
String personJsonFormat = JSON.toJSONString(listOfPersons.get(0));
45+
Person newPerson = JSON.parseObject(personJsonFormat, Person.class);
46+
assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute
47+
assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
48+
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
49+
}
50+
51+
@Test
52+
public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
53+
JSONArray jsonArray = new JSONArray();
54+
for (int i = 0; i < 2; i++) {
55+
JSONObject jsonObject = new JSONObject();
56+
jsonObject.put("FIRST NAME", "John" + i);
57+
jsonObject.put("LAST NAME", "Doe" + i);
58+
jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
59+
jsonArray.add(jsonObject);
60+
}
61+
assertEquals(
62+
jsonArray.toString(),
63+
"[{\"LAST NAME\":\"Doe0\",\"DATE OF BIRTH\":"
64+
+ "\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John0\"},{\"LAST NAME\":\"Doe1\","
65+
+ "\"DATE OF BIRTH\":\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John1\"}]");
66+
}
67+
68+
@Test
69+
public void givenContextFilter_whenJavaObject_thanJsonCorrect() {
70+
ContextValueFilter valueFilter = new ContextValueFilter() {
71+
public Object process(BeanContext context, Object object,
72+
String name, Object value) {
73+
if (name.equals("DATE OF BIRTH")) {
74+
return "NOT TO DISCLOSE";
75+
}
76+
if (value.equals("John") || value.equals("Doe")) {
77+
return ((String) value).toUpperCase();
78+
} else {
79+
return null;
80+
}
81+
}
82+
};
83+
JSON.toJSONString(listOfPersons, valueFilter);
84+
}
85+
86+
@Test
87+
public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() {
88+
NameFilter formatName = new NameFilter() {
89+
public String process(Object object, String name, Object value) {
90+
return name.toLowerCase().replace(" ", "_");
91+
}
92+
};
93+
SerializeConfig.getGlobalInstance().addFilter(Person.class, formatName);
94+
String jsonOutput = JSON.toJSONStringWithDateFormat(listOfPersons,
95+
"yyyy-MM-dd");
96+
assertEquals(
97+
jsonOutput,
98+
"[{\"first_name\":\"Doe\",\"last_name\":\"John\","
99+
+ "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":"
100+
+ "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
101+
// resetting custom serializer
102+
SerializeConfig.getGlobalInstance().put(Person.class, null);
103+
}
104+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package fast_json;
2+
3+
import com.alibaba.fastjson.annotation.JSONField;
4+
5+
import java.util.Date;
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+
@Override
34+
public String toString() {
35+
return "Person [age=" + age + ", lastName=" + lastName + ", firstName="
36+
+ firstName + ", dateOfBirth=" + dateOfBirth + "]";
37+
}
38+
39+
public int getAge() {
40+
return age;
41+
}
42+
43+
public void setAge(int age) {
44+
this.age = age;
45+
}
46+
47+
public String getLastName() {
48+
return lastName;
49+
}
50+
51+
public void setLastName(String lastName) {
52+
this.lastName = lastName;
53+
}
54+
55+
public String getFirstName() {
56+
return firstName;
57+
}
58+
59+
public void setFirstName(String firstName) {
60+
this.firstName = firstName;
61+
}
62+
63+
public Date getDateOfBirth() {
64+
return dateOfBirth;
65+
}
66+
67+
public void setDateOfBirth(Date dateOfBirth) {
68+
this.dateOfBirth = dateOfBirth;
69+
}
70+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<module>jjwt</module>
3030
<module>jooq-spring</module>
3131
<module>json-path</module>
32+
<module>json</module>
3233
<module>mockito</module>
3334
<module>mocks</module>
3435
<module>jee7schedule</module>

0 commit comments

Comments
 (0)