Add KeyableFormat for Map json serialization (#125). #331
+67
−5
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
mapFormatmethod and improve the extensibility of this method. ThemapFormatmethod provides aRootJsonFormatto serializescala.Predef#Maps asJsObjects and vice versa.Uncertain behavior
The signature of the
mapFormatmethod incorrectly implies that all types of keys are applicable to serialize aMapas aJsObjectas long as these keys are serializable asJsValue. Thus trying to serialize a Map with a key which is not serialized asJsStringresults in aSerializationException.This PR will modify the method signature such that an implicit
KeyableFormatinstead of aJsonFormatis required. AKeyableFormatimplies 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 surprisingSerializationExceptions while serializing aMapwithout a valid key format.Extensibility
The newly introduced
KeyableFormattrait is extendable and enables user to provide their own format to serialize their keys. A simple and probably most generousKeyableFormatforStringis provided in theKeyableFormatsobject.Is this is a breaking change?
I would argue partially: The signature of an implicit method was changed by replacing the implicit
JsonFormatargument for the key to an implicitKeyableFormatand theDefaultJsonProtocolprovides 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.