JSONクラスのバグ修正。リアクションの並び順をAPI応答とそろえる

This commit is contained in:
tateisu 2020-01-07 17:31:05 +09:00
parent 154af2118c
commit 4355edd062
3 changed files with 63 additions and 113 deletions

View File

@ -2491,28 +2491,27 @@ internal class ItemViewHolder(
lastButton = b
}
// 既定のリアクション
for(mr in MisskeyReaction.values()) {
val count = reactionsCount[mr.shortcode]
if(count == null || count <= 0) continue
addEmojiReaction(mr.shortcode, mr.emojiUtf16, count)
}
// カスタム絵文字またはUnicode絵文字のリアクション
val list = reactionsCount.keys
.filter { MisskeyReaction.shortcodeMap[it] == null }
.sorted()
for(key in list) {
val count = reactionsCount[key]
if(count == null || count <= 0) continue
for(entry in reactionsCount.entries) {
val key = entry.key
val count = entry.value
if(count <= 0) continue
// 組み込みのリアクション
val mr = MisskeyReaction.shortcodeMap[key]
if(mr != null) {
addEmojiReaction(mr.shortcode, mr.emojiUtf16, count)
continue
}
// カスタム絵文字のリアクション
val customCode = key.replace(":", "")
if(key != customCode) {
addCustomEmojiReaction(key, customCode, count)
} else {
addEmojiReaction(key, key, count)
continue
}
// Unicode絵文字のリアクション
addEmojiReaction(key, key, count)
}
lastButton

View File

@ -20,6 +20,7 @@ import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern
import kotlin.collections.ArrayList
import kotlin.collections.LinkedHashMap
import kotlin.math.abs
class FilterTrees(
@ -150,7 +151,7 @@ class TootStatus(parser : TootParser, src : JsonObject) : TimelineItem() {
var viaMobile : Boolean = false
var reactionCounts : HashMap<String, Int>? = null
var reactionCounts : LinkedHashMap<String, Int>? = null
var myReaction : String? = null
var reply : TootStatus?
@ -740,7 +741,7 @@ class TootStatus(parser : TootParser, src : JsonObject) : TimelineItem() {
// カウントを増やす
var map = this.reactionCounts
if(map == null) {
map = HashMap()
map = LinkedHashMap()
this.reactionCounts = map
}
map[reaction] = (map[reaction] ?: 0) + 1
@ -769,7 +770,7 @@ class TootStatus(parser : TootParser, src : JsonObject) : TimelineItem() {
// カウントを減らす
var map = this.reactionCounts
if(map == null) {
map = HashMap()
map = LinkedHashMap()
this.reactionCounts = map
}
map[reaction] = (map[reaction] ?: 1) - 1
@ -1063,16 +1064,15 @@ class TootStatus(parser : TootParser, src : JsonObject) : TimelineItem() {
return rv
}
private fun parseReactionCounts(src : JsonObject?) : HashMap<String, Int>? {
var rv : HashMap<String, Int>? = null
if(src != null) {
for(key in src.keys) {
if(key.isEmpty()) continue
val v = src.parseInt(key) ?: continue
// カスタム絵文字などが含まれるようになったので、内容のバリデーションはできない
if(rv == null) rv = HashMap()
rv[key] = v
}
private fun parseReactionCounts(src : JsonObject?) : LinkedHashMap<String, Int>? {
// カスタム絵文字などが含まれるようになったので、内容のバリデーションはできない
var rv : LinkedHashMap<String, Int>? = null
src?.entries?.forEach { entry ->
val key = entry.key.notEmpty() ?: return@forEach
val v = src.parseInt(key)?.notZero() ?: return@forEach
if(rv == null) rv = LinkedHashMap()
rv!![key] = v
}
return rv
}

View File

@ -919,60 +919,9 @@ fun Writer.writeArray(indentFactor : Int, indent : Int, src : Any) : Writer =
throw JsonException(e)
}
fun Writer.writeJsonObject(indentFactor : Int, indent : Int, src : JsonObject) : Writer =
try {
append('{')
when(src.size) {
0 -> {
}
1 -> {
val entry = src.entries.first()
writeQuote(entry.key)
append(':')
if(indentFactor > 0) append(' ')
try {
writeJsonValue(indentFactor, indent, entry.value)
} catch(ex : Throwable) {
throw JsonException(
"Unable to write JsonObject value for key: ${entry.key}",
ex
)
}
}
else -> {
val newIndent = indent + indentFactor
var needsComma = false
for(entry in src.entries) {
if(needsComma) append(',')
indent(indentFactor, newIndent)
writeQuote(entry.key)
append(':')
if(indentFactor > 0) append(' ')
try {
writeJsonValue(indentFactor, newIndent, entry.value)
} catch(ex : Exception) {
throw JsonException(
"Unable to write JsonObject value for key: ${entry.key}",
ex
)
}
needsComma = true
}
indent(indentFactor, indent)
}
}
append('}')
this
} catch(ex : IOException) {
throw JsonException(ex)
}
private fun Writer.writeMap(indentFactor : Int, indent : Int, src : Map<*, *>) : Writer =
try {
append('[')
append('{')
when(src.size) {
0 -> {
}
@ -1014,7 +963,7 @@ private fun Writer.writeMap(indentFactor : Int, indent : Int, src : Map<*, *>) :
indent(indentFactor, indent)
}
}
append(']')
append('}')
this
} catch(e : IOException) {
throw JsonException(e)
@ -1025,34 +974,30 @@ private fun Writer.writeJsonValue(
indent : Int,
value : Any?
) : Writer {
if(value == null) {
write("null")
} else if(value is String) {
writeQuote(value)
} else if(value is Number) {
val sv = value.toJsonString()
if(reNumber.matcher(sv).matches()) {
write(sv)
} else {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
writeQuote(sv)
when {
value == null -> write("null")
value is Boolean -> write(value.toString())
value is Number -> {
val sv = value.toJsonString()
if(reNumber.matcher(sv).matches()) {
write(sv)
} else {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
writeQuote(sv)
}
}
} else if(value is Boolean) {
writeQuote(value.toString())
} else if(value is Enum<*>) {
writeQuote(value.name)
} else if(value is JsonObject) {
writeJsonObject(indentFactor, indent, value)
} else if(value is Map<*, *>) {
writeMap(indentFactor, indent, value)
} else if(value is Collection<*>) {
writeCollection(indentFactor, indent, value)
} else if(value.javaClass.isArray) {
writeArray(indentFactor, indent, value)
} else {
writeQuote(value.toString())
value is String -> writeQuote(value)
value is Enum<*> -> writeQuote(value.name)
value is JsonObject -> writeMap(indentFactor, indent, value)
value is Map<*, *> -> writeMap(indentFactor, indent, value)
value is Collection<*> -> writeCollection(indentFactor, indent, value)
value.javaClass.isArray -> writeArray(indentFactor, indent, value)
else -> writeQuote(value.toString())
}
return this
}
@ -1064,10 +1009,16 @@ fun notEmptyOrThrow(name : String, value : String?) =
fun List<Any?>.toJsonArray() = JsonArray(this)
// return null if the json value is "null"
fun String.parseJson() = JsonTokenizer(this).nextValue()
private val log = LogCategory("Json")
// return null if the json value is "null"
fun String.parseJson() = try{
JsonTokenizer(this).nextValue()
}catch(ex:Throwable){
log.e(ex,"parseJson failed. $this")
throw ex
}
//private val log = LogCategory("Json")
//fun String.parseJsonOrNull() =try {
// parseJson()
//}catch(ex:Throwable){