mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Report json name if different from property name in kotlin (#917)
* Report json name in code gen if different from property name Resolves #800 * Unify required property name error message * Report json name in kotlinjsonadapter, match code gen * Upper case JSON Co-Authored-By: Jake Wharton <jakew@google.com> * Upper case JSON Co-Authored-By: Jake Wharton <jakew@google.com> * Don't keep constants * Inline json name - property name comparison to util methods * Remove unnecessary constructor keyword * Consolidate non-null/missing property tests to parameterized suite * Add custom json name tests for nonNull property checks * Rename test to make maven happy Maven won't run the test unless it ends with `Test` or `TestCase`
This commit is contained in:
@@ -203,7 +203,7 @@ internal class AdapterGenerator(
|
||||
result.addStatement("%N = %N.fromJson(%N)",
|
||||
property.localName, nameAllocator[property.delegateKey], readerParam)
|
||||
} else {
|
||||
val exception = unexpectedNull(property.localName, readerParam)
|
||||
val exception = unexpectedNull(property, readerParam)
|
||||
result.addStatement("%N = %N.fromJson(%N) ?: throw·%L",
|
||||
property.localName, nameAllocator[property.delegateKey], readerParam, exception)
|
||||
}
|
||||
@@ -221,7 +221,7 @@ internal class AdapterGenerator(
|
||||
result.addStatement("%L -> %N = %N.fromJson(%N)",
|
||||
propertyIndex, property.localName, nameAllocator[property.delegateKey], readerParam)
|
||||
} else {
|
||||
val exception = unexpectedNull(property.localName, readerParam)
|
||||
val exception = unexpectedNull(property, readerParam)
|
||||
result.addStatement("%L -> %N = %N.fromJson(%N) ?: throw·%L",
|
||||
propertyIndex, property.localName, nameAllocator[property.delegateKey], readerParam,
|
||||
exception)
|
||||
@@ -308,7 +308,8 @@ internal class AdapterGenerator(
|
||||
}
|
||||
if (!property.isTransient && property.isRequired) {
|
||||
val missingPropertyBlock =
|
||||
CodeBlock.of("%T.missingProperty(%S, %N)", Util::class, property.localName, readerParam)
|
||||
CodeBlock.of("%T.missingProperty(%S, %S, %N)",
|
||||
MOSHI_UTIL, property.localName, property.jsonName, readerParam)
|
||||
result.addCode(" ?: throw·%L", missingPropertyBlock)
|
||||
}
|
||||
separator = ",\n"
|
||||
@@ -341,8 +342,9 @@ internal class AdapterGenerator(
|
||||
return result.build()
|
||||
}
|
||||
|
||||
private fun unexpectedNull(identifier: String, reader: ParameterSpec): CodeBlock {
|
||||
return CodeBlock.of("%T.unexpectedNull(%S, %N)", Util::class, identifier, reader)
|
||||
private fun unexpectedNull(property: PropertyGenerator, reader: ParameterSpec): CodeBlock {
|
||||
return CodeBlock.of("%T.unexpectedNull(%S, %S, %N)",
|
||||
MOSHI_UTIL, property.localName, property.jsonName, reader)
|
||||
}
|
||||
|
||||
private fun generateToJsonFun(): FunSpec {
|
||||
|
Reference in New Issue
Block a user