Skip to content

Commit

Permalink
Add support for decoding legacy palletes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronjyoder committed Jun 4, 2022
1 parent a8bb6fa commit 32aff2b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
// gradlew publishToSonatype closeSonatypeStagingRepository for staging and manual release
// gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository for automatic release

val versionObject = Version(breaking = "1", minor = "0", nonbreaking = "0", revision = "0", date = "2206")
val versionObject = Version(breaking = "1", minor = "0", nonbreaking = "0", revision = "1", date = "2206")
project.group = "com.riskrieg"
project.version = "$versionObject"

Expand All @@ -29,7 +29,7 @@ dependencies {
api("com.github.spotbugs:spotbugs-annotations:4.6.0")

implementation("com.riskrieg:map:1.0.0-0.2206")
implementation("com.riskrieg:palette:1.0.0-0.2206")
implementation("com.riskrieg:palette:1.1.0-0.2206")

implementation("com.fasterxml.jackson.core:jackson-databind:2.13.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3")
Expand Down
46 changes: 43 additions & 3 deletions src/main/java/com/riskrieg/codec/decode/RkpDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.riskrieg.codec.internal.json.JsonHelper;
import com.riskrieg.palette.RkpPalette;
import com.riskrieg.palette.legacy.LegacyPalette;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -32,22 +33,61 @@ public final class RkpDecoder implements Decoder<RkpPalette> {
@Override
public RkpPalette decode(Path path) throws IOException {
Objects.requireNonNull(path);
return JsonHelper.read(path, RkpPalette.class);
try {
return JsonHelper.read(path, RkpPalette.class);
} catch (Exception e) {
return decodeLegacy(path);
}
}

private RkpPalette decodeLegacy(Path path) throws IOException {
try {
return JsonHelper.read(path, LegacyPalette.class).toRkpPalette();
} catch (NullPointerException e) {
return null;
}
}

@Override
public RkpPalette decode(URL url) throws IOException {
Objects.requireNonNull(url);
try {
try (InputStream inputStream = url.openStream()) {
final byte[] data = inputStream.readAllBytes();
return JsonHelper.read(new String(data, StandardCharsets.UTF_8), RkpPalette.class);
}
} catch (Exception e) {
return decodeLegacy(url);
}
}

private RkpPalette decodeLegacy(URL url) throws IOException {
try (InputStream inputStream = url.openStream()) {
final byte[] data = inputStream.readAllBytes();
return JsonHelper.read(new String(data, StandardCharsets.UTF_8), RkpPalette.class);
LegacyPalette legacy = JsonHelper.read(new String(data, StandardCharsets.UTF_8), LegacyPalette.class);
return legacy.toRkpPalette();
} catch (NullPointerException e) {
return null;
}
}

@Override
public RkpPalette decode(byte[] data) throws IOException {
Objects.requireNonNull(data);
return JsonHelper.read(new String(data, StandardCharsets.UTF_8), RkpPalette.class);
try {
return JsonHelper.read(new String(data, StandardCharsets.UTF_8), RkpPalette.class);
} catch (Exception e) {
return decodeLegacy(data);
}
}

private RkpPalette decodeLegacy(byte[] data) throws IOException {
try {
LegacyPalette legacy = JsonHelper.read(new String(data, StandardCharsets.UTF_8), LegacyPalette.class);
return legacy.toRkpPalette();
} catch (NullPointerException e) {
return null;
}
}

}

0 comments on commit 32aff2b

Please sign in to comment.