Skip to content

Commit 593ffbc

Browse files
committed
Clarify the definition of deserializable type objects
- nominalize nominalizables - ensure that the nominalization is a nominal type - consider compliant if there is any public or `is build` attribute
1 parent 399060a commit 593ffbc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Revision history for JSON-Unmarshal
22

33
{{$NEXT}}
4+
- Nominalize type objects first if they're nominalizable
5+
- Clarify the rule defining deserializable type objects: a nominal with at least one public attribute
6+
- Consider `is build`-marked attributes as public
47

58
0.14 2023-07-25T18:54:32+02:00
69
- Tighten up the _unmarshal(%, Mu) candidate to not match Positional|Associative target

lib/JSON/Unmarshal.rakumod

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ multi sub panic($json, Mu \type, Str $why?) {
203203
:target($*JSON-UNMARSHAL-TYPE) ).throw
204204
}
205205

206+
my sub maybe-nominalize(Mu \obj) is pure is raw {
207+
obj.HOW.archetypes.nominalizable ?? obj.^nominalize !! obj
208+
}
209+
206210
multi _unmarshal(Any:U, Mu $type) {
207211
$type;
208212
}
@@ -248,13 +252,22 @@ multi _unmarshal(Any:D $json, Bool) {
248252
return Bool($json);
249253
}
250254

251-
subset PosNoAccessor of Positional where { ! .^attributes.first(*.has_accessor) };
255+
subset PosNoAccessor of Positional where { ! maybe-nominalize($_).^attributes.first({ .has_accessor || .is_built }) };
252256

253257
multi _unmarshal(%json, PosNoAccessor $obj ) {
254258
panic(%json, Positional, "type mismatch");
255259
}
256260

257-
multi _unmarshal(%json, Mu $obj is raw where { ? .^attributes.first(*.has_accessor) }) {
261+
# A class-like type is the one we can instantiate and it has at least one public or `is build`-marked attribute.
262+
subset ClassLike of Mu
263+
where -> Mu \type {
264+
.HOW.archetypes.nominal
265+
&& .HOW.^can('attributes')
266+
&& .^attributes.first({ $_ ~~ Attribute && (.has_accessor || .is_built) })
267+
given maybe-nominalize(type)
268+
};
269+
270+
multi _unmarshal(%json, ClassLike $obj is raw) {
258271
my %args;
259272
my $params = $*JSON-UNMARSHALL-PARAMS;
260273
my SetHash $used-json-keys .= new;

0 commit comments

Comments
 (0)