I'm attempting to parse json from the GitHub API with play-json, and encountering a problem with the merge_commit_sha
field on Pull Requests (incidentally, I know this field is deprecated, but don't want to discuss that in this parsing problem!). Unfortunately merge_commit_sha
field comes back as the empty string in some cases:
"merge_commit_sha": ""
This is how the field is declared in my case class:
merge_commit_sha: Option[ObjectId],
I have an implicit Format[ObjectId]
, which does not tolerate the empty string, because that's not a valid value for a Git hash id. I'm also using a play-json macro-generated Read[PullRequest]
, which I'd like to keep on using, in preference to individually declaring reads for every single field on pull requests.
As I've declared the field to be an Option, I'd like "merge_commit_sha": ""
to be read as the value None
, but this is not what currently happens - a string is present, so the Format[ObjectId]
is invoked, and returns a JsFailure
.
One thing I tried was declaring an implicit Format[Option[ObjectId]]
with the required behaviour, but it didn't seem to get used by the macro-generated Read[PullRequest]
.