mirror of
https://github.com/fankes/moshi.git
synced 2025-10-21 08:59:20 +08:00
Update dependency com.pinterest.ktlint:ktlint-cli to v1.2.0 (#1814)
* Update dependency com.pinterest.ktlint:ktlint-cli to v1.2.0 * Appease ktlint --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jake Wharton <jw@squareup.com>
This commit is contained in:
@@ -61,7 +61,9 @@ internal class AdapterMethodsFactory(
|
||||
override fun toJson(writer: JsonWriter, value: Any?) {
|
||||
when {
|
||||
toAdapter == null -> knownNotNull(delegate).toJson(writer, value)
|
||||
|
||||
!toAdapter.nullable && value == null -> writer.nullValue()
|
||||
|
||||
else -> {
|
||||
try {
|
||||
toAdapter.toJson(moshi, writer, value)
|
||||
@@ -77,7 +79,9 @@ internal class AdapterMethodsFactory(
|
||||
override fun fromJson(reader: JsonReader): Any? {
|
||||
return when {
|
||||
fromAdapter == null -> knownNotNull(delegate).fromJson(reader)
|
||||
|
||||
!fromAdapter.nullable && reader.peek() == JsonReader.Token.NULL -> reader.nextNull<Any>()
|
||||
|
||||
else -> {
|
||||
try {
|
||||
fromAdapter.fromJson(moshi, reader)
|
||||
@@ -161,6 +165,7 @@ internal class AdapterMethodsFactory(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parameterTypes.size == 1 && returnType != Void.TYPE -> {
|
||||
// List<Integer> pointToJson(Point point) {
|
||||
val returnTypeAnnotations = method.jsonAnnotations
|
||||
@@ -194,6 +199,7 @@ internal class AdapterMethodsFactory(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw IllegalArgumentException(
|
||||
"""
|
||||
@@ -249,6 +255,7 @@ internal class AdapterMethodsFactory(
|
||||
override fun fromJson(moshi: Moshi, reader: JsonReader) = invokeMethod(reader)
|
||||
}
|
||||
}
|
||||
|
||||
parameterTypes.size == 1 && returnType != Void.TYPE -> {
|
||||
// Point pointFromJson(List<Integer> o) {
|
||||
val qualifierAnnotations = parameterAnnotations[0].jsonAnnotations
|
||||
@@ -279,6 +286,7 @@ internal class AdapterMethodsFactory(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw IllegalArgumentException(
|
||||
"""
|
||||
|
@@ -49,41 +49,49 @@ internal class ArrayJsonAdapter(
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is ByteArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is CharArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is DoubleArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is FloatArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is IntArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is LongArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is ShortArray -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
}
|
||||
}
|
||||
|
||||
is Array<*> -> {
|
||||
for (element in value) {
|
||||
elementAdapter.toJson(writer, element)
|
||||
|
@@ -53,9 +53,11 @@ internal abstract class CollectionJsonAdapter<C : MutableCollection<T?>, T> priv
|
||||
List::class.java, Collection::class.java -> {
|
||||
newArrayListAdapter<Any>(type, moshi).nullSafe()
|
||||
}
|
||||
|
||||
Set::class.java -> {
|
||||
newLinkedHashSetAdapter<Any>(type, moshi).nullSafe()
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
@@ -483,6 +483,7 @@ public sealed class JsonReader : Closeable {
|
||||
endArray()
|
||||
}
|
||||
}
|
||||
|
||||
Token.BEGIN_OBJECT -> {
|
||||
return buildMap {
|
||||
beginObject()
|
||||
@@ -497,10 +498,15 @@ public sealed class JsonReader : Closeable {
|
||||
endObject()
|
||||
}
|
||||
}
|
||||
|
||||
Token.STRING -> nextString()
|
||||
|
||||
Token.NUMBER -> nextDouble()
|
||||
|
||||
Token.BOOLEAN -> nextBoolean()
|
||||
|
||||
Token.NULL -> nextNull<Any>()
|
||||
|
||||
else -> throw IllegalStateException("Expected a value but was ${peek()} at path $path")
|
||||
}
|
||||
}
|
||||
|
@@ -59,12 +59,14 @@ internal object JsonScope {
|
||||
for (i in 0 until stackSize) {
|
||||
when (stack[i]) {
|
||||
EMPTY_ARRAY, NONEMPTY_ARRAY -> append('[').append(pathIndices[i]).append(']')
|
||||
|
||||
EMPTY_OBJECT, DANGLING_NAME, NONEMPTY_OBJECT -> {
|
||||
append('.')
|
||||
if (pathNames[i] != null) {
|
||||
append(pathNames[i])
|
||||
}
|
||||
}
|
||||
|
||||
NONEMPTY_DOCUMENT, EMPTY_DOCUMENT, CLOSED -> {}
|
||||
}
|
||||
}
|
||||
|
@@ -147,6 +147,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
val peekStack = scopes[stackSize - 1]
|
||||
when (peekStack) {
|
||||
JsonScope.EMPTY_ARRAY -> scopes[stackSize - 1] = JsonScope.NONEMPTY_ARRAY
|
||||
|
||||
JsonScope.NONEMPTY_ARRAY -> {
|
||||
// Look for a comma before the next element.
|
||||
val c = nextNonWhitespace(true).toChar()
|
||||
@@ -155,11 +156,16 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
']' -> {
|
||||
return setPeeked(PEEKED_END_ARRAY)
|
||||
}
|
||||
|
||||
';' -> checkLenient()
|
||||
',' -> Unit /*no op*/
|
||||
|
||||
/*no op*/
|
||||
',' -> Unit
|
||||
|
||||
else -> throw syntaxError("Unterminated array")
|
||||
}
|
||||
}
|
||||
|
||||
JsonScope.EMPTY_OBJECT, JsonScope.NONEMPTY_OBJECT -> {
|
||||
scopes[stackSize - 1] = JsonScope.DANGLING_NAME
|
||||
// Look for a comma before the next element.
|
||||
@@ -170,8 +176,12 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
'}' -> {
|
||||
return setPeeked(PEEKED_END_OBJECT)
|
||||
}
|
||||
',' -> Unit /*no op*/
|
||||
|
||||
/*no op*/
|
||||
',' -> Unit
|
||||
|
||||
';' -> checkLenient()
|
||||
|
||||
else -> throw syntaxError("Unterminated object")
|
||||
}
|
||||
}
|
||||
@@ -180,17 +190,20 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
buffer.readByte() // consume the '\"'.
|
||||
PEEKED_DOUBLE_QUOTED_NAME
|
||||
}
|
||||
|
||||
'\'' -> {
|
||||
buffer.readByte() // consume the '\''.
|
||||
checkLenient()
|
||||
PEEKED_SINGLE_QUOTED_NAME
|
||||
}
|
||||
|
||||
'}' -> if (peekStack != JsonScope.NONEMPTY_OBJECT) {
|
||||
buffer.readByte() // consume the '}'.
|
||||
PEEKED_END_OBJECT
|
||||
} else {
|
||||
throw syntaxError("Expected name")
|
||||
}
|
||||
|
||||
else -> {
|
||||
checkLenient()
|
||||
if (isLiteral(c.code)) {
|
||||
@@ -203,23 +216,29 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
peeked = next
|
||||
return next
|
||||
}
|
||||
|
||||
JsonScope.DANGLING_NAME -> {
|
||||
scopes[stackSize - 1] = JsonScope.NONEMPTY_OBJECT
|
||||
// Look for a colon before the value.
|
||||
val c = nextNonWhitespace(true).toChar()
|
||||
buffer.readByte() // Consume ':'.
|
||||
when (c) {
|
||||
':' -> Unit /*no op*/
|
||||
/*no op*/
|
||||
':' -> Unit
|
||||
|
||||
'=' -> {
|
||||
checkLenient()
|
||||
if (source.request(1) && buffer[0].asChar() == '>') {
|
||||
buffer.readByte() // Consume '>'.
|
||||
}
|
||||
}
|
||||
|
||||
else -> throw syntaxError("Expected ':'")
|
||||
}
|
||||
}
|
||||
|
||||
JsonScope.EMPTY_DOCUMENT -> scopes[stackSize - 1] = JsonScope.NONEMPTY_DOCUMENT
|
||||
|
||||
JsonScope.NONEMPTY_DOCUMENT -> {
|
||||
if (nextNonWhitespace(false) == -1) {
|
||||
return setPeeked(PEEKED_EOF)
|
||||
@@ -227,12 +246,14 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
checkLenient()
|
||||
}
|
||||
}
|
||||
|
||||
JsonScope.STREAMING_VALUE -> {
|
||||
valueSource!!.discard()
|
||||
valueSource = null
|
||||
stackSize--
|
||||
return doPeek()
|
||||
}
|
||||
|
||||
else -> check(peekStack != JsonScope.CLOSED) { "JsonReader is closed" }
|
||||
}
|
||||
// "fallthrough" from previous `when`
|
||||
@@ -243,39 +264,48 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
buffer.readByte() // Consume ']'.
|
||||
setPeeked(PEEKED_END_ARRAY)
|
||||
}
|
||||
|
||||
JsonScope.NONEMPTY_ARRAY -> {
|
||||
// In lenient mode, a 0-length literal in an array means 'null'.
|
||||
checkLenient()
|
||||
setPeeked(PEEKED_NULL)
|
||||
}
|
||||
|
||||
else -> throw syntaxError("Unexpected value")
|
||||
}
|
||||
}
|
||||
|
||||
// In lenient mode, a 0-length literal in an array means 'null'.
|
||||
';', ',' -> return when (peekStack) {
|
||||
JsonScope.EMPTY_ARRAY, JsonScope.NONEMPTY_ARRAY -> {
|
||||
checkLenient()
|
||||
setPeeked(PEEKED_NULL)
|
||||
}
|
||||
|
||||
else -> throw syntaxError("Unexpected value")
|
||||
}
|
||||
|
||||
'\'' -> {
|
||||
checkLenient()
|
||||
buffer.readByte() // Consume '\''.
|
||||
return setPeeked(PEEKED_SINGLE_QUOTED)
|
||||
}
|
||||
|
||||
'"' -> {
|
||||
buffer.readByte() // Consume '\"'.
|
||||
return setPeeked(PEEKED_DOUBLE_QUOTED)
|
||||
}
|
||||
|
||||
'[' -> {
|
||||
buffer.readByte() // Consume '['.
|
||||
return setPeeked(PEEKED_BEGIN_ARRAY)
|
||||
}
|
||||
|
||||
'{' -> {
|
||||
buffer.readByte() // Consume '{'.
|
||||
return setPeeked(PEEKED_BEGIN_OBJECT)
|
||||
}
|
||||
|
||||
else -> Unit /* no-op */
|
||||
}
|
||||
var result = peekKeyword()
|
||||
@@ -305,16 +335,19 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
keywordUpper = "TRUE"
|
||||
peeking = PEEKED_TRUE
|
||||
}
|
||||
|
||||
'f', 'F' -> {
|
||||
keyword = "false"
|
||||
keywordUpper = "FALSE"
|
||||
peeking = PEEKED_FALSE
|
||||
}
|
||||
|
||||
'n', 'N' -> {
|
||||
keyword = "null"
|
||||
keywordUpper = "NULL"
|
||||
peeking = PEEKED_NULL
|
||||
}
|
||||
|
||||
else -> return PEEKED_NONE
|
||||
}
|
||||
|
||||
@@ -358,6 +391,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
NUMBER_CHAR_EXP_E -> {
|
||||
last = NUMBER_CHAR_EXP_SIGN
|
||||
i++
|
||||
@@ -366,6 +400,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
return PEEKED_NONE
|
||||
}
|
||||
|
||||
'+' -> {
|
||||
if (last == NUMBER_CHAR_EXP_E) {
|
||||
last = NUMBER_CHAR_EXP_SIGN
|
||||
@@ -374,6 +409,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
return PEEKED_NONE
|
||||
}
|
||||
|
||||
'e', 'E' -> {
|
||||
if (last == NUMBER_CHAR_DIGIT || last == NUMBER_CHAR_FRACTION_DIGIT) {
|
||||
last = NUMBER_CHAR_EXP_E
|
||||
@@ -382,6 +418,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
return PEEKED_NONE
|
||||
}
|
||||
|
||||
'.' -> {
|
||||
if (last == NUMBER_CHAR_DIGIT) {
|
||||
last = NUMBER_CHAR_DECIMAL
|
||||
@@ -390,6 +427,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
return PEEKED_NONE
|
||||
}
|
||||
|
||||
else -> {
|
||||
if (c !in '0'..'9') {
|
||||
if (!isLiteral(c.code)) break
|
||||
@@ -400,6 +438,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
value = -(c - '0').toLong()
|
||||
last = NUMBER_CHAR_DIGIT
|
||||
}
|
||||
|
||||
NUMBER_CHAR_DIGIT -> {
|
||||
if (value == 0L) {
|
||||
return PEEKED_NONE // Leading '0' prefix is not allowed (since it could be octal).
|
||||
@@ -413,7 +452,9 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
)
|
||||
value = newValue
|
||||
}
|
||||
|
||||
NUMBER_CHAR_DECIMAL -> last = NUMBER_CHAR_FRACTION_DIGIT
|
||||
|
||||
NUMBER_CHAR_EXP_E, NUMBER_CHAR_EXP_SIGN -> last = NUMBER_CHAR_EXP_DIGIT
|
||||
}
|
||||
}
|
||||
@@ -431,12 +472,14 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
buffer.skip(i)
|
||||
setPeeked(PEEKED_LONG)
|
||||
}
|
||||
|
||||
last == NUMBER_CHAR_DIGIT ||
|
||||
last == NUMBER_CHAR_FRACTION_DIGIT ||
|
||||
last == NUMBER_CHAR_EXP_DIGIT -> {
|
||||
peekedNumberLength = i.toInt()
|
||||
setPeeked(PEEKED_NUMBER)
|
||||
}
|
||||
|
||||
else -> PEEKED_NONE
|
||||
}
|
||||
}
|
||||
@@ -448,8 +491,10 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
checkLenient() // fall-through
|
||||
false
|
||||
}
|
||||
|
||||
// 0x000C = \f
|
||||
'{', '}', '[', ']', ':', ',', ' ', '\t', '\u000C', '\r', '\n' -> false
|
||||
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
@@ -458,13 +503,17 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
override fun nextName(): String {
|
||||
val result = when (peekIfNone()) {
|
||||
PEEKED_UNQUOTED_NAME -> nextUnquotedValue()
|
||||
|
||||
PEEKED_DOUBLE_QUOTED_NAME -> nextQuotedValue(DOUBLE_QUOTE_OR_SLASH)
|
||||
|
||||
PEEKED_SINGLE_QUOTED_NAME -> nextQuotedValue(SINGLE_QUOTE_OR_SLASH)
|
||||
|
||||
PEEKED_BUFFERED_NAME -> {
|
||||
val name = peekedString!!
|
||||
peekedString = null
|
||||
name
|
||||
}
|
||||
|
||||
else -> throw JsonDataException("Expected a name but was ${peek()} at path $path")
|
||||
}
|
||||
peeked = PEEKED_NONE
|
||||
@@ -539,15 +588,21 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
override fun nextString(): String {
|
||||
val result = when (peekIfNone()) {
|
||||
PEEKED_UNQUOTED -> nextUnquotedValue()
|
||||
|
||||
PEEKED_DOUBLE_QUOTED -> nextQuotedValue(DOUBLE_QUOTE_OR_SLASH)
|
||||
|
||||
PEEKED_SINGLE_QUOTED -> nextQuotedValue(SINGLE_QUOTE_OR_SLASH)
|
||||
|
||||
PEEKED_BUFFERED -> {
|
||||
val buffered = peekedString!!
|
||||
peekedString = null
|
||||
buffered
|
||||
}
|
||||
|
||||
PEEKED_LONG -> peekedLong.toString()
|
||||
|
||||
PEEKED_NUMBER -> buffer.readUtf8(peekedNumberLength.toLong())
|
||||
|
||||
else -> throw JsonDataException("Expected a string but was ${peek()} at path $path")
|
||||
}
|
||||
peeked = PEEKED_NONE
|
||||
@@ -601,11 +656,13 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
pathIndices[stackSize - 1]++
|
||||
true
|
||||
}
|
||||
|
||||
PEEKED_FALSE -> {
|
||||
peeked = PEEKED_NONE
|
||||
pathIndices[stackSize - 1]++
|
||||
false
|
||||
}
|
||||
|
||||
else -> throw JsonDataException("Expected a boolean but was ${peek()} at path $path")
|
||||
}
|
||||
}
|
||||
@@ -630,13 +687,18 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
val next = when (p) {
|
||||
PEEKED_NUMBER -> buffer.readUtf8(peekedNumberLength.toLong()).also { peekedString = it }
|
||||
|
||||
PEEKED_DOUBLE_QUOTED -> nextQuotedValue(DOUBLE_QUOTE_OR_SLASH).also { peekedString = it }
|
||||
|
||||
PEEKED_SINGLE_QUOTED -> nextQuotedValue(SINGLE_QUOTE_OR_SLASH).also { peekedString = it }
|
||||
|
||||
PEEKED_UNQUOTED -> nextUnquotedValue().also { peekedString = it }
|
||||
|
||||
PEEKED_BUFFERED -> {
|
||||
// PEEKED_BUFFERED means the value's been stored in peekedString
|
||||
knownNotNull(peekedString)
|
||||
}
|
||||
|
||||
else -> throw JsonDataException("Expected a double but was " + peek() + " at path " + path)
|
||||
}
|
||||
peeked = PEEKED_BUFFERED
|
||||
@@ -663,6 +725,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
when {
|
||||
p == PEEKED_NUMBER -> peekedString = buffer.readUtf8(peekedNumberLength.toLong())
|
||||
|
||||
p == PEEKED_DOUBLE_QUOTED || p == PEEKED_SINGLE_QUOTED -> {
|
||||
peekedString = if (p == PEEKED_DOUBLE_QUOTED) nextQuotedValue(DOUBLE_QUOTE_OR_SLASH) else nextQuotedValue(SINGLE_QUOTE_OR_SLASH)
|
||||
try {
|
||||
@@ -674,6 +737,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
// Fall back to parse as a BigDecimal below.
|
||||
}
|
||||
}
|
||||
|
||||
p != PEEKED_BUFFERED -> {
|
||||
throw JsonDataException("Expected a long but was " + peek() + " at path " + path)
|
||||
}
|
||||
@@ -768,6 +832,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
PEEKED_NUMBER -> {
|
||||
buffer.readUtf8(peekedNumberLength.toLong()).also { peekedString = it }
|
||||
}
|
||||
|
||||
PEEKED_DOUBLE_QUOTED, PEEKED_SINGLE_QUOTED -> {
|
||||
val next = if (p == PEEKED_DOUBLE_QUOTED) {
|
||||
nextQuotedValue(DOUBLE_QUOTE_OR_SLASH)
|
||||
@@ -785,10 +850,12 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
next
|
||||
}
|
||||
}
|
||||
|
||||
PEEKED_BUFFERED -> {
|
||||
// PEEKED_BUFFERED means the value's been stored in peekedString
|
||||
knownNotNull(peekedString)
|
||||
}
|
||||
|
||||
else -> throw JsonDataException("Expected an int but was ${peek()} at path $path")
|
||||
}
|
||||
peeked = PEEKED_BUFFERED
|
||||
@@ -826,10 +893,12 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
pushScope(JsonScope.EMPTY_ARRAY)
|
||||
count++
|
||||
}
|
||||
|
||||
PEEKED_BEGIN_OBJECT -> {
|
||||
pushScope(JsonScope.EMPTY_OBJECT)
|
||||
count++
|
||||
}
|
||||
|
||||
PEEKED_END_ARRAY -> {
|
||||
count--
|
||||
if (count < 0) {
|
||||
@@ -837,6 +906,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
stackSize--
|
||||
}
|
||||
|
||||
PEEKED_END_OBJECT -> {
|
||||
count--
|
||||
if (count < 0) {
|
||||
@@ -844,10 +914,15 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
}
|
||||
stackSize--
|
||||
}
|
||||
|
||||
PEEKED_UNQUOTED_NAME, PEEKED_UNQUOTED -> skipUnquotedValue()
|
||||
|
||||
PEEKED_DOUBLE_QUOTED, PEEKED_DOUBLE_QUOTED_NAME -> skipQuotedValue(DOUBLE_QUOTE_OR_SLASH)
|
||||
|
||||
PEEKED_SINGLE_QUOTED, PEEKED_SINGLE_QUOTED_NAME -> skipQuotedValue(SINGLE_QUOTE_OR_SLASH)
|
||||
|
||||
PEEKED_NUMBER -> buffer.skip(peekedNumberLength.toLong())
|
||||
|
||||
PEEKED_EOF -> throw JsonDataException("Expected a value but was ${peek()} at path $path")
|
||||
}
|
||||
peeked = PEEKED_NONE
|
||||
@@ -867,29 +942,38 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
state = JsonValueSource.STATE_JSON
|
||||
valueSourceStackSize++
|
||||
}
|
||||
|
||||
PEEKED_BEGIN_OBJECT -> {
|
||||
prefix.writeUtf8("{")
|
||||
state = JsonValueSource.STATE_JSON
|
||||
valueSourceStackSize++
|
||||
}
|
||||
|
||||
PEEKED_DOUBLE_QUOTED -> {
|
||||
prefix.writeUtf8("\"")
|
||||
state = JsonValueSource.STATE_DOUBLE_QUOTED
|
||||
}
|
||||
|
||||
PEEKED_SINGLE_QUOTED -> {
|
||||
prefix.writeUtf8("'")
|
||||
state = JsonValueSource.STATE_SINGLE_QUOTED
|
||||
}
|
||||
|
||||
PEEKED_NUMBER, PEEKED_LONG, PEEKED_UNQUOTED -> prefix.writeUtf8(nextString())
|
||||
|
||||
PEEKED_TRUE -> prefix.writeUtf8("true")
|
||||
|
||||
PEEKED_FALSE -> prefix.writeUtf8("false")
|
||||
|
||||
PEEKED_NULL -> prefix.writeUtf8("null")
|
||||
|
||||
PEEKED_BUFFERED -> {
|
||||
val string = nextString()
|
||||
JsonWriter.of(prefix).use { jsonWriter ->
|
||||
jsonWriter.value(string)
|
||||
}
|
||||
}
|
||||
|
||||
else -> throw JsonDataException("Expected a value but was ${peek()} at path $path")
|
||||
}
|
||||
|
||||
@@ -942,6 +1026,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
p = 0
|
||||
continue
|
||||
}
|
||||
|
||||
'/' -> {
|
||||
// skip a // end-of-line comment
|
||||
buffer.readByte() // '/'
|
||||
@@ -950,9 +1035,11 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
p = 0
|
||||
continue
|
||||
}
|
||||
|
||||
else -> c.code
|
||||
}
|
||||
}
|
||||
|
||||
'#' -> {
|
||||
// Skip a # hash end-of-line comment. The JSON RFC doesn't specify this behaviour, but it's
|
||||
// required to parse existing documents.
|
||||
@@ -960,6 +1047,7 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
skipToEndOfLine()
|
||||
p = 0
|
||||
}
|
||||
|
||||
else -> return c.code
|
||||
}
|
||||
}
|
||||
@@ -1026,12 +1114,20 @@ internal class JsonUtf8Reader : JsonReader {
|
||||
buffer.skip(4)
|
||||
result
|
||||
}
|
||||
|
||||
't' -> '\t'
|
||||
|
||||
'b' -> '\b'
|
||||
|
||||
'n' -> '\n'
|
||||
|
||||
'r' -> '\r'
|
||||
'f' -> '\u000C' /*\f*/
|
||||
|
||||
/*\f*/
|
||||
'f' -> '\u000C'
|
||||
|
||||
'\n', '\'', '"', '\\', '/' -> escaped
|
||||
|
||||
else -> {
|
||||
if (!lenient) throw syntaxError("Invalid escape sequence: \\$escaped")
|
||||
escaped
|
||||
|
@@ -302,21 +302,27 @@ internal class JsonUtf8Writer(
|
||||
}
|
||||
nextTop = JsonScope.NONEMPTY_DOCUMENT
|
||||
}
|
||||
|
||||
JsonScope.EMPTY_DOCUMENT -> nextTop = JsonScope.NONEMPTY_DOCUMENT
|
||||
|
||||
JsonScope.NONEMPTY_ARRAY -> {
|
||||
sink.writeByte(','.code)
|
||||
newline()
|
||||
nextTop = JsonScope.NONEMPTY_ARRAY
|
||||
}
|
||||
|
||||
JsonScope.EMPTY_ARRAY -> {
|
||||
newline()
|
||||
nextTop = JsonScope.NONEMPTY_ARRAY
|
||||
}
|
||||
|
||||
JsonScope.DANGLING_NAME -> {
|
||||
nextTop = JsonScope.NONEMPTY_OBJECT
|
||||
sink.writeUtf8(separator)
|
||||
}
|
||||
|
||||
JsonScope.STREAMING_VALUE -> throw IllegalStateException("Sink from valueSink() was not closed")
|
||||
|
||||
else -> throw IllegalStateException("Nesting problem.")
|
||||
}
|
||||
replaceTop(nextTop)
|
||||
|
@@ -113,13 +113,21 @@ internal class JsonValueReader : JsonReader {
|
||||
// If the top of the stack is an iterator, take its first element and push it on the stack.
|
||||
return when (val peeked = stack[stackSize - 1]) {
|
||||
is JsonIterator -> peeked.endToken
|
||||
|
||||
is List<*> -> Token.BEGIN_ARRAY
|
||||
|
||||
is Map<*, *> -> Token.BEGIN_OBJECT
|
||||
|
||||
is Map.Entry<*, *> -> Token.NAME
|
||||
|
||||
is String -> Token.STRING
|
||||
|
||||
is Boolean -> Token.BOOLEAN
|
||||
|
||||
is Number -> Token.NUMBER
|
||||
|
||||
null -> Token.NULL
|
||||
|
||||
else -> ifNotClosed(peeked) {
|
||||
throw typeMismatch(peeked, "a JSON value")
|
||||
}
|
||||
@@ -170,10 +178,12 @@ internal class JsonValueReader : JsonReader {
|
||||
remove()
|
||||
peeked
|
||||
}
|
||||
|
||||
is Number -> {
|
||||
remove()
|
||||
peeked.toString()
|
||||
}
|
||||
|
||||
else -> ifNotClosed(peeked) {
|
||||
throw typeMismatch(peeked, Token.STRING)
|
||||
}
|
||||
@@ -211,6 +221,7 @@ internal class JsonValueReader : JsonReader {
|
||||
override fun nextDouble(): Double {
|
||||
val result = when (val peeked = require<Any>(Token.NUMBER)) {
|
||||
is Number -> peeked.toDouble()
|
||||
|
||||
is String -> {
|
||||
try {
|
||||
peeked.toDouble()
|
||||
@@ -218,6 +229,7 @@ internal class JsonValueReader : JsonReader {
|
||||
throw typeMismatch(peeked, Token.NUMBER)
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw typeMismatch(peeked, Token.NUMBER)
|
||||
}
|
||||
@@ -232,6 +244,7 @@ internal class JsonValueReader : JsonReader {
|
||||
override fun nextLong(): Long {
|
||||
val result: Long = when (val peeked = require<Any>(Token.NUMBER)) {
|
||||
is Number -> peeked.toLong()
|
||||
|
||||
is String -> try {
|
||||
peeked.toLong()
|
||||
} catch (e: NumberFormatException) {
|
||||
@@ -241,6 +254,7 @@ internal class JsonValueReader : JsonReader {
|
||||
throw typeMismatch(peeked, Token.NUMBER)
|
||||
}
|
||||
}
|
||||
|
||||
else -> throw typeMismatch(peeked, Token.NUMBER)
|
||||
}
|
||||
remove()
|
||||
@@ -250,6 +264,7 @@ internal class JsonValueReader : JsonReader {
|
||||
override fun nextInt(): Int {
|
||||
val result = when (val peeked = require<Any>(Token.NUMBER)) {
|
||||
is Number -> peeked.toInt()
|
||||
|
||||
is String -> try {
|
||||
peeked.toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
@@ -259,6 +274,7 @@ internal class JsonValueReader : JsonReader {
|
||||
throw typeMismatch(peeked, Token.NUMBER)
|
||||
}
|
||||
}
|
||||
|
||||
else -> throw typeMismatch(peeked, Token.NUMBER)
|
||||
}
|
||||
remove()
|
||||
|
@@ -91,19 +91,23 @@ internal class JsonValueSource @JvmOverloads constructor(
|
||||
stackSize++
|
||||
limit = index + 1
|
||||
}
|
||||
|
||||
']', '}' -> {
|
||||
stackSize--
|
||||
if (stackSize == 0) state = STATE_END_OF_JSON
|
||||
limit = index + 1
|
||||
}
|
||||
|
||||
'\"' -> {
|
||||
state = STATE_DOUBLE_QUOTED
|
||||
limit = index + 1
|
||||
}
|
||||
|
||||
'\'' -> {
|
||||
state = STATE_SINGLE_QUOTED
|
||||
limit = index + 1
|
||||
}
|
||||
|
||||
'/' -> {
|
||||
source.require(index + 2)
|
||||
when (buffer[index + 1]) {
|
||||
@@ -111,20 +115,24 @@ internal class JsonValueSource @JvmOverloads constructor(
|
||||
state = STATE_END_OF_LINE_COMMENT
|
||||
limit = index + 2
|
||||
}
|
||||
|
||||
'*'.code.toByte() -> {
|
||||
state = STATE_C_STYLE_COMMENT
|
||||
limit = index + 2
|
||||
}
|
||||
|
||||
else -> {
|
||||
limit = index + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
'#' -> {
|
||||
state = STATE_END_OF_LINE_COMMENT
|
||||
limit = index + 1
|
||||
}
|
||||
}
|
||||
|
||||
state === STATE_SINGLE_QUOTED || state === STATE_DOUBLE_QUOTED -> {
|
||||
if (b == '\\'.code.toByte()) {
|
||||
source.require(index + 2)
|
||||
@@ -134,6 +142,7 @@ internal class JsonValueSource @JvmOverloads constructor(
|
||||
limit = index + 1
|
||||
}
|
||||
}
|
||||
|
||||
state === STATE_C_STYLE_COMMENT -> {
|
||||
source.require(index + 2)
|
||||
if (buffer[index + 1] == '/'.code.toByte()) {
|
||||
@@ -143,10 +152,12 @@ internal class JsonValueSource @JvmOverloads constructor(
|
||||
limit = index + 1
|
||||
}
|
||||
}
|
||||
|
||||
state === STATE_END_OF_LINE_COMMENT -> {
|
||||
limit = index + 1
|
||||
state = STATE_JSON
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw AssertionError()
|
||||
}
|
||||
|
@@ -168,10 +168,13 @@ internal class JsonValueWriter : JsonWriter() {
|
||||
override fun value(value: Number?): JsonWriter = apply {
|
||||
when (value) {
|
||||
null -> nullValue()
|
||||
|
||||
// If it's trivially converted to a long, do that.
|
||||
is Byte, is Short, is Int, is Long -> value(value.toLong())
|
||||
|
||||
// If it's trivially converted to a double, do that.
|
||||
is Float, is Double -> value(value.toDouble())
|
||||
|
||||
else -> {
|
||||
// Everything else gets converted to a BigDecimal.
|
||||
val bigDecimalValue = if (value is BigDecimal) value else BigDecimal(value.toString())
|
||||
@@ -229,6 +232,7 @@ internal class JsonValueWriter : JsonWriter() {
|
||||
scopes[stackSize - 1] = NONEMPTY_DOCUMENT
|
||||
stack[stackSize - 1] = newTop
|
||||
}
|
||||
|
||||
scope == EMPTY_OBJECT && deferredName != null -> {
|
||||
if (newTop != null || serializeNulls) {
|
||||
// Our maps always have string keys and object values.
|
||||
@@ -242,13 +246,16 @@ internal class JsonValueWriter : JsonWriter() {
|
||||
}
|
||||
deferredName = null
|
||||
}
|
||||
|
||||
scope == EMPTY_ARRAY -> {
|
||||
// Our lists always have object values.
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val list = stack[stackSize - 1] as MutableList<Any?>
|
||||
list.add(newTop)
|
||||
}
|
||||
|
||||
scope == STREAMING_VALUE -> throw IllegalStateException("Sink from valueSink() was not closed")
|
||||
|
||||
else -> throw IllegalStateException("Nesting problem.")
|
||||
}
|
||||
return this
|
||||
|
@@ -151,22 +151,15 @@ public sealed class JsonWriter : Closeable, Flushable {
|
||||
* pretty printing.
|
||||
*/
|
||||
@JvmField
|
||||
@Suppress("ktlint:standard:property-naming") // Exposed to sealed subtypes.
|
||||
@Suppress("ktlint:standard:backing-property-naming") // Exposed to sealed subtypes.
|
||||
protected var _indent: String? = null
|
||||
public open var indent: String
|
||||
/**
|
||||
* Returns a string containing only whitespace, used for each level of indentation. If empty,
|
||||
* the encoded document will be compact.
|
||||
*/
|
||||
get() = _indent.orEmpty()
|
||||
|
||||
/**
|
||||
* Sets the indentation string to be repeated for each level of indentation in the encoded
|
||||
* document. If `indent.isEmpty()` the encoded document will be compact. Otherwise, the
|
||||
* encoded document will be more human-readable.
|
||||
*
|
||||
* @param value a string containing only whitespace.
|
||||
*/
|
||||
/**
|
||||
* A string containing only whitespace, used for each level of indentation.
|
||||
* If empty, the encoded document will be compact.
|
||||
*/
|
||||
public open var indent: String
|
||||
get() = _indent.orEmpty()
|
||||
set(value) {
|
||||
_indent = value.ifEmpty { null }
|
||||
}
|
||||
@@ -402,6 +395,7 @@ public sealed class JsonWriter : Closeable, Flushable {
|
||||
}
|
||||
endObject()
|
||||
}
|
||||
|
||||
is List<*> -> {
|
||||
beginArray()
|
||||
for (element in value) {
|
||||
@@ -409,12 +403,19 @@ public sealed class JsonWriter : Closeable, Flushable {
|
||||
}
|
||||
endArray()
|
||||
}
|
||||
|
||||
is String -> value(value as String?)
|
||||
|
||||
is Boolean -> value(value)
|
||||
|
||||
is Double -> value(value)
|
||||
|
||||
is Long -> value(value)
|
||||
|
||||
is Number -> value(value)
|
||||
|
||||
null -> nullValue()
|
||||
|
||||
else -> throw IllegalArgumentException("Unsupported type: ${value.javaClass.name}")
|
||||
}
|
||||
return this
|
||||
|
@@ -14,14 +14,10 @@ private val NATURAL_ORDER = Comparator<Any> { o1, o2 -> (o1 as Comparable<Any>).
|
||||
* removal.
|
||||
*
|
||||
* This implementation was derived from Android 4.1's TreeMap and LinkedHashMap classes.
|
||||
*/
|
||||
internal class LinkedHashTreeMap<K, V>
|
||||
/**
|
||||
* Create a tree map ordered by [comparator]. This map's keys may only be null if [comparator] permits.
|
||||
*
|
||||
* @param comparator the comparator to order elements with, or null to use the natural ordering.
|
||||
*/
|
||||
constructor(
|
||||
internal class LinkedHashTreeMap<K, V>(
|
||||
comparator: Comparator<Any?>? = null,
|
||||
) : AbstractMutableMap<K, V>(), Serializable {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@@ -402,6 +398,7 @@ constructor(
|
||||
break // no further rotations will be necessary
|
||||
}
|
||||
}
|
||||
|
||||
2 -> {
|
||||
val leftLeft = left!!.left
|
||||
val leftRight = left.right
|
||||
@@ -417,12 +414,14 @@ constructor(
|
||||
break // no further rotations will be necessary
|
||||
}
|
||||
}
|
||||
|
||||
0 -> {
|
||||
node.height = leftHeight + 1 // leftHeight == rightHeight
|
||||
if (insert) {
|
||||
break // the insert caused balance, so rebalancing is done!
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
assert(delta == -1 || delta == 1)
|
||||
node.height = max(leftHeight, rightHeight) + 1
|
||||
@@ -748,6 +747,7 @@ internal class AvlBuilder<K, V> {
|
||||
left.parent = center
|
||||
right.parent = center
|
||||
}
|
||||
|
||||
1 -> {
|
||||
// Pop right and center, then make center the top of the stack.
|
||||
val right = stack
|
||||
@@ -759,6 +759,7 @@ internal class AvlBuilder<K, V> {
|
||||
right.parent = center
|
||||
leavesSkipped = 0
|
||||
}
|
||||
|
||||
2 -> {
|
||||
leavesSkipped = 0
|
||||
}
|
||||
|
@@ -238,11 +238,17 @@ internal object StandardJsonAdapters : JsonAdapter.Factory {
|
||||
override fun fromJson(reader: JsonReader): Any? {
|
||||
return when (reader.peek()) {
|
||||
JsonReader.Token.BEGIN_ARRAY -> listJsonAdapter.fromJson(reader)
|
||||
|
||||
JsonReader.Token.BEGIN_OBJECT -> mapAdapter.fromJson(reader)
|
||||
|
||||
JsonReader.Token.STRING -> stringAdapter.fromJson(reader)
|
||||
|
||||
JsonReader.Token.NUMBER -> doubleAdapter.fromJson(reader)
|
||||
|
||||
JsonReader.Token.BOOLEAN -> booleanAdapter.fromJson(reader)
|
||||
|
||||
JsonReader.Token.NULL -> reader.nextNull()
|
||||
|
||||
else -> throw IllegalStateException(
|
||||
"Expected a value but was ${reader.peek()} at path ${reader.path}",
|
||||
)
|
||||
|
@@ -163,22 +163,27 @@ public object Types {
|
||||
// type is a normal class.
|
||||
type
|
||||
}
|
||||
|
||||
is ParameterizedType -> {
|
||||
// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
|
||||
// suspects some pathological case related to nested classes exists.
|
||||
val rawType = type.rawType
|
||||
rawType as Class<*>
|
||||
}
|
||||
|
||||
is GenericArrayType -> {
|
||||
val componentType = type.genericComponentType
|
||||
java.lang.reflect.Array.newInstance(getRawType(componentType), 0).javaClass
|
||||
}
|
||||
|
||||
is TypeVariable<*> -> {
|
||||
// We could use the variable's bounds, but that won't work if there are multiple. having a raw
|
||||
// type that's more general than necessary is okay.
|
||||
Any::class.java
|
||||
}
|
||||
|
||||
is WildcardType -> getRawType(type.upperBounds[0])
|
||||
|
||||
else -> {
|
||||
val className = type?.javaClass?.name?.toString()
|
||||
throw IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <$type> is of type $className")
|
||||
@@ -222,6 +227,7 @@ public object Types {
|
||||
a == b // Class already specifies equals().
|
||||
}
|
||||
}
|
||||
|
||||
is ParameterizedType -> {
|
||||
// Class instance with generic info, from method return types
|
||||
if (b is Class<*> && a.rawType == b.rawType) {
|
||||
@@ -235,6 +241,7 @@ public object Types {
|
||||
(a.rawType == b.rawType) && aTypeArguments.contentEquals(bTypeArguments)
|
||||
)
|
||||
}
|
||||
|
||||
is GenericArrayType -> {
|
||||
if (b is Class<*>) {
|
||||
return equals(b.componentType, a.genericComponentType)
|
||||
@@ -242,14 +249,17 @@ public object Types {
|
||||
if (b !is GenericArrayType) return false
|
||||
return equals(a.genericComponentType, b.genericComponentType)
|
||||
}
|
||||
|
||||
is WildcardType -> {
|
||||
if (b !is WildcardType) return false
|
||||
return (a.upperBounds.contentEquals(b.upperBounds) && a.lowerBounds.contentEquals(b.lowerBounds))
|
||||
}
|
||||
|
||||
is TypeVariable<*> -> {
|
||||
if (b !is TypeVariable<*>) return false
|
||||
return (a.genericDeclaration === b.genericDeclaration && (a.name == b.name))
|
||||
}
|
||||
|
||||
else -> return false // This isn't a supported type.
|
||||
}
|
||||
}
|
||||
@@ -305,12 +315,16 @@ public object Types {
|
||||
) { proxy, method, args ->
|
||||
when (method.name) {
|
||||
"annotationType" -> annotationType
|
||||
|
||||
"equals" -> {
|
||||
val o = args[0]
|
||||
annotationType.isInstance(o)
|
||||
}
|
||||
|
||||
"hashCode" -> 0
|
||||
|
||||
"toString" -> "@${annotationType.name}()"
|
||||
|
||||
else -> method.invoke(proxy, *args)
|
||||
}
|
||||
} as T
|
||||
|
@@ -176,18 +176,22 @@ internal fun Type.canonicalize(): Type {
|
||||
is Class<*> -> {
|
||||
if (isArray) GenericArrayTypeImpl(this@canonicalize.componentType.canonicalize()) else this
|
||||
}
|
||||
|
||||
is ParameterizedType -> {
|
||||
if (this is ParameterizedTypeImpl) return this
|
||||
ParameterizedTypeImpl(ownerType, rawType, *actualTypeArguments)
|
||||
}
|
||||
|
||||
is GenericArrayType -> {
|
||||
if (this is GenericArrayTypeImpl) return this
|
||||
GenericArrayTypeImpl(genericComponentType)
|
||||
}
|
||||
|
||||
is WildcardType -> {
|
||||
if (this is WildcardTypeImpl) return this
|
||||
WildcardTypeImpl(upperBounds, lowerBounds)
|
||||
}
|
||||
|
||||
else -> this // This type is unsupported!
|
||||
}
|
||||
}
|
||||
@@ -226,18 +230,21 @@ private fun Type.resolve(
|
||||
toResolve = resolveTypeVariable(context, contextRawType, typeVariable)
|
||||
if (toResolve === typeVariable) return toResolve
|
||||
}
|
||||
|
||||
toResolve is Class<*> && toResolve.isArray -> {
|
||||
val original = toResolve
|
||||
val componentType: Type = original.componentType
|
||||
val newComponentType = componentType.resolve(context, contextRawType, visitedTypeVariables)
|
||||
return if (componentType === newComponentType) original else newComponentType.asArrayType()
|
||||
}
|
||||
|
||||
toResolve is GenericArrayType -> {
|
||||
val original = toResolve
|
||||
val componentType = original.genericComponentType
|
||||
val newComponentType = componentType.resolve(context, contextRawType, visitedTypeVariables)
|
||||
return if (componentType === newComponentType) original else newComponentType.asArrayType()
|
||||
}
|
||||
|
||||
toResolve is ParameterizedType -> {
|
||||
val original = toResolve
|
||||
val ownerType: Type? = original.ownerType
|
||||
@@ -258,6 +265,7 @@ private fun Type.resolve(
|
||||
}
|
||||
return if (changed) ParameterizedTypeImpl(newOwnerType, original.rawType, *args) else original
|
||||
}
|
||||
|
||||
toResolve is WildcardType -> {
|
||||
val original = toResolve
|
||||
val originalLowerBound = original.lowerBounds
|
||||
@@ -275,6 +283,7 @@ private fun Type.resolve(
|
||||
}
|
||||
return original
|
||||
}
|
||||
|
||||
else -> return toResolve
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user