feat(hashmap): add FromJson impl for HashMap[String, V] to match Map - #3584
Conversation
Coverage Report for CI Build 6265Coverage increased (+0.002%) to 90.793%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
There was a problem hiding this comment.
Pull request overview
Adds JSON deserialization support for HashMap[String, V] to align HashMap’s API with the existing Map[String, V] FromJson implementation in moonbitlang/core/json.
Changes:
- Added
impl @json.FromJson for HashMap[String, V]inhashmap/json.mbt. - Added
moonbitlang/core/jsonas a non-test dependency for thehashmappackage. - Updated generated package interface metadata (
pkg.generated.mbti) to reflect the new import and impl.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| hashmap/json.mbt | Implements @json.FromJson for HashMap[String, V] and documents usage. |
| hashmap/moon.pkg | Adds moonbitlang/core/json to non-test imports so the new impl can compile/use @json. |
| hashmap/pkg.generated.mbti | Regenerates package interface to include the new json import and FromJson impl. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| guard json is Object(obj) else { | ||
| raise @json.JsonDecodeError((path, "HashMap::from_json: expected object")) | ||
| } | ||
| let res : HashMap[String, V] = HashMap([]) |
| pub impl[V : @json.FromJson] @json.FromJson for HashMap[String, V] with from_json( | ||
| json, | ||
| path, | ||
| ) { | ||
| guard json is Object(obj) else { | ||
| raise @json.JsonDecodeError((path, "HashMap::from_json: expected object")) | ||
| } |
`HashMap` could be serialized to JSON but not read back: `ToJson` has been there for a while and `FromJson` never landed, so a map could make a round trip in one direction only. `Map`, `SortedMap` and `@immut/sorted_map` all carry the object-decoding impl already; this brings `HashMap` in line with them. The impl follows `@sorted_map`'s exactly -- an object is required, each member is decoded with `V`'s impl, and the path is extended with the member's key so a failure names the entry rather than the whole object. Since the object's size is known up front, the table is sized once instead of rehashing as the entries go in. Restricting the key to `String` matches every other map impl in core. `ToJson` accepts any `K : Show`, so a round trip is only guaranteed for `String` keys, which is the same asymmetry `Map` has. Tests cover a generated round trip, an empty object, nested values, a non-object input, and the decode path reported when one member fails -- replacing `path.add_key(k)` with `path` fails that last test. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
fea7121 to
afac6d8
Compare
|
Rebased onto What the rebase changedThe only conflicting file was the generated
Codex CLI review (
|
Summary
Adds
impl FromJson for HashMap[String, V]to mirror the same impl thatMap[String, V]already has injson/from_json.mbt.Motivation
MapandHashMapshould have a consistent API.HashMapwas missing JSON deserialization support.Changes
impl @json.FromJson for HashMap[String, V]inhashmap/json.mbtmoonbitlang/core/jsonas a non-test import inhashmap/moon.pkgJsonPath::add_keyAPI (enum variants are private)pkg.generated.mbti