|
2 | 2 | // Licensed under the MIT License. |
3 | 3 | package com.microsoft.durabletask; |
4 | 4 |
|
| 5 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 6 | +import com.fasterxml.jackson.core.JsonParser; |
| 7 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 8 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 9 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 10 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 11 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 12 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 13 | + |
5 | 14 | import javax.annotation.Nonnull; |
| 15 | +import java.io.IOException; |
6 | 16 | import java.util.Locale; |
7 | 17 | import java.util.Objects; |
8 | 18 |
|
|
11 | 21 | * <p> |
12 | 22 | * The name typically corresponds to the entity class/type name, and the key identifies the specific |
13 | 23 | * entity instance (e.g., a user ID or account number). |
| 24 | + * <p> |
| 25 | + * Serializes to and deserializes from a compact string format {@code @{name}@{key}}, |
| 26 | + * matching the .NET SDK's {@code EntityInstanceId} JSON representation. |
14 | 27 | */ |
| 28 | +@JsonSerialize(using = EntityInstanceId.Serializer.class) |
| 29 | +@JsonDeserialize(using = EntityInstanceId.Deserializer.class) |
15 | 30 | public final class EntityInstanceId implements Comparable<EntityInstanceId> { |
16 | 31 | private final String name; |
17 | 32 | private final String key; |
@@ -116,4 +131,26 @@ public int compareTo(@Nonnull EntityInstanceId other) { |
116 | 131 | } |
117 | 132 | return this.key.compareTo(other.key); |
118 | 133 | } |
| 134 | + |
| 135 | + /** |
| 136 | + * Jackson serializer that writes an {@code EntityInstanceId} as a compact {@code "@name@key"} string. |
| 137 | + */ |
| 138 | + static class Serializer extends JsonSerializer<EntityInstanceId> { |
| 139 | + @Override |
| 140 | + public void serialize(EntityInstanceId value, JsonGenerator gen, SerializerProvider serializers) |
| 141 | + throws IOException { |
| 142 | + gen.writeString(value.toString()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Jackson deserializer that reads an {@code EntityInstanceId} from a compact {@code "@name@key"} string. |
| 148 | + */ |
| 149 | + static class Deserializer extends JsonDeserializer<EntityInstanceId> { |
| 150 | + @Override |
| 151 | + public EntityInstanceId deserialize(JsonParser p, DeserializationContext ctxt) |
| 152 | + throws IOException { |
| 153 | + return EntityInstanceId.fromString(p.getText()); |
| 154 | + } |
| 155 | + } |
119 | 156 | } |
0 commit comments