-
Notifications
You must be signed in to change notification settings - Fork 215
/
JjwtSerializer.java
120 lines (103 loc) · 4.43 KB
/
JjwtSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.jwt;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.io.SerializationException;
import io.jsonwebtoken.io.Serializer;
import io.jsonwebtoken.lang.Collections;
import io.jsonwebtoken.lang.DateFormats;
import io.jsonwebtoken.lang.Objects;
/**
* JJWT library Serializer implementation which translates Java Objects (e.g. Maps) to JSON strings.
*/
@Immutable
public final class JjwtSerializer implements Serializer<Map<String, ?>> {
private static Serializer<Map<String, ?>> instance;
/**
* @return the instance of {@link JjwtSerializer}.
*/
public static Serializer<Map<String, ?>> getInstance() {
if (instance == null) {
instance = new JjwtSerializer();
}
return instance;
}
@Override
public byte[] serialize(final Map<String, ?> t) {
try {
return toJson(t).toString().getBytes(StandardCharsets.UTF_8);
} catch (final SerializationException se) {
throw se;
} catch (final Exception e) {
throw new SerializationException("Unable to serialize object of type " +
Optional.ofNullable(t).map(obj -> obj.getClass().getName()).orElse("<null>") +
" to JSON: " + e.getMessage(), e);
}
}
private static JsonValue toJson(final Object input) {
if (input == null) {
return JsonFactory.nullLiteral();
} else if (input instanceof Boolean aBoolean) {
return JsonFactory.newValue(aBoolean);
} else if (input instanceof Byte || input instanceof Short || input instanceof Integer) {
return JsonFactory.newValue((int) input);
} else if (input instanceof Long aLong) {
return JsonFactory.newValue(aLong);
} else if (input instanceof Float aFloat) {
return JsonFactory.newValue(aFloat);
} else if (input instanceof Double aDouble) {
return JsonFactory.newValue(aDouble);
} else if (input instanceof Character || input instanceof String || input instanceof Enum) {
return JsonFactory.newValue(input.toString());
} else if (input instanceof Calendar calendar) {
return JsonFactory.newValue(DateFormats.formatIso8601(calendar.getTime()));
} else if (input instanceof Date date) {
return JsonFactory.newValue(DateFormats.formatIso8601(date));
} else if (input instanceof byte[] bytes) {
return JsonFactory.newValue(Encoders.BASE64.encode((bytes)));
} else if (input instanceof char[] chars) {
return JsonFactory.newValue(new String(chars));
} else if (input instanceof Map map) {
return toJsonObject(map);
} else if (input instanceof Collection<?> collection) {
return toJsonArray(collection);
} else if (Objects.isArray(input)) {
return toJsonArray(Collections.arrayToList(input));
}
throw new SerializationException("Unable to serialize object of type " + input.getClass().getName() +
" to JSON using known heuristics.");
}
private static JsonObject toJsonObject(final Map<?, ?> map) {
return map.entrySet().stream()
.map(entry -> JsonField.newInstance(String.valueOf(entry.getKey()), toJson(entry.getValue())))
.collect(JsonCollectors.fieldsToObject());
}
private static JsonArray toJsonArray(final Collection<?> c) {
return c.stream()
.map(JjwtSerializer::toJson)
.collect(JsonCollectors.valuesToArray());
}
}