View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.serialization;
20  
21  import com.fasterxml.jackson.core.JsonGenerator;
22  import com.fasterxml.jackson.databind.JsonSerializer;
23  import java.io.IOException;
24  import java.util.Base64;
25  import java.util.List;
26  import org.identityconnectors.common.security.GuardedString;
27  
28  public abstract class AbstractValueSerializer<T extends Object> extends JsonSerializer<T> {
29  
30      public static final String BYTE_ARRAY_PREFIX = "<binary>";
31  
32      public static final String BYTE_ARRAY_SUFFIX = "</binary>";
33  
34      protected void doSerialize(final List<Object> value, final JsonGenerator jgen) throws IOException {
35          if (value == null) {
36              jgen.writeNull();
37          } else {
38              jgen.writeStartArray();
39              for (Object v : value) {
40                  if (v == null) {
41                      jgen.writeNull();
42                  } else if (v instanceof GuardedString) {
43                      jgen.writeObject(v);
44                  } else if (v instanceof Integer) {
45                      jgen.writeNumber((Integer) v);
46                  } else if (v instanceof Long) {
47                      jgen.writeNumber((Long) v);
48                  } else if (v instanceof Double) {
49                      jgen.writeNumber((Double) v);
50                  } else if (v instanceof Boolean) {
51                      jgen.writeBoolean((Boolean) v);
52                  } else if (v instanceof byte[]) {
53                      jgen.writeString(
54                              BYTE_ARRAY_PREFIX
55                              + Base64.getEncoder().encodeToString((byte[]) v)
56                              + BYTE_ARRAY_SUFFIX);
57                  } else {
58                      jgen.writeString(v.toString());
59                  }
60              }
61              jgen.writeEndArray();
62          }
63      }
64  }