Treat empty strings as null values in jsonfeed adapters
This commit is contained in:
parent
d958dcbf04
commit
2bfb061f48
@ -1,6 +1,7 @@
|
|||||||
package com.readrops.api.localfeed.json
|
package com.readrops.api.localfeed.json
|
||||||
|
|
||||||
import com.readrops.api.utils.ParseException
|
import com.readrops.api.utils.ParseException
|
||||||
|
import com.readrops.api.utils.nextNonEmptyString
|
||||||
import com.readrops.api.utils.nextNullableString
|
import com.readrops.api.utils.nextNullableString
|
||||||
import com.readrops.db.entities.Feed
|
import com.readrops.db.entities.Feed
|
||||||
import com.squareup.moshi.FromJson
|
import com.squareup.moshi.FromJson
|
||||||
@ -21,7 +22,7 @@ class JSONFeedAdapter {
|
|||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
with(feed) {
|
with(feed) {
|
||||||
when (reader.selectName(names)) {
|
when (reader.selectName(names)) {
|
||||||
0 -> name = reader.nextString()
|
0 -> name = reader.nextNonEmptyString()
|
||||||
1 -> siteUrl = reader.nextNullableString()
|
1 -> siteUrl = reader.nextNullableString()
|
||||||
2 -> url = reader.nextNullableString()
|
2 -> url = reader.nextNullableString()
|
||||||
3 -> description = reader.nextNullableString()
|
3 -> description = reader.nextNullableString()
|
||||||
|
@ -3,6 +3,7 @@ package com.readrops.api.localfeed.json
|
|||||||
import com.readrops.api.localfeed.XmlAdapter.Companion.AUTHORS_MAX
|
import com.readrops.api.localfeed.XmlAdapter.Companion.AUTHORS_MAX
|
||||||
import com.readrops.api.utils.DateUtils
|
import com.readrops.api.utils.DateUtils
|
||||||
import com.readrops.api.utils.ParseException
|
import com.readrops.api.utils.ParseException
|
||||||
|
import com.readrops.api.utils.nextNonEmptyString
|
||||||
import com.readrops.api.utils.nextNullableString
|
import com.readrops.api.utils.nextNullableString
|
||||||
import com.readrops.db.entities.Item
|
import com.readrops.db.entities.Item
|
||||||
import com.squareup.moshi.FromJson
|
import com.squareup.moshi.FromJson
|
||||||
@ -16,9 +17,8 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
|
|||||||
// not useful
|
// not useful
|
||||||
}
|
}
|
||||||
|
|
||||||
@FromJson
|
|
||||||
override fun fromJson(reader: JsonReader): List<Item> {
|
override fun fromJson(reader: JsonReader): List<Item> {
|
||||||
try {
|
return try {
|
||||||
val items = arrayListOf<Item>()
|
val items = arrayListOf<Item>()
|
||||||
reader.beginObject()
|
reader.beginObject()
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return items
|
items
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw ParseException(e.message)
|
throw ParseException(e.message)
|
||||||
}
|
}
|
||||||
@ -48,14 +48,14 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
|
|||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
with(item) {
|
with(item) {
|
||||||
when (reader.selectName(names)) {
|
when (reader.selectName(names)) {
|
||||||
0 -> guid = reader.nextString()
|
0 -> guid = reader.nextNonEmptyString()
|
||||||
1 -> link = reader.nextString()
|
1 -> link = reader.nextNonEmptyString()
|
||||||
2 -> title = reader.nextString()
|
2 -> title = reader.nextNonEmptyString()
|
||||||
3 -> contentHtml = reader.nextNullableString()
|
3 -> contentHtml = reader.nextNullableString()
|
||||||
4 -> contentText = reader.nextNullableString()
|
4 -> contentText = reader.nextNullableString()
|
||||||
5 -> description = reader.nextNullableString()
|
5 -> description = reader.nextNullableString()
|
||||||
6 -> imageLink = reader.nextNullableString()
|
6 -> imageLink = reader.nextNullableString()
|
||||||
7 -> pubDate = DateUtils.stringToLocalDateTime(reader.nextString())
|
7 -> pubDate = DateUtils.stringToLocalDateTime(reader.nextNonEmptyString())
|
||||||
8 -> author = parseAuthor(reader) // jsonfeed 1.0
|
8 -> author = parseAuthor(reader) // jsonfeed 1.0
|
||||||
9 -> author = parseAuthors(reader) // jsonfeed 1.1
|
9 -> author = parseAuthors(reader) // jsonfeed 1.1
|
||||||
else -> reader.skipValue()
|
else -> reader.skipValue()
|
||||||
@ -98,9 +98,8 @@ class JSONItemsAdapter : JsonAdapter<List<Item>>() {
|
|||||||
|
|
||||||
reader.endArray()
|
reader.endArray()
|
||||||
|
|
||||||
// here, nextNullableString doesn't check if authors values are empty
|
return if (authors.filterNotNull().isNotEmpty())
|
||||||
return if (authors.filterNot { author -> author.isNullOrEmpty() }.isNotEmpty())
|
authors.filterNotNull().joinToString(limit = AUTHORS_MAX) else null
|
||||||
authors.filterNot { author -> author.isNullOrEmpty() }.joinToString(limit = AUTHORS_MAX) else null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun validateItem(item: Item) {
|
private fun validateItem(item: Item) {
|
||||||
|
@ -3,4 +3,9 @@ package com.readrops.api.utils
|
|||||||
import com.squareup.moshi.JsonReader
|
import com.squareup.moshi.JsonReader
|
||||||
|
|
||||||
fun JsonReader.nextNullableString(): String? =
|
fun JsonReader.nextNullableString(): String? =
|
||||||
if (peek() != JsonReader.Token.NULL) nextString() else nextNull()
|
if (peek() != JsonReader.Token.NULL) nextString().ifEmpty { null }?.trim() else nextNull()
|
||||||
|
|
||||||
|
fun JsonReader.nextNonEmptyString(): String {
|
||||||
|
val text = nextString()
|
||||||
|
return if (text.isNotEmpty()) text.trim() else throw ParseException("Json value can't be null")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user