1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.primefaces.extensions.converter;
23
24 import javax.el.ValueExpression;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27
28 import org.primefaces.extensions.util.json.GsonConverter;
29 import org.primefaces.extensions.util.json.GsonExposeAwareConverter;
30
31
32
33
34
35
36
37
38 public class JsonExposeAwareConverter extends JsonConverter {
39
40 private static final long serialVersionUID = 1L;
41
42 private boolean excludeFieldsWithoutExposeAnnotation = false;
43
44 public JsonExposeAwareConverter() {
45 }
46
47 public JsonExposeAwareConverter(boolean excludeFieldsWithoutExposeAnnotation) {
48 this.excludeFieldsWithoutExposeAnnotation = excludeFieldsWithoutExposeAnnotation;
49 }
50
51 public boolean isExcludeFieldsWithoutExposeAnnotation() {
52 return excludeFieldsWithoutExposeAnnotation;
53 }
54
55 public void setExcludeFieldsWithoutExposeAnnotation(boolean excludeFieldsWithoutExposeAnnotation) {
56 this.excludeFieldsWithoutExposeAnnotation = excludeFieldsWithoutExposeAnnotation;
57 }
58
59 @Override
60 public Object getAsObject(FacesContext context, UIComponent component, String value) {
61 java.lang.reflect.Type objType;
62
63 if (getType() == null) {
64 final ValueExpression expression = component.getValueExpression("value");
65 objType = expression.getType(context.getELContext());
66 }
67 else {
68 objType = getObjectType(getType().trim(), false);
69 }
70
71 if (excludeFieldsWithoutExposeAnnotation) {
72 return GsonExposeAwareConverter.getGson().fromJson(value, objType);
73 }
74 else {
75 return GsonConverter.getGson().fromJson(value, objType);
76 }
77 }
78
79 @Override
80 public String getAsString(FacesContext context, UIComponent component, Object value) {
81 if (excludeFieldsWithoutExposeAnnotation) {
82 if (getType() == null) {
83 return GsonExposeAwareConverter.getGson().toJson(value);
84 }
85 else {
86 return GsonExposeAwareConverter.getGson().toJson(value, getObjectType(getType().trim(), false));
87 }
88 }
89 else {
90 if (getType() == null) {
91 return GsonConverter.getGson().toJson(value);
92 }
93 else {
94 return GsonConverter.getGson().toJson(value, getObjectType(getType().trim(), false));
95 }
96 }
97 }
98 }