Concatenate jsonfeed authors when they are several

This commit is contained in:
Shinokuni 2020-09-23 22:29:47 +02:00
parent c742c4fbf2
commit 22fe77d5cf
3 changed files with 21 additions and 8 deletions

View File

@ -16,11 +16,23 @@
},
{
"url": "url 2",
"name": "Author 2"
"name": ""
},
{
"url": "url 3",
"name": "Author 3"
},
{
"url": "url 4",
"name": "Author 4"
},
{
"url": "url 5",
"name": "Author 5"
},
{
"url": "url 6",
"name": "Author 6"
}
]
}

View File

@ -51,7 +51,7 @@ class JSONItemsAdapterTest {
assertEquals(item.description, "This is a summary")
assertEquals(item.content, "content_html")
assertEquals(item.imageLink, "https://image.com")
assertEquals(item.author, "Author 1")
assertEquals(item.author, "Author 1, Author 3, Author 4, Author 5, ...")
}
@Test

View File

@ -57,6 +57,7 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
7 -> pubDate = DateUtils.stringToLocalDateTime(reader.nextString())
8 -> author = parseAuthor(reader) // jsonfeed 1.0
9 -> author = parseAuthors(reader) // jsonfeed 1.1
else -> reader.skipValue()
}
}
}
@ -86,9 +87,6 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
return author
}
/**
* Returns the first author of the array
*/
private fun parseAuthors(reader: JsonReader): String? {
val authors = arrayListOf<String?>()
reader.beginArray()
@ -98,7 +96,10 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
}
reader.endArray()
return if (authors.filterNotNull().isNotEmpty()) authors.filterNotNull().first() else null
// here, nextNullableString doesn't check if authors values are empty
return if (authors.filterNot { author -> author.isNullOrEmpty() }.isNotEmpty())
authors.filterNot { author -> author.isNullOrEmpty() }.joinToString(limit = 4) else null
}
private fun validateItem(item: Item) {
@ -110,7 +111,7 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
}
companion object {
val names: JsonReader.Options = JsonReader.Options.of("id", "url", "title", "content_html", "content_text",
"summary", "image", "date_published", "author", "authors")
val names: JsonReader.Options = JsonReader.Options.of("id", "url", "title",
"content_html", "content_text", "summary", "image", "date_published", "author", "authors")
}
}