-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCSP-36217: polymorphic serialization (#163)
* DOCSP-36217: polymorphic serialization * wip * wip * wip * fix error in codectest * MM PR fixes 1
- Loading branch information
Showing
10 changed files
with
171 additions
and
58 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
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
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
22 changes: 22 additions & 0 deletions
22
source/examples/generated/KotlinXSerializationTest.snippet.polymorphic-dataclasses.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,22 @@ | ||
@Serializable | ||
sealed interface Person { | ||
val name: String | ||
} | ||
|
||
@Serializable | ||
data class Student( | ||
@Contextual | ||
@SerialName("_id") | ||
val id: ObjectId, | ||
override val name: String, | ||
val grade: Int, | ||
) : Person | ||
|
||
@Serializable | ||
data class Teacher( | ||
@Contextual | ||
@SerialName("_id") | ||
val id: ObjectId, | ||
override val name: String, | ||
val department: String, | ||
) : Person |
24 changes: 24 additions & 0 deletions
24
source/examples/generated/KotlinXSerializationTest.snippet.polymorphic-example.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,24 @@ | ||
val collection = database.getCollection<Person>("school") | ||
|
||
val teacherDoc = Teacher(ObjectId(), "Vivian Lee", "History") | ||
val studentDoc = Student(ObjectId(), "Kate Parker", 10) | ||
|
||
collection.insertOne(teacherDoc) | ||
collection.insertOne(studentDoc) | ||
|
||
println("Retrieving by using data classes") | ||
collection.withDocumentClass<Teacher>() | ||
.find(Filters.exists("department")) | ||
.first().also { println(it) } | ||
|
||
collection.withDocumentClass<Student>() | ||
.find(Filters.exists("grade")) | ||
.first().also { println(it) } | ||
|
||
println("\nRetrieving by using Person interface") | ||
val resultsFlow = collection.withDocumentClass<Person>().find() | ||
resultsFlow.collect { println(it) } | ||
|
||
println("\nRetrieving as Document type") | ||
val resultsDocFlow = collection.withDocumentClass<Document>().find() | ||
resultsDocFlow.collect { println(it) } |
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