Skip to content

Commit 1d5f22e

Browse files
Stypoxlitetex
authored andcommitted
Fix checkstyle issues & more in JsonUtils
Also use Java 8 streams and extract duplicate code to getInstanceOf function
1 parent 87d2834 commit 1d5f22e

1 file changed

Lines changed: 61 additions & 58 deletions

File tree

  • extractor/src/main/java/org/schabi/newpipe/extractor/utils

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,111 +12,114 @@
1212
import java.util.ArrayList;
1313
import java.util.Arrays;
1414
import java.util.List;
15+
import java.util.stream.Collectors;
1516

1617
import javax.annotation.Nonnull;
1718
import javax.annotation.Nullable;
1819

19-
public class JsonUtils {
20+
public final class JsonUtils {
2021
public static final JsonObject EMPTY_OBJECT = new JsonObject();
2122
public static final JsonArray EMPTY_ARRAY = new JsonArray();
2223

2324
private JsonUtils() {
2425
}
2526

2627
@Nonnull
27-
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
28+
public static Object getValue(@Nonnull final JsonObject object,
29+
@Nonnull final String path) throws ParsingException {
2830

29-
List<String> keys = Arrays.asList(path.split("\\."));
30-
object = getObject(object, keys.subList(0, keys.size() - 1));
31-
if (null == object) throw new ParsingException("Unable to get " + path);
32-
Object result = object.get(keys.get(keys.size() - 1));
33-
if (null == result) throw new ParsingException("Unable to get " + path);
31+
final List<String> keys = Arrays.asList(path.split("\\."));
32+
final JsonObject parentObject = getObject(object, keys.subList(0, keys.size() - 1));
33+
if (parentObject == null) {
34+
throw new ParsingException("Unable to get " + path);
35+
}
36+
37+
final Object result = object.get(keys.get(keys.size() - 1));
38+
if (result == null) {
39+
throw new ParsingException("Unable to get " + path);
40+
}
3441
return result;
3542
}
3643

37-
@Nonnull
38-
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
39-
Object value = getValue(object, path);
40-
if (value instanceof String) {
41-
return (String) value;
44+
private static <T> T getInstanceOf(@Nonnull final JsonObject object,
45+
@Nonnull final String path,
46+
@Nonnull final Class<T> klass) throws ParsingException {
47+
final Object value = getValue(object, path);
48+
if (klass.isInstance(value)) {
49+
return klass.cast(value);
4250
} else {
43-
throw new ParsingException("Unable to get " + path);
51+
throw new ParsingException("Wrong data type at path " + path);
4452
}
4553
}
4654

4755
@Nonnull
48-
public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
49-
Object value = getValue(object, path);
50-
if (value instanceof Boolean) {
51-
return (Boolean) value;
52-
} else {
53-
throw new ParsingException("Unable to get " + path);
54-
}
56+
public static String getString(@Nonnull final JsonObject object, @Nonnull final String path)
57+
throws ParsingException {
58+
return getInstanceOf(object, path, String.class);
5559
}
5660

5761
@Nonnull
58-
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
59-
Object value = getValue(object, path);
60-
if (value instanceof Number) {
61-
return (Number) value;
62-
} else {
63-
throw new ParsingException("Unable to get " + path);
64-
}
62+
public static Boolean getBoolean(@Nonnull final JsonObject object,
63+
@Nonnull final String path) throws ParsingException {
64+
return getInstanceOf(object, path, Boolean.class);
6565
}
6666

6767
@Nonnull
68-
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
69-
Object value = getValue(object, path);
70-
if (value instanceof JsonObject) {
71-
return (JsonObject) value;
72-
} else {
73-
throw new ParsingException("Unable to get " + path);
74-
}
68+
public static Number getNumber(@Nonnull final JsonObject object,
69+
@Nonnull final String path)
70+
throws ParsingException {
71+
return getInstanceOf(object, path, Number.class);
7572
}
7673

7774
@Nonnull
78-
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
79-
Object value = getValue(object, path);
80-
if (value instanceof JsonArray) {
81-
return (JsonArray) value;
82-
} else {
83-
throw new ParsingException("Unable to get " + path);
84-
}
75+
public static JsonObject getObject(@Nonnull final JsonObject object,
76+
@Nonnull final String path) throws ParsingException {
77+
return getInstanceOf(object, path, JsonObject.class);
8578
}
8679

8780
@Nonnull
88-
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
81+
public static JsonArray getArray(@Nonnull final JsonObject object, @Nonnull final String path)
82+
throws ParsingException {
83+
return getInstanceOf(object, path, JsonArray.class);
84+
}
8985

90-
List<Object> result = new ArrayList<>();
86+
@Nonnull
87+
public static List<Object> getValues(@Nonnull final JsonArray array, @Nonnull final String path)
88+
throws ParsingException {
89+
90+
final List<Object> result = new ArrayList<>();
9191
for (int i = 0; i < array.size(); i++) {
92-
JsonObject obj = array.getObject(i);
92+
final JsonObject obj = array.getObject(i);
9393
result.add(getValue(obj, path));
9494
}
9595
return result;
9696
}
9797

9898
@Nullable
99-
private static JsonObject getObject(@Nonnull JsonObject object, @Nonnull List<String> keys) {
99+
private static JsonObject getObject(@Nonnull final JsonObject object,
100+
@Nonnull final List<String> keys) {
100101
JsonObject result = object;
101-
for (String key : keys) {
102+
for (final String key : keys) {
102103
result = result.getObject(key);
103-
if (null == result) break;
104+
if (result == null) {
105+
break;
106+
}
104107
}
105108
return result;
106109
}
107110

108111
public static JsonArray toJsonArray(final String responseBody) throws ParsingException {
109112
try {
110113
return JsonParser.array().from(responseBody);
111-
} catch (JsonParserException e) {
114+
} catch (final JsonParserException e) {
112115
throw new ParsingException("Could not parse JSON", e);
113116
}
114117
}
115118

116119
public static JsonObject toJsonObject(final String responseBody) throws ParsingException {
117120
try {
118121
return JsonParser.object().from(responseBody);
119-
} catch (JsonParserException e) {
122+
} catch (final JsonParserException e) {
120123
throw new ParsingException("Could not parse JSON", e);
121124
}
122125
}
@@ -128,10 +131,12 @@ public static JsonObject toJsonObject(final String responseBody) throws ParsingE
128131
* <p>Example HTML:</p>
129132
* <pre>
130133
* {@code
131-
* <p data-town="{&quot;name&quot;:&quot;Mycenae&quot;,&quot;country&quot;:&quot;Greece&quot;}">This is Sparta!</p>
134+
* <p data-town="{&quot;name&quot;:&quot;Mycenae&quot;,&quot;country&quot;:&quot;Greece&quot;}">
135+
* This is Sparta!</p>
132136
* }
133137
* </pre>
134-
* <p>Calling this function to get the attribute <code>data-town</code> returns the JsonObject for</p>
138+
* <p>Calling this function to get the attribute <code>data-town</code> returns the JsonObject
139+
* for</p>
135140
* <pre>
136141
* {@code
137142
* {
@@ -140,6 +145,7 @@ public static JsonObject toJsonObject(final String responseBody) throws ParsingE
140145
* }
141146
* }
142147
* </pre>
148+
*
143149
* @param html The HTML where the JSON we're looking for is stored inside a
144150
* variable inside some JavaScript block
145151
* @param variable Name of the variable
@@ -153,12 +159,9 @@ public static JsonObject getJsonData(final String html, final String variable)
153159
}
154160

155161
public static List<String> getStringListFromJsonArray(@Nonnull final JsonArray array) {
156-
final List<String> stringList = new ArrayList<>(array.size());
157-
for (Object o : array) {
158-
if (o instanceof String) {
159-
stringList.add((String) o);
160-
}
161-
}
162-
return stringList;
162+
return array.stream()
163+
.filter(String.class::isInstance)
164+
.map(String.class::cast)
165+
.collect(Collectors.toList());
163166
}
164167
}

0 commit comments

Comments
 (0)