Add KeyableFormat for Map json serialization (#125). #331
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims both to resolve an unclear behavior of the
mapFormat
method and improve the extensibility of this method. ThemapFormat
method provides aRootJsonFormat
to serializescala.Predef#Map
s asJsObject
s and vice versa.Uncertain behavior
The signature of the
mapFormat
method incorrectly implies that all types of keys are applicable to serialize aMap
as aJsObject
as long as these keys are serializable asJsValue
. Thus trying to serialize a Map with a key which is not serialized asJsString
results in aSerializationException
.This PR will modify the method signature such that an implicit
KeyableFormat
instead of aJsonFormat
is required. AKeyableFormat
implies that a key must be serializable as aString
- the only valid key format in JSON - and it can be deserialized back from aString
. This change will improve the type-safety of the method and prevent surprisingSerializationException
s while serializing aMap
without a valid key format.Extensibility
The newly introduced
KeyableFormat
trait is extendable and enables user to provide their own format to serialize their keys. A simple and probably most generousKeyableFormat
forString
is provided in theKeyableFormats
object.Is this is a breaking change?
I would argue partially: The signature of an implicit method was changed by replacing the implicit
JsonFormat
argument for the key to an implicitKeyableFormat
and theDefaultJsonProtocol
provides the defaultStringKeyableFormat
. Thus most users may not be affected by this change. Nevertheless it is a change in a public API and should be considered as breaking change.