Skip to content

Commit

Permalink
Fix/disposition not deserializing default field (#342)
Browse files Browse the repository at this point in the history
* fix(ffprobe): Correctly deserialize dispositions

* chore: Add missing @OverRide annotation

* chore: Check if element is primitive before converting

* test: Add explicit tests for string based boolean
  • Loading branch information
Euklios authored Aug 9, 2024
1 parent 4518bfc commit 46c41e3
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 249 deletions.
4 changes: 0 additions & 4 deletions src/main/java/net/bramp/ffmpeg/FFmpegUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import net.bramp.commons.lang3.math.gson.FractionAdapter;
import net.bramp.ffmpeg.adapter.FFmpegPacketsAndFramesAdapter;
import net.bramp.ffmpeg.gson.LowercaseEnumTypeAdapterFactory;
import net.bramp.ffmpeg.gson.NamedBitsetAdapter;
import net.bramp.ffmpeg.probe.FFmpegDisposition;
import net.bramp.ffmpeg.probe.FFmpegFrameOrPacket;
import org.apache.commons.lang3.math.Fraction;

Expand Down Expand Up @@ -132,8 +130,6 @@ private static Gson setupGson() {
builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
builder.registerTypeAdapter(Fraction.class, new FractionAdapter());
builder.registerTypeAdapter(FFmpegFrameOrPacket.class, new FFmpegPacketsAndFramesAdapter());
builder.registerTypeAdapter(
FFmpegDisposition.class, new NamedBitsetAdapter<>(FFmpegDisposition.class));

return builder.create();
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/bramp/ffmpeg/adapter/BooleanTypeAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.bramp.ffmpeg.adapter;

import java.lang.reflect.Type;
import com.google.gson.*;

public class BooleanTypeAdapter implements JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean()) {
return json.getAsBoolean();
}

if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
String jsonValue = json.getAsString();
if (jsonValue.equalsIgnoreCase("true")) {
return true;
} else if (jsonValue.equalsIgnoreCase("false")) {
return false;
} else {
return null;
}
}

return json.getAsInt() != 0;
}
}
142 changes: 0 additions & 142 deletions src/main/java/net/bramp/ffmpeg/gson/NamedBitsetAdapter.java

This file was deleted.

31 changes: 31 additions & 0 deletions src/main/java/net/bramp/ffmpeg/probe/FFmpegDisposition.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
package net.bramp.ffmpeg.probe;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.bramp.ffmpeg.adapter.BooleanTypeAdapter;

/** Represents the AV_DISPOSITION_* fields */
@SuppressFBWarnings(
value = {"UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD"},
justification = "POJO objects where the fields are populated by gson")
public class FFmpegDisposition {
@SerializedName("default")
@JsonAdapter(BooleanTypeAdapter.class)
public boolean _default;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean dub;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean original;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean comment;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean lyrics;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean karaoke;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean forced;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean hearing_impaired;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean visual_impaired;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean clean_effects;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean attached_pic;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean captions;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean descriptions;

@JsonAdapter(BooleanTypeAdapter.class)
public boolean metadata;

public boolean isDefault() {
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/net/bramp/ffmpeg/adapter/BooleanTypeAdapterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package net.bramp.ffmpeg.adapter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.JsonAdapter;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class BooleanTypeAdapterTest {
static class Set {
@JsonAdapter(BooleanTypeAdapter.class)
public boolean a;
}

static Gson gson;

@BeforeClass
public static void setupGson() {
gson = new GsonBuilder().create();
}

@Test
public void testReadTrue() {
Set s = gson.fromJson("{\"a\":true}", Set.class);
assertTrue(s.a);
}

@Test
public void testReadFalse() {
Set s = gson.fromJson("{\"a\":false}", Set.class);
assertFalse(s.a);
}

@Test
public void testReadStringTrue() {
Set s = gson.fromJson("{\"a\":\"true\"}", Set.class);
assertTrue(s.a);
}

@Test
public void testReadStringFalse() {
Set s = gson.fromJson("{\"a\":\"false\"}", Set.class);
assertFalse(s.a);
}

@Test
public void testRead1() {
Set s = gson.fromJson("{\"a\":1}", Set.class);
assertTrue(s.a);
}

@Test
public void testRead0() {
Set s = gson.fromJson("{\"a\":0}", Set.class);
assertFalse(s.a);
}

@Test
public void testReadNull() {
Set s = gson.fromJson("{\"a\":null}", Set.class);
assertFalse(s.a);
}
}
Loading

0 comments on commit 46c41e3

Please sign in to comment.