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.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  
34  
35  
36  
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  }