-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathJsonSerdes.java
More file actions
209 lines (191 loc) · 8.04 KB
/
JsonSerdes.java
File metadata and controls
209 lines (191 loc) · 8.04 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import dev.restate.common.Slice;
import dev.restate.common.function.ThrowingBiConsumer;
import dev.restate.common.function.ThrowingFunction;
import dev.restate.serde.Serde;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import org.jspecify.annotations.NonNull;
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public abstract class JsonSerdes {
private JsonSerdes() {}
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull String> STRING =
usingJackson(
"string",
JsonGenerator::writeString,
p -> {
if (p.nextToken() != JsonToken.VALUE_STRING) {
throw new IllegalStateException(
"Expecting token " + JsonToken.VALUE_STRING + ", got " + p.getCurrentToken());
}
return p.getText();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Boolean> BOOLEAN =
usingJackson(
"boolean",
JsonGenerator::writeBoolean,
p -> {
p.nextToken();
return p.getBooleanValue();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Byte> BYTE =
usingJackson(
"number",
JsonGenerator::writeNumber,
p -> {
p.nextToken();
return p.getByteValue();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Short> SHORT =
usingJackson(
"number",
JsonGenerator::writeNumber,
p -> {
p.nextToken();
return p.getShortValue();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Integer> INT =
usingJackson(
"number",
JsonGenerator::writeNumber,
p -> {
p.nextToken();
return p.getIntValue();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Long> LONG =
usingJackson(
"number",
JsonGenerator::writeNumber,
p -> {
p.nextToken();
return p.getLongValue();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Float> FLOAT =
usingJackson(
"number",
JsonGenerator::writeNumber,
p -> {
p.nextToken();
return p.getFloatValue();
});
/**
* @deprecated For primitive types, simply use the overloads of the {@link Context}/{@link
* dev.restate.sdk.common.StateKey}/{@link dev.restate.sdk.common.DurablePromiseKey} methods
* accepting {@link Class}, for example {@code ctx.run(String.class, myClosure)} or {@code
* ctx.awakeable(Integer.TYPE)} or {@code StateKey.of("key", String.class)}
*/
@Deprecated(since = "2.0", forRemoval = true)
public static final Serde<@NonNull Double> DOUBLE =
usingJackson(
"number",
JsonGenerator::writeNumber,
p -> {
p.nextToken();
return p.getDoubleValue();
});
// --- Helpers for jackson-core
private static final JsonFactory JSON_FACTORY = new JsonFactory();
private static <T extends @NonNull Object> Serde<T> usingJackson(
String type,
ThrowingBiConsumer<JsonGenerator, T> serializer,
ThrowingFunction<JsonParser, T> deserializer) {
return new Serde<>() {
@Override
public Schema jsonSchema() {
return new JsonSchema(Map.of("type", type));
}
@Override
public Slice serialize(T value) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (JsonGenerator gen = JSON_FACTORY.createGenerator(outputStream)) {
serializer.asBiConsumer().accept(gen, value);
} catch (IOException e) {
throw new RuntimeException("Cannot create JsonGenerator", e);
}
return Slice.wrap(outputStream.toByteArray());
}
@Override
public T deserialize(@NonNull Slice value) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(value.toByteArray());
try (JsonParser parser = JSON_FACTORY.createParser(inputStream)) {
return deserializer.asFunction().apply(parser);
} catch (IOException e) {
throw new RuntimeException("Cannot create JsonGenerator", e);
}
}
@Override
public String contentType() {
return "application/json";
}
};
}
}