mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-01 07:13:56 +01:00
Convert WikiSiteTypeAdapter to kotlin
This commit is contained in:
parent
85dda964bc
commit
4ac81e1b64
2 changed files with 61 additions and 63 deletions
|
|
@ -1,63 +0,0 @@
|
||||||
package fr.free.nrw.commons.wikidata.json;
|
|
||||||
|
|
||||||
import android.net.Uri;
|
|
||||||
|
|
||||||
import com.google.gson.JsonParseException;
|
|
||||||
import com.google.gson.TypeAdapter;
|
|
||||||
import com.google.gson.stream.JsonReader;
|
|
||||||
import com.google.gson.stream.JsonToken;
|
|
||||||
import com.google.gson.stream.JsonWriter;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.wikidata.model.WikiSite;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class WikiSiteTypeAdapter extends TypeAdapter<WikiSite> {
|
|
||||||
private static final String DOMAIN = "domain";
|
|
||||||
private static final String LANGUAGE_CODE = "languageCode";
|
|
||||||
|
|
||||||
@Override public void write(JsonWriter out, WikiSite value) throws IOException {
|
|
||||||
out.beginObject();
|
|
||||||
out.name(DOMAIN);
|
|
||||||
out.value(value.url());
|
|
||||||
|
|
||||||
out.name(LANGUAGE_CODE);
|
|
||||||
out.value(value.languageCode());
|
|
||||||
out.endObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public WikiSite read(JsonReader in) throws IOException {
|
|
||||||
// todo: legacy; remove in June 2018
|
|
||||||
if (in.peek() == JsonToken.STRING) {
|
|
||||||
return new WikiSite(Uri.parse(in.nextString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
String domain = null;
|
|
||||||
String languageCode = null;
|
|
||||||
in.beginObject();
|
|
||||||
while (in.hasNext()) {
|
|
||||||
String field = in.nextName();
|
|
||||||
String val = in.nextString();
|
|
||||||
switch (field) {
|
|
||||||
case DOMAIN:
|
|
||||||
domain = val;
|
|
||||||
break;
|
|
||||||
case LANGUAGE_CODE:
|
|
||||||
languageCode = val;
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
in.endObject();
|
|
||||||
|
|
||||||
if (domain == null) {
|
|
||||||
throw new JsonParseException("Missing domain");
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: legacy; remove in June 2018
|
|
||||||
if (languageCode == null) {
|
|
||||||
return new WikiSite(domain);
|
|
||||||
}
|
|
||||||
return new WikiSite(domain, languageCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
package fr.free.nrw.commons.wikidata.json
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import com.google.gson.JsonParseException
|
||||||
|
import com.google.gson.TypeAdapter
|
||||||
|
import com.google.gson.stream.JsonReader
|
||||||
|
import com.google.gson.stream.JsonToken
|
||||||
|
import com.google.gson.stream.JsonWriter
|
||||||
|
import fr.free.nrw.commons.wikidata.model.WikiSite
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class WikiSiteTypeAdapter : TypeAdapter<WikiSite>() {
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun write(out: JsonWriter, value: WikiSite) {
|
||||||
|
out.beginObject()
|
||||||
|
out.name(DOMAIN)
|
||||||
|
out.value(value.url())
|
||||||
|
|
||||||
|
out.name(LANGUAGE_CODE)
|
||||||
|
out.value(value.languageCode())
|
||||||
|
out.endObject()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun read(reader: JsonReader): WikiSite {
|
||||||
|
// todo: legacy; remove reader June 2018
|
||||||
|
if (reader.peek() == JsonToken.STRING) {
|
||||||
|
return WikiSite(Uri.parse(reader.nextString()))
|
||||||
|
}
|
||||||
|
|
||||||
|
var domain: String? = null
|
||||||
|
var languageCode: String? = null
|
||||||
|
reader.beginObject()
|
||||||
|
while (reader.hasNext()) {
|
||||||
|
val field = reader.nextName()
|
||||||
|
val value = reader.nextString()
|
||||||
|
when (field) {
|
||||||
|
DOMAIN -> domain = value
|
||||||
|
LANGUAGE_CODE -> languageCode = value
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reader.endObject()
|
||||||
|
|
||||||
|
if (domain == null) {
|
||||||
|
throw JsonParseException("Missing domain")
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo: legacy; remove reader June 2018
|
||||||
|
return if (languageCode == null) {
|
||||||
|
WikiSite(domain)
|
||||||
|
} else {
|
||||||
|
WikiSite(domain, languageCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val DOMAIN = "domain"
|
||||||
|
private const val LANGUAGE_CODE = "languageCode"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue