Add Autoconversion of elegible Objects to Json #125
Merged
+69
−2
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.
Issue to be solved
A lot of times, a Collection or Map contains Non-Json Values and cannot be serialized directly.
The solution
Expose an interface,
JsonWritable
for an arbitrary class to represent an instance of itself as a Json-Conforming value via thetoJsonValue
. The serialization method must yield a String, Number,boolean
,Map
,Collection
,null
or another instance ofJsonWritable
. The process is applied recursively.Alternatives
Manually loop through these sorts of Collections and Maps, writing each entry, manually converting them through non-standard mechanisms, recursively if necessary.
Risks and Assumptions
The only risk this solution poses is the risk of a
StackOverflowError
, if an object maliciously returns another instance of its type directly or indirectly intoJsonValue
. This case is assumed to be rare at best, as people are not expected to crash the writer on purpose, and returning a value other than the standard values types is an active process, so its unlikely to be done by accident. It is assumed that the overhead resulting from the call totoJsonValue
is minimal, as it is just a wrapper for the manual conversions Users already have to do.