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.component.layout;
23  
24  import java.lang.reflect.Type;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.primefaces.extensions.model.layout.LayoutOptions;
29  
30  import com.google.gson.*;
31  
32  /**
33   * Gson serializer for layout options.
34   *
35   * @author Oleg Varaksin / last modified by Melloware
36   * @since 0.6.0
37   */
38  public class LayoutOptionsSerializer implements JsonSerializer<LayoutOptions> {
39  
40      @Override
41      public JsonElement serialize(final LayoutOptions src, final Type typeOfSrc, final JsonSerializationContext context) {
42          final JsonObject result = new JsonObject();
43  
44          final Set<Map.Entry<String, Object>> options = src.getOptions().entrySet();
45          for (final Map.Entry<String, Object> entry : options) {
46              final Object value = entry.getValue();
47              JsonPrimitive jsonPrimitive = null;
48  
49              if (value instanceof Boolean) {
50                  jsonPrimitive = new JsonPrimitive((Boolean) value);
51              }
52              else if (value instanceof Number) {
53                  jsonPrimitive = new JsonPrimitive((Number) value);
54              }
55              else if (value instanceof String) {
56                  jsonPrimitive = new JsonPrimitive((String) value);
57              }
58  
59              result.add(entry.getKey(), jsonPrimitive);
60          }
61  
62          if (src.getPanesOptions() != null) {
63              result.add("defaults", context.serialize(src.getPanesOptions(), src.getPanesOptions().getClass()));
64          }
65  
66          if (src.getTips() != null) {
67              result.add("tips", context.serialize(src.getTips(), src.getTips().getClass()));
68          }
69  
70          if (src.getNorthOptions() != null) {
71              result.add("north", context.serialize(src.getNorthOptions(), src.getNorthOptions().getClass()));
72          }
73  
74          if (src.getSouthOptions() != null) {
75              result.add("south", context.serialize(src.getSouthOptions(), src.getSouthOptions().getClass()));
76          }
77  
78          if (src.getWestOptions() != null) {
79              result.add("west", context.serialize(src.getWestOptions(), src.getWestOptions().getClass()));
80          }
81  
82          if (src.getEastOptions() != null) {
83              result.add("east", context.serialize(src.getEastOptions(), src.getEastOptions().getClass()));
84          }
85  
86          if (src.getCenterOptions() != null) {
87              result.add("center", context.serialize(src.getCenterOptions(), src.getCenterOptions().getClass()));
88          }
89  
90          if (src.getChildOptions() != null) {
91              result.add("children", context.serialize(src.getChildOptions(), src.getChildOptions().getClass()));
92          }
93  
94          return result;
95      }
96  
97  }