Skip to content

Commit

Permalink
switch to jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinckles committed Jul 16, 2023
1 parent ed20a57 commit 103a8e0
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 82 deletions.
4 changes: 2 additions & 2 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

<!-- json -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

Expand Down
7 changes: 3 additions & 4 deletions cli/src/main/java/com/pwinckles/cassette/cli/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pwinckles.cassette.cli;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pwinckles.cassette.common.model.Data;
import com.pwinckles.cassette.common.model.Move;
import com.pwinckles.cassette.common.model.MoveAccuracy;
Expand All @@ -8,7 +9,6 @@
import com.pwinckles.cassette.common.model.Species;
import com.pwinckles.cassette.index.Index;
import com.pwinckles.cassette.index.SearchResult;
import io.avaje.jsonb.Jsonb;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -187,10 +187,9 @@ private static void print(Move move) {
}

private static Data readData() throws IOException {
var jsonb = Jsonb.builder().build();
var dataJsonType = jsonb.type(Data.class);
var objectMapper = new ObjectMapper();
try (var is = new BufferedInputStream(Main.class.getResourceAsStream("/data.json"))) {
return dataJsonType.fromJson(is);
return objectMapper.readValue(is, Data.class);
}
}

Expand Down
9 changes: 2 additions & 7 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@
<dependencies>
<!-- json -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<scope>provided</scope>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

<!-- Lang ergonomics -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Json
@RecordBuilderFull
public record BootlegMoves(List<String> common, Map<SpeciesType, List<String>> typeSpecific) {}
public record BootlegMoves(List<String> common, Map<SpeciesType, List<String>> typeSpecific) {

public BootlegMoves(List<String> common, Map<SpeciesType, List<String>> typeSpecific) {
this.common = Objects.requireNonNull(common);
this.typeSpecific = Objects.requireNonNull(typeSpecific);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.Objects;

@Json
@RecordBuilderFull
public record CompatibleSpecies(String name, MoveSource source) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import java.util.Objects;

@Json
@RecordBuilderFull
public record Data(List<Species> species, List<Move> moves) {
public Data(List<Species> species, List<Move> moves) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Json
@RecordBuilderFull
public record Move(
String name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@Json
@Json.SubType(type = MoveAccuracy.Unavoidable.class, name = "UNAVOIDABLE")
@Json.SubType(type = MoveAccuracy.Avoidable.class, name = "AVOIDABLE")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = MoveAccuracy.Unavoidable.class, name = "UNAVOIDABLE"),
@JsonSubTypes.Type(value = MoveAccuracy.Avoidable.class, name = "AVOIDABLE")
})
public interface MoveAccuracy {

record Unavoidable() implements MoveAccuracy {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;

@Json
@RecordBuilderFull
public record MoveHits(int min, int max) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import java.util.Objects;

@Json
@RecordBuilderFull
public record Species(
String name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;

@Json
@RecordBuilderFull
public record SpeciesMoves(List<String> initial, List<String> learned, List<String> compatible, BootlegMoves bootleg) {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;

@Json
@RecordBuilderFull
public record SpeciesStats(
int hp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.Objects;

@Json
@RecordBuilderFull
public record StatusEffect(String name, Kind kind) {

Expand Down
15 changes: 7 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<slf4j.version>2.0.7</slf4j.version>
<record-builder.version>36</record-builder.version>
<lucene.version>9.7.0</lucene.version>
<avaje.jsonb.version>1.6-RC5</avaje.jsonb.version>
<jackson.version>2.15.2</jackson.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -71,15 +71,14 @@

<!-- json -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<version>${avaje.jsonb.version}</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<version>${avaje.jsonb.version}</version>
<scope>provided</scope>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- Logging -->
Expand Down
9 changes: 2 additions & 7 deletions scraper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@

<!-- json -->
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<scope>provided</scope>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<!-- Lang ergonomics -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.pwinckles.cassette.scraper;

import com.pwinckles.cassette.common.model.Data;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pwinckles.cassette.common.model.DataBuilder;
import com.pwinckles.cassette.common.model.Move;
import com.pwinckles.cassette.common.model.Species;
import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
Expand All @@ -17,15 +15,10 @@ public class Assembler {

private static final Logger log = LoggerFactory.getLogger(Assembler.class);

private JsonType<Data> dataJsonType;
private final JsonType<Species> speciesJsonType;
private final JsonType<Move> moveJsonType;
private final ObjectMapper objectMapper;

public Assembler() {
var jsonb = Jsonb.builder().build();
this.dataJsonType = jsonb.type(Data.class);
this.speciesJsonType = jsonb.type(Species.class);
this.moveJsonType = jsonb.type(Move.class);
this.objectMapper = new ObjectMapper();
}

public void assemble(Path dir) throws IOException {
Expand All @@ -35,21 +28,19 @@ public void assemble(Path dir) throws IOException {
var data = DataBuilder.builder();

try (var files = Files.list(dir.resolve(Constants.SPECIES_DIR))) {
files.map(file -> readJson(file, speciesJsonType)).forEach(data::addSpecies);
files.map(file -> readJson(file, Species.class)).forEach(data::addSpecies);
}

try (var files = Files.list(dir.resolve(Constants.MOVES_DIR))) {
files.map(file -> readJson(file, moveJsonType)).forEach(data::addMoves);
files.map(file -> readJson(file, Move.class)).forEach(data::addMoves);
}

try (var writer = Files.newBufferedWriter(outputFile)) {
dataJsonType.toJson(data.build(), writer);
}
objectMapper.writeValue(outputFile.toFile(), data.build());
}

private <T> T readJson(Path file, JsonType<T> jsonType) {
try (var reader = Files.newBufferedReader(file)) {
return jsonType.fromJson(reader);
private <T> T readJson(Path file, Class<T> clazz) {
try {
return objectMapper.readValue(file.toFile(), clazz);
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.pwinckles.cassette.scraper;

import com.pwinckles.cassette.common.model.Move;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.pwinckles.cassette.common.model.MoveBuilder;
import com.pwinckles.cassette.common.model.Species;
import com.pwinckles.cassette.common.model.SpeciesBuilder;
import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -27,17 +25,14 @@ public class WikiScraper {
private final SpeciesScraper speciesScraper;
private final MoveScraper moveScraper;

private final JsonType<Species> speciesJsonType;
private final JsonType<Move> moveJsonType;
private final ObjectMapper objectMapper;

private final Duration wait;

public WikiScraper() {
this.speciesScraper = new SpeciesScraper();
this.moveScraper = new MoveScraper();
var jsonb = Jsonb.builder().build();
this.speciesJsonType = jsonb.type(Species.class);
this.moveJsonType = jsonb.type(Move.class);
this.objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
this.wait = Duration.ofSeconds(1);
}

Expand All @@ -62,8 +57,7 @@ private void scrapeSpecies(Path speciesDir) throws IOException, InterruptedExcep
} else {
var url = BASE_URL + s.ref;
var scraped = SpeciesBuilder.from(speciesScraper.scrape(url)).withUrl(url);
var json = speciesJsonType.toJsonPretty(scraped);
Files.writeString(output, json);
objectMapper.writeValue(output.toFile(), scraped);

if (it.hasNext()) {
Thread.sleep(wait.toMillis());
Expand All @@ -88,8 +82,7 @@ private void scrapeMoves(Path movesDir) throws IOException, InterruptedException
} else {
var url = BASE_URL + move.ref;
var scraped = MoveBuilder.from(moveScraper.scrape(url)).withUrl(url);
var json = moveJsonType.toJsonPretty(scraped);
Files.writeString(output, json);
objectMapper.writeValue(output.toFile(), scraped);

if (it.hasNext()) {
Thread.sleep(wait.toMillis());
Expand Down

0 comments on commit 103a8e0

Please sign in to comment.