Skip to content

Commit

Permalink
Guess with org.embulk.spi.json.JsonValue, instead of org.msgpack.valu…
Browse files Browse the repository at this point in the history
…e.Value
  • Loading branch information
dmikurube committed Sep 22, 2023
1 parent 3fa6872 commit 6b55264
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
3 changes: 1 addition & 2 deletions embulk-guess-json/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ java {

dependencies {
compileOnly "org.embulk:embulk-spi:0.11"
compileOnly "org.msgpack:msgpack-core:0.8.24"

implementation "org.embulk:embulk-util-config:0.3.4"
implementation "org.embulk:embulk-util-file:0.1.5"
implementation "org.embulk:embulk-util-json:0.2.2"
implementation "org.embulk:embulk-util-json:0.3.0"
}

embulkPlugin {
Expand Down
2 changes: 1 addition & 1 deletion embulk-guess-json/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ javax.validation:validation-api:1.1.0.Final=compileClasspath,runtimeClasspath
org.embulk:embulk-spi:0.11=compileClasspath
org.embulk:embulk-util-config:0.3.4=compileClasspath,runtimeClasspath
org.embulk:embulk-util-file:0.1.5=compileClasspath,runtimeClasspath
org.embulk:embulk-util-json:0.2.2=compileClasspath,runtimeClasspath
org.embulk:embulk-util-json:0.3.0=compileClasspath,runtimeClasspath
org.msgpack:msgpack-core:0.8.24=compileClasspath
org.slf4j:slf4j-api:2.0.7=compileClasspath
empty=
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import org.embulk.spi.BufferAllocator;
import org.embulk.spi.Exec;
import org.embulk.spi.GuessPlugin;
import org.embulk.spi.json.JsonValue;
import org.embulk.util.config.ConfigMapperFactory;
import org.embulk.util.file.FileInputInputStream;
import org.embulk.util.file.InputStreamFileInput;
import org.embulk.util.json.JsonParseException;
import org.embulk.util.json.JsonParser;
import org.msgpack.value.Value;
import org.embulk.util.json.JsonValueParser;

public class JsonGuessPlugin implements GuessPlugin {
@Override
Expand All @@ -45,17 +45,17 @@ public ConfigDiff guess(final ConfigSource config, final Buffer sample) {

final BufferAllocator bufferAllocator = Exec.getBufferAllocator();

// Use org.embulk.spi.json.JsonParser to respond to multi-line Json
final JsonParser.Stream jsonParser = newJsonParser(sample, bufferAllocator);
// Use org.embulk.util.json.JsonValueParser to respond to multi-line JSON
final JsonValueParser parser = newJsonParser(sample, bufferAllocator);

boolean oneJsonParsed = false;
try {
Value v = null;
while ((v = jsonParser.next()) != null) {
// "v" needs to be JSON object type (isMapValue) because:
JsonValue v = null;
while ((v = parser.readJsonValue()) != null) {
// "v" needs to be JSON object type (isJsonValue) because:
// 1) Single-column CSV can be mis-guessed as JSON if JSON non-objects are accepted.
// 2) JsonParserPlugin accepts only the JSON object type.
if (!v.isMapValue()) {
if (!v.isJsonObject()) {
throw new JsonParseException("v must be JSON object type");
}
oneJsonParsed = true;
Expand All @@ -76,7 +76,7 @@ public ConfigDiff guess(final ConfigSource config, final Buffer sample) {
return configDiff;
}

private static JsonParser.Stream newJsonParser(final Buffer buffer, final BufferAllocator bufferAllocator) {
private static JsonValueParser newJsonParser(final Buffer buffer, final BufferAllocator bufferAllocator) {
final ArrayList<InputStream> inputStreams = new ArrayList<>();
inputStreams.add(buildByteArrayInputStream(buffer));

Expand All @@ -85,7 +85,7 @@ private static JsonParser.Stream newJsonParser(final Buffer buffer, final Buffer
final FileInputInputStream input = new FileInputInputStream(new InputStreamFileInput(bufferAllocator, iteratorProvider));
input.nextFile();
try {
return (new JsonParser()).open(input);
return JsonValueParser.builder().build(input);
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
Expand Down

0 comments on commit 6b55264

Please sign in to comment.