Skip to content

Commit 46c41e3

Browse files
authored
Fix/disposition not deserializing default field (#342)
* 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
1 parent 4518bfc commit 46c41e3

File tree

6 files changed

+123
-249
lines changed

6 files changed

+123
-249
lines changed

src/main/java/net/bramp/ffmpeg/FFmpegUtils.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import net.bramp.commons.lang3.math.gson.FractionAdapter;
1515
import net.bramp.ffmpeg.adapter.FFmpegPacketsAndFramesAdapter;
1616
import net.bramp.ffmpeg.gson.LowercaseEnumTypeAdapterFactory;
17-
import net.bramp.ffmpeg.gson.NamedBitsetAdapter;
18-
import net.bramp.ffmpeg.probe.FFmpegDisposition;
1917
import net.bramp.ffmpeg.probe.FFmpegFrameOrPacket;
2018
import org.apache.commons.lang3.math.Fraction;
2119

@@ -132,8 +130,6 @@ private static Gson setupGson() {
132130
builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
133131
builder.registerTypeAdapter(Fraction.class, new FractionAdapter());
134132
builder.registerTypeAdapter(FFmpegFrameOrPacket.class, new FFmpegPacketsAndFramesAdapter());
135-
builder.registerTypeAdapter(
136-
FFmpegDisposition.class, new NamedBitsetAdapter<>(FFmpegDisposition.class));
137133

138134
return builder.create();
139135
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.bramp.ffmpeg.adapter;
2+
3+
import java.lang.reflect.Type;
4+
import com.google.gson.*;
5+
6+
public class BooleanTypeAdapter implements JsonDeserializer<Boolean> {
7+
@Override
8+
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
9+
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean()) {
10+
return json.getAsBoolean();
11+
}
12+
13+
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
14+
String jsonValue = json.getAsString();
15+
if (jsonValue.equalsIgnoreCase("true")) {
16+
return true;
17+
} else if (jsonValue.equalsIgnoreCase("false")) {
18+
return false;
19+
} else {
20+
return null;
21+
}
22+
}
23+
24+
return json.getAsInt() != 0;
25+
}
26+
}

src/main/java/net/bramp/ffmpeg/gson/NamedBitsetAdapter.java

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/main/java/net/bramp/ffmpeg/probe/FFmpegDisposition.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,56 @@
11
package net.bramp.ffmpeg.probe;
22

3+
import com.google.gson.annotations.JsonAdapter;
4+
import com.google.gson.annotations.SerializedName;
35
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
6+
import net.bramp.ffmpeg.adapter.BooleanTypeAdapter;
47

58
/** Represents the AV_DISPOSITION_* fields */
69
@SuppressFBWarnings(
710
value = {"UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD"},
811
justification = "POJO objects where the fields are populated by gson")
912
public class FFmpegDisposition {
13+
@SerializedName("default")
14+
@JsonAdapter(BooleanTypeAdapter.class)
1015
public boolean _default;
16+
17+
@JsonAdapter(BooleanTypeAdapter.class)
1118
public boolean dub;
19+
20+
@JsonAdapter(BooleanTypeAdapter.class)
1221
public boolean original;
22+
23+
@JsonAdapter(BooleanTypeAdapter.class)
1324
public boolean comment;
25+
26+
@JsonAdapter(BooleanTypeAdapter.class)
1427
public boolean lyrics;
28+
29+
@JsonAdapter(BooleanTypeAdapter.class)
1530
public boolean karaoke;
31+
32+
@JsonAdapter(BooleanTypeAdapter.class)
1633
public boolean forced;
34+
35+
@JsonAdapter(BooleanTypeAdapter.class)
1736
public boolean hearing_impaired;
37+
38+
@JsonAdapter(BooleanTypeAdapter.class)
1839
public boolean visual_impaired;
40+
41+
@JsonAdapter(BooleanTypeAdapter.class)
1942
public boolean clean_effects;
43+
44+
@JsonAdapter(BooleanTypeAdapter.class)
2045
public boolean attached_pic;
46+
47+
@JsonAdapter(BooleanTypeAdapter.class)
2148
public boolean captions;
49+
50+
@JsonAdapter(BooleanTypeAdapter.class)
2251
public boolean descriptions;
52+
53+
@JsonAdapter(BooleanTypeAdapter.class)
2354
public boolean metadata;
2455

2556
public boolean isDefault() {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package net.bramp.ffmpeg.adapter;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.annotations.JsonAdapter;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertTrue;
10+
import static org.junit.Assert.assertFalse;
11+
12+
public class BooleanTypeAdapterTest {
13+
static class Set {
14+
@JsonAdapter(BooleanTypeAdapter.class)
15+
public boolean a;
16+
}
17+
18+
static Gson gson;
19+
20+
@BeforeClass
21+
public static void setupGson() {
22+
gson = new GsonBuilder().create();
23+
}
24+
25+
@Test
26+
public void testReadTrue() {
27+
Set s = gson.fromJson("{\"a\":true}", Set.class);
28+
assertTrue(s.a);
29+
}
30+
31+
@Test
32+
public void testReadFalse() {
33+
Set s = gson.fromJson("{\"a\":false}", Set.class);
34+
assertFalse(s.a);
35+
}
36+
37+
@Test
38+
public void testReadStringTrue() {
39+
Set s = gson.fromJson("{\"a\":\"true\"}", Set.class);
40+
assertTrue(s.a);
41+
}
42+
43+
@Test
44+
public void testReadStringFalse() {
45+
Set s = gson.fromJson("{\"a\":\"false\"}", Set.class);
46+
assertFalse(s.a);
47+
}
48+
49+
@Test
50+
public void testRead1() {
51+
Set s = gson.fromJson("{\"a\":1}", Set.class);
52+
assertTrue(s.a);
53+
}
54+
55+
@Test
56+
public void testRead0() {
57+
Set s = gson.fromJson("{\"a\":0}", Set.class);
58+
assertFalse(s.a);
59+
}
60+
61+
@Test
62+
public void testReadNull() {
63+
Set s = gson.fromJson("{\"a\":null}", Set.class);
64+
assertFalse(s.a);
65+
}
66+
}

0 commit comments

Comments
 (0)