View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
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   * Extension of {@link org.primefaces.extensions.converter.JsonConverter}. It uses specific Gson converters from this tag library.
33   *
34   * @author Oleg Varaksin / last modified by $Author$
35   * @version $Revision$
36   * @since 1.1.0
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  }