Provide a way to deserailize to a typed apex class, while also accessing any data for keys which were unmapped.
Apex
class Foo {
String foo;
Integer bar;
}
JSON
{
"foo": "abc",
"bar": 123,
"unknownProp": "you didn't know I would be here",
"from": "from is reserved keyword. Could map this even if you wanted",
}
When calling Foo f = (Foo) JSON.deseralize(jsonStr, Foo.class);
the unmapped properties (unknownProp
& from
) are essentially lost.
You could use JSON.deserializeUntyped
as an additional call or with custom mapping logic to solve this problem, but this is either a waste of CPU resources or developer time.
A better solution would be to provide the ability to both deseralize to a class AND capture any untyped properties in one call:
Map<String, Object> untypedProps = new Map<String, Object>();
Foo f = (Foo) JSON.deserialize(jsonStr, Foo.class, untypedProps);
JSON.DeserializeResult res = JSON.deserializeWithUnmapped(jsonStr, Foo.class); // working title :)
Foo f = (Foo) res.mappedResult;
Map<String, Object> untypedProps = res.unmappedResult;
class Foo {
String foo;
Integer bar;
@UnMappedJson //working title :)
Map<String, Object> untypedProps;
}
Foo f = (Foo) JSON.deserialize(jsonStr, Foo.class);