|
17 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 | */ |
19 | 19 |
|
20 | | -package org.schabi.newpipe.local.subscription.services; |
21 | | - |
22 | | -import androidx.annotation.Nullable; |
23 | | - |
24 | | -import com.grack.nanojson.JsonAppendableWriter; |
25 | | -import com.grack.nanojson.JsonArray; |
26 | | -import com.grack.nanojson.JsonObject; |
27 | | -import com.grack.nanojson.JsonParser; |
28 | | -import com.grack.nanojson.JsonWriter; |
29 | | - |
30 | | -import org.schabi.newpipe.BuildConfig; |
31 | | -import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.InvalidSourceException; |
32 | | -import org.schabi.newpipe.extractor.subscription.SubscriptionItem; |
33 | | - |
34 | | -import java.io.InputStream; |
35 | | -import java.io.OutputStream; |
36 | | -import java.util.ArrayList; |
37 | | -import java.util.List; |
| 20 | +package org.schabi.newpipe.local.subscription.services |
| 21 | + |
| 22 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 23 | +import kotlinx.serialization.json.Json |
| 24 | +import kotlinx.serialization.json.decodeFromStream |
| 25 | +import kotlinx.serialization.json.encodeToStream |
| 26 | +import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.InvalidSourceException |
| 27 | +import org.schabi.newpipe.local.subscription.workers.SubscriptionData |
| 28 | +import org.schabi.newpipe.local.subscription.workers.SubscriptionItem |
| 29 | +import java.io.InputStream |
| 30 | +import java.io.OutputStream |
38 | 31 |
|
39 | 32 | /** |
40 | 33 | * A JSON implementation capable of importing and exporting subscriptions, it has the advantage |
41 | 34 | * of being able to transfer subscriptions to any device. |
42 | 35 | */ |
43 | | -public final class ImportExportJsonHelper { |
44 | | - /*////////////////////////////////////////////////////////////////////////// |
45 | | - // Json implementation |
46 | | - //////////////////////////////////////////////////////////////////////////*/ |
47 | | -
|
48 | | - private static final String JSON_APP_VERSION_KEY = "app_version"; |
49 | | - private static final String JSON_APP_VERSION_INT_KEY = "app_version_int"; |
50 | | -
|
51 | | - private static final String JSON_SUBSCRIPTIONS_ARRAY_KEY = "subscriptions"; |
52 | | -
|
53 | | - private static final String JSON_SERVICE_ID_KEY = "service_id"; |
54 | | - private static final String JSON_URL_KEY = "url"; |
55 | | - private static final String JSON_NAME_KEY = "name"; |
56 | | -
|
57 | | - private ImportExportJsonHelper() { } |
| 36 | +object ImportExportJsonHelper { |
| 37 | + private val json = Json { encodeDefaults = true } |
58 | 38 |
|
59 | 39 | /** |
60 | 40 | * Read a JSON source through the input stream. |
61 | 41 | * |
62 | 42 | * @param in the input stream (e.g. a file) |
63 | | - * @param eventListener listener for the events generated |
64 | 43 | * @return the parsed subscription items |
65 | 44 | */ |
66 | | - public static List<SubscriptionItem> readFrom( |
67 | | - final InputStream in, @Nullable final ImportExportEventListener eventListener) |
68 | | - throws InvalidSourceException { |
69 | | - if (in == null) { |
70 | | - throw new InvalidSourceException("input is null"); |
| 45 | + @JvmStatic |
| 46 | + @Throws(InvalidSourceException::class) |
| 47 | + fun readFrom(`in`: InputStream?): List<SubscriptionItem> { |
| 48 | + if (`in` == null) { |
| 49 | + throw InvalidSourceException("input is null") |
71 | 50 | } |
72 | 51 |
|
73 | | - final List<SubscriptionItem> channels = new ArrayList<>(); |
74 | | -
|
75 | 52 | try { |
76 | | - final JsonObject parentObject = JsonParser.object().from(in); |
77 | | -
|
78 | | - if (!parentObject.has(JSON_SUBSCRIPTIONS_ARRAY_KEY)) { |
79 | | - throw new InvalidSourceException("Channels array is null"); |
80 | | - } |
81 | | -
|
82 | | - final JsonArray channelsArray = parentObject.getArray(JSON_SUBSCRIPTIONS_ARRAY_KEY); |
83 | | -
|
84 | | - if (eventListener != null) { |
85 | | - eventListener.onSizeReceived(channelsArray.size()); |
86 | | - } |
87 | | -
|
88 | | - for (final Object o : channelsArray) { |
89 | | - if (o instanceof JsonObject) { |
90 | | - final JsonObject itemObject = (JsonObject) o; |
91 | | - final int serviceId = itemObject.getInt(JSON_SERVICE_ID_KEY, 0); |
92 | | - final String url = itemObject.getString(JSON_URL_KEY); |
93 | | - final String name = itemObject.getString(JSON_NAME_KEY); |
94 | | -
|
95 | | - if (url != null && name != null && !url.isEmpty() && !name.isEmpty()) { |
96 | | - channels.add(new SubscriptionItem(serviceId, url, name)); |
97 | | - if (eventListener != null) { |
98 | | - eventListener.onItemCompleted(name); |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | - } catch (final Throwable e) { |
104 | | - throw new InvalidSourceException("Couldn't parse json", e); |
| 53 | + @OptIn(ExperimentalSerializationApi::class) |
| 54 | + return json.decodeFromStream<SubscriptionData>(`in`).subscriptions |
| 55 | + } catch (e: Throwable) { |
| 56 | + throw InvalidSourceException("Couldn't parse json", e) |
105 | 57 | } |
106 | | -
|
107 | | - return channels; |
108 | 58 | } |
109 | 59 |
|
110 | 60 | /** |
111 | 61 | * Write the subscriptions items list as JSON to the output. |
112 | 62 | * |
113 | 63 | * @param items the list of subscriptions items |
114 | 64 | * @param out the output stream (e.g. a file) |
115 | | - * @param eventListener listener for the events generated |
116 | 65 | */ |
117 | | - public static void writeTo(final List<SubscriptionItem> items, final OutputStream out, |
118 | | - @Nullable final ImportExportEventListener eventListener) { |
119 | | - final JsonAppendableWriter writer = JsonWriter.on(out); |
120 | | - writeTo(items, writer, eventListener); |
121 | | - writer.done(); |
122 | | - } |
123 | | -
|
124 | | - /** |
125 | | - * @see #writeTo(List, OutputStream, ImportExportEventListener) |
126 | | - * @param items the list of subscriptions items |
127 | | - * @param writer the output {@link JsonAppendableWriter} |
128 | | - * @param eventListener listener for the events generated |
129 | | - */ |
130 | | - public static void writeTo(final List<SubscriptionItem> items, |
131 | | - final JsonAppendableWriter writer, |
132 | | - @Nullable final ImportExportEventListener eventListener) { |
133 | | - if (eventListener != null) { |
134 | | - eventListener.onSizeReceived(items.size()); |
135 | | - } |
136 | | -
|
137 | | - writer.object(); |
138 | | -
|
139 | | - writer.value(JSON_APP_VERSION_KEY, BuildConfig.VERSION_NAME); |
140 | | - writer.value(JSON_APP_VERSION_INT_KEY, BuildConfig.VERSION_CODE); |
141 | | -
|
142 | | - writer.array(JSON_SUBSCRIPTIONS_ARRAY_KEY); |
143 | | - for (final SubscriptionItem item : items) { |
144 | | - writer.object(); |
145 | | - writer.value(JSON_SERVICE_ID_KEY, item.getServiceId()); |
146 | | - writer.value(JSON_URL_KEY, item.getUrl()); |
147 | | - writer.value(JSON_NAME_KEY, item.getName()); |
148 | | - writer.end(); |
149 | | -
|
150 | | - if (eventListener != null) { |
151 | | - eventListener.onItemCompleted(item.getName()); |
152 | | - } |
153 | | - } |
154 | | - writer.end(); |
155 | | -
|
156 | | - writer.end(); |
| 66 | + @OptIn(ExperimentalSerializationApi::class) |
| 67 | + @JvmStatic |
| 68 | + fun writeTo( |
| 69 | + items: List<SubscriptionItem>, |
| 70 | + out: OutputStream, |
| 71 | + ) { |
| 72 | + json.encodeToStream(SubscriptionData(items), out) |
157 | 73 | } |
158 | 74 | } |
0 commit comments