-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide support for JsonNamingStrategy to be used in Json for propert…
- Loading branch information
1 parent
694e2f7
commit 60c632c
Showing
21 changed files
with
769 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
benchmark/src/jmh/kotlin/kotlinx/benchmarks/model/MacroTwitterUntailored.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package kotlinx.benchmarks.model | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
|
||
/** | ||
* All model classes are the same as in MacroTwitter.kt but named accordingly to Kotlin naming policies to test JsonNamingStrategy performance. | ||
* Only Size, SizeType and Urls are not copied | ||
*/ | ||
|
||
@Serializable | ||
data class MacroTwitterFeedKt( | ||
val statuses: List<TwitterStatusKt>, | ||
val searchMetadata: SearchMetadata | ||
) | ||
|
||
@Serializable | ||
data class MicroTwitterFeedKt( | ||
val statuses: List<TwitterTrimmedStatusKt> | ||
) | ||
|
||
@Serializable | ||
data class TwitterTrimmedStatusKt( | ||
val metadata: MetadataKt, | ||
val createdAt: String, | ||
val id: Long, | ||
val idStr: String, | ||
val text: String, | ||
val source: String, | ||
val truncated: Boolean, | ||
val user: TwitterTrimmedUserKt, | ||
val retweetedStatus: TwitterTrimmedStatusKt? = null, | ||
) | ||
|
||
@Serializable | ||
data class TwitterStatusKt( | ||
val metadata: MetadataKt, | ||
val createdAt: String, | ||
val id: Long, | ||
val idStr: String, | ||
val text: String, | ||
val source: String, | ||
val truncated: Boolean, | ||
val inReplyToStatusId: Long?, | ||
val inReplyToStatusIdStr: String?, | ||
val inReplyToUserId: Long?, | ||
val inReplyToUserIdStr: String?, | ||
val inReplyToScreenName: String?, | ||
val user: TwitterUserKt, | ||
val geo: String?, | ||
val coordinates: String?, | ||
val place: String?, | ||
val contributors: List<String>?, | ||
val retweetedStatus: TwitterStatusKt? = null, | ||
val retweetCount: Int, | ||
val favoriteCount: Int, | ||
val entities: StatusEntitiesKt, | ||
val favorited: Boolean, | ||
val retweeted: Boolean, | ||
val lang: String, | ||
val possiblySensitive: Boolean? = null | ||
) | ||
|
||
@Serializable | ||
data class StatusEntitiesKt( | ||
val hashtags: List<Hashtag>, | ||
val symbols: List<String>, | ||
val urls: List<Url>, | ||
val userMentions: List<TwitterUserMentionKt>, | ||
val media: List<TwitterMediaKt>? = null | ||
) | ||
|
||
@Serializable | ||
data class TwitterMediaKt( | ||
val id: Long, | ||
val idStr: String, | ||
val url: String, | ||
val mediaUrl: String, | ||
val mediaUrlHttps: String, | ||
val expandedUrl: String, | ||
val displayUrl: String, | ||
val indices: List<Int>, | ||
val type: String, | ||
val sizes: SizeType, | ||
val sourceStatusId: Long? = null, | ||
val sourceStatusIdStr: String? = null | ||
) | ||
|
||
@Serializable | ||
data class TwitterUserMentionKt( | ||
val screenName: String, | ||
val name: String, | ||
val id: Long, | ||
val idStr: String, | ||
val indices: List<Int> | ||
) | ||
|
||
@Serializable | ||
data class MetadataKt( | ||
val resultType: String, | ||
val isoLanguageCode: String | ||
) | ||
|
||
@Serializable | ||
data class TwitterTrimmedUserKt( | ||
val id: Long, | ||
val idStr: String, | ||
val name: String, | ||
val screenName: String, | ||
val location: String, | ||
val description: String, | ||
val url: String?, | ||
val entities: UserEntitiesKt, | ||
val protected: Boolean, | ||
val followersCount: Int, | ||
val friendsCount: Int, | ||
val listedCount: Int, | ||
val createdAt: String, | ||
val favouritesCount: Int, | ||
) | ||
|
||
@Serializable | ||
data class TwitterUserKt( | ||
val id: Long, | ||
val idStr: String, | ||
val name: String, | ||
val screenName: String, | ||
val location: String, | ||
val description: String, | ||
val url: String?, | ||
val entities: UserEntitiesKt, | ||
val protected: Boolean, | ||
val followersCount: Int, | ||
val friendsCount: Int, | ||
val listedCount: Int, | ||
val createdAt: String, | ||
val favouritesCount: Int, | ||
val utcOffset: Int?, | ||
val timeZone: String?, | ||
val geoEnabled: Boolean, | ||
val verified: Boolean, | ||
val statusesCount: Int, | ||
val lang: String, | ||
val contributorsEnabled: Boolean, | ||
val isTranslator: Boolean, | ||
val isTranslationEnabled: Boolean, | ||
val profileBackgroundColor: String, | ||
val profileBackgroundImageUrl: String, | ||
val profileBackgroundImageUrlHttps: String, | ||
val profileBackgroundTile: Boolean, | ||
val profileImageUrl: String, | ||
val profileImageUrlHttps: String, | ||
val profileBannerUrl: String? = null, | ||
val profileLinkColor: String, | ||
val profileSidebarBorderColor: String, | ||
val profileSidebarFillColor: String, | ||
val profileTextColor: String, | ||
val profileUseBackgroundImage: Boolean, | ||
val defaultProfile: Boolean, | ||
val defaultProfileImage: Boolean, | ||
val following: Boolean, | ||
val followRequestSent: Boolean, | ||
val notifications: Boolean | ||
) | ||
|
||
@Serializable | ||
data class UserEntitiesKt( | ||
val url: Urls? = null, | ||
val description: Urls | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...on-tests/commonTest/src/kotlinx/serialization/features/JsonNamingStrategyExclusionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package kotlinx.serialization.features | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlin.test.* | ||
|
||
class JsonNamingStrategyExclusionTest : JsonTestBase() { | ||
@SerialInfo | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY) | ||
annotation class OriginalSerialName | ||
|
||
private fun List<Annotation>.hasOriginal() = filterIsInstance<OriginalSerialName>().isNotEmpty() | ||
|
||
private val myStrategy = JsonNamingStrategy { descriptor, index, serialName -> | ||
if (descriptor.annotations.hasOriginal() || descriptor.getElementAnnotations(index).hasOriginal()) serialName | ||
else JsonNamingStrategy.SnakeCase.serialNameForJson(descriptor, index, serialName) | ||
} | ||
|
||
@Serializable | ||
@OriginalSerialName | ||
data class Foo(val firstArg: String = "a", val secondArg: String = "b") | ||
|
||
enum class E { | ||
@OriginalSerialName | ||
FIRST_E, | ||
SECOND_E | ||
} | ||
|
||
@Serializable | ||
data class Bar( | ||
val firstBar: String = "a", | ||
@OriginalSerialName val secondBar: String = "b", | ||
val fooBar: Foo = Foo(), | ||
val enumBarOne: E = E.FIRST_E, | ||
val enumBarTwo: E = E.SECOND_E | ||
) | ||
|
||
private fun doTest(json: Json) { | ||
val j = Json(json) { | ||
namingStrategy = myStrategy | ||
} | ||
val bar = Bar() | ||
assertJsonFormAndRestored( | ||
Bar.serializer(), | ||
bar, | ||
"""{"first_bar":"a","secondBar":"b","foo_bar":{"firstArg":"a","secondArg":"b"},"enum_bar_one":"FIRST_E","enum_bar_two":"SECOND_E"}""", | ||
j | ||
) | ||
} | ||
|
||
@Test | ||
fun testJsonNamingStrategyWithAlternativeNames() = doTest(Json(default) { | ||
useAlternativeNames = true | ||
}) | ||
|
||
@Test | ||
fun testJsonNamingStrategyWithoutAlternativeNames() = doTest(Json(default) { | ||
useAlternativeNames = false | ||
}) | ||
} |
Oops, something went wrong.