-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Json writer and reader for enum with generic value (#221)
- Loading branch information
Showing
22 changed files
with
409 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
...on-processor/src/test/java/ru/tinkoff/kora/json/annotation/processor/dto/DtoWithEnum.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 15 additions & 8 deletions
23
json/json-common/src/main/java/ru/tinkoff/kora/json/common/EnumJsonWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,35 @@ | ||
package ru.tinkoff.kora.json.common; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.io.SerializedString; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.function.Function; | ||
|
||
public final class EnumJsonWriter<T extends Enum<T>> implements JsonWriter<T> { | ||
private final SerializedString[] values; | ||
public final class EnumJsonWriter<T extends Enum<T>, V> implements JsonWriter<T> { | ||
private final RawJson[] values; | ||
|
||
public EnumJsonWriter(T[] values, Function<T, String> mapper) { | ||
this.values = new SerializedString[values.length]; | ||
public EnumJsonWriter(T[] values, Function<T, V> valueExtractor, JsonWriter<V> valueWriter) { | ||
this.values = new RawJson[values.length]; | ||
for (int i = 0; i < values.length; i++) { | ||
this.values[i] = new SerializedString(mapper.apply(values[i])); | ||
var enumValue = values[i]; | ||
var value = valueExtractor.apply(enumValue); | ||
try { | ||
var bytes = valueWriter.toByteArray(value); | ||
this.values[i] = new RawJson(bytes); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void write(JsonGenerator gen, @Nullable T object) throws IOException { | ||
if (object == null) { | ||
gen.writeNull(); | ||
} else { | ||
gen.writeString(this.values[object.ordinal()]); | ||
return; | ||
} | ||
gen.writeRawValue(this.values[object.ordinal()]); | ||
} | ||
} |
Oops, something went wrong.