Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializers for common java types #350

Closed
wants to merge 20 commits into from
Closed

Serializers for common java types #350

wants to merge 20 commits into from

Conversation

qwwdfsad
Copy link
Collaborator

No description provided.

sandwwraith and others added 19 commits November 27, 2018 15:38
… polymorphic module builder and fixed old tests (working only in bootstrap mode :()
Introduce SubtypeNotRegisteredException

Small testing utilities and refactoring of test data
because Konan needs to be updated to be able to work with -dev-1984 plugin.
'todo: arrays' erased intentionally because we won't support polymorphic arrays – at least for now.
…te files. Some methods converted to extensions.
… implementation (except polymorphic, which is already tested indirectly)
# Conflicts:
#	build.gradle
#	gradle.properties
#	runtime/common/src/main/kotlin/kotlinx/serialization/Annotations.kt
#	runtime/common/src/test/kotlin/kotlinx/serialization/features/SkipDefaults.kt
#	runtime/js/src/main/kotlin/kotlinx/serialization/Serialization.kt
#	runtime/jvm/src/main/kotlin/kotlinx/serialization/Serialization.kt
#	runtime/native/src/main/kotlin/kotlinx/serialization/Platform.kt
import kotlin.reflect.*

/**
* Module with external serializers for the following Java types:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are you going to combat this class from playing whack-a-mole forever in the future from people wanting their type included. For example, why isn't AtomicIntegerArray included? Why isn't java.time.*. Why isn't File?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind including other types on a demand-driven basis when it is reasonable, an additional bonus is that this kind of features is a good "first-time contribution".

The only question is what is a "reasonable" request. E.g. java.sql.Date is a good candidate, while java.io.File is definitely not (at least because its representation is OS/machine/JVM specific). I know no strict criteria for a type to be included except common sense.

@sandwwraith
Copy link
Member

@qwwdfsad IMO we should move all this stuff to another module to publish separately, just like add-on formats or schema generators. People who are using the library in multiplatform context don't need all these java.* bits. We can also incrementally grow this module without fear of bloating main library itself.

* Serializer for [java.util.Date] which serializers date as format string in default US locale.
*/
public object DateSerializer : KSerializer<Date> {
private val format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US)!!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a non standard format, without a time zone reference?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, there are actually a number of different relevant standards. You probably want to create serializers for each of them. As well as with or without time. Of course maybe dates/calendars/instants (whichever date type you want to use) are not the best starting point for this.

@qwwdfsad
Copy link
Collaborator Author

TODO: discuss FQN in descriptors

private val format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US)!!

override fun serialize(encoder: Encoder, obj: Date) {
val string = synchronized(format) { format.format(obj) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you've preferred synchronized on a format instead of making it ThreadLocal?

@sandwwraith sandwwraith force-pushed the new_polymorphic branch 2 times, most recently from cdcbf81 to 50e8af8 Compare March 22, 2019 14:19
@qwwdfsad qwwdfsad changed the base branch from new_polymorphic to dev March 22, 2019 15:03
@sandwwraith sandwwraith reopened this Mar 26, 2019
@pdvrieze
Copy link
Contributor

pdvrieze commented Jul 2, 2019

@qwwdfsad IMO we should move all this stuff to another module to publish separately, just like add-on formats or schema generators. People who are using the library in multiplatform context don't need all these java.* bits. We can also incrementally grow this module without fear of bloating main library itself.

I think so too. I have my own suggested additions as well (serializers to generate hashcode?). But that doesn't really belong in the main runtime support library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants