File tree Expand file tree Collapse file tree
src/main/java/com/grack/nanojson Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33This is a fork of [ nanojson] ( https://github.com/mmastrac/nanojson ) for use by NewPipe(Extractor). It has the following changes:
44
55- It returns an empty ` JsonObject ` or ` JsonArray ` by default instead of ` null ` , preventing ` NullPointerException ` s.
6- - It accepts JS-like JSON, such as ` { this: 'is', an: 'example' } ` .
6+ - It accepts JS-like JSON, such as ` { this: 'is', an: 'example' } ` .
7+ - Added `` JsonArray#streamAs `` and `` JsonArray#streamAsJsonObjects `` shortcut methods.
Original file line number Diff line number Diff line change 1818import java .util .ArrayList ;
1919import java .util .Arrays ;
2020import java .util .Collection ;
21+ import java .util .stream .Stream ;
2122
2223/**
2324 * Extends an {@link ArrayList} with helper methods to determine the underlying JSON type of the list element.
@@ -253,4 +254,20 @@ public boolean isNumber(int key) {
253254 public boolean isString (int key ) {
254255 return get (key ) instanceof String ;
255256 }
257+
258+ /**
259+ * Shortcut method to create a stream with only objects of these classes.
260+ */
261+ public <T > Stream <T > streamAs (Class <T > clazz ) {
262+ return stream ()
263+ .filter (clazz ::isInstance )
264+ .map (clazz ::cast );
265+ }
266+
267+ /**
268+ * Shortcut method to create a stream that only contains {@link JsonObject JsonObjects}.
269+ */
270+ public Stream <JsonObject > streamAsJsonObjects () {
271+ return streamAs (JsonObject .class );
272+ }
256273}
You can’t perform that action at this time.
0 commit comments