Skip to content

Commit d1a24bf

Browse files
Json example
1 parent 9501ae3 commit d1a24bf

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sporadic.jsonExample;
2+
3+
import org.codehaus.jackson.annotate.JsonIgnore;
4+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
5+
import org.codehaus.jackson.annotate.JsonProperty;
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.stereotype.Service;
8+
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
import lombok.Setter;
12+
import lombok.ToString;
13+
14+
/**This package is to demo how @JsonIgnore and @JsonIgnorProperties and @JsonProperty work:
15+
* @JsonIgnore is to annotate a field or method to make it free from being seriazlied by json
16+
* @JsonIgnoreProperties is to annotate a class, and you must/may specifiy which properties you want json to skip serializing.
17+
* @JsonProperty is to annotate a field and give this field a specific name when Jackson does serialization, e.g. a name.*/
18+
19+
20+
@NoArgsConstructor
21+
@Service
22+
@ToString
23+
@JsonIgnoreProperties({"noInterestingMember", "forgetThisField"})
24+
public class JsonDemoClass {
25+
@Getter
26+
@Setter
27+
@JsonProperty("ID")
28+
public long id;
29+
30+
@Getter
31+
@Setter
32+
@JsonProperty("NAME")
33+
public String name;
34+
35+
@Getter
36+
@Setter
37+
public String noInterestingMember;
38+
39+
@Getter
40+
@Setter
41+
@JsonIgnore
42+
@JsonProperty("ANOTHER-MEMEBR")
43+
public int anotherMember;
44+
45+
@Getter
46+
@Setter
47+
public double forgetThisField;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package sporadic.jsonExample;
2+
3+
import java.io.IOException;
4+
5+
import org.codehaus.jackson.map.ObjectMapper;
6+
import org.springframework.context.support.AbstractApplicationContext;
7+
import org.springframework.context.support.ClassPathXmlApplicationContext;
8+
9+
public class JsonIgnoreAnnotationDemo {
10+
11+
public static void main(String... args) {
12+
13+
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
14+
"spring-configuration/json-spring-configuration.xml");
15+
JsonDemoClass jsonDemoClass = (JsonDemoClass) context.getBean("jsonDemoClass");
16+
17+
ObjectMapper mapper = new ObjectMapper();
18+
19+
jsonDemoClass.setId(1);
20+
jsonDemoClass.setName("Test program!");
21+
jsonDemoClass.setAnotherMember(123);
22+
jsonDemoClass.setForgetThisField(123.456);
23+
jsonDemoClass.setNoInterestingMember("Something not interesting!");
24+
25+
String s = null;
26+
try {
27+
s = mapper.writeValueAsString(jsonDemoClass);
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
System.out.println("String s is: " + s);
32+
33+
JsonDemoClass jsonDemoClass2 = (JsonDemoClass) context.getBean("jsonDemoClass");
34+
try {
35+
jsonDemoClass2 = mapper.readValue(s, JsonDemoClass.class);
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
System.out.println("jsonDemoClass2.toString() s is: " + jsonDemoClass2.toString() + "\nThe end.");
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10+
11+
<context:component-scan base-package="sporadic.JsonExample"></context:component-scan>
12+
13+
</beans>

0 commit comments

Comments
 (0)