JsonSerializer ignores IntPtr
System.Text.Json.JsonSerializer.Serialize(new {Handle = (IntPtr) 123});
results in
{
"Handle": { }
}
Instead of the current behavior, I would expect one of two behaviors:
- Either, the
IntPtris serialized just fine (e.g. as a number). This would probably make the most sense since other serialization mechanisms serialize this type just fine. - Or, an exception is thrown (e.g.
NotSupportedException).
Happens on .net 5.0.102
Relevant discussion: #47164
Tagging subscribers to this area: @eiriktsarpalis, @layomia See info in area-owners.md if you want to be subscribed.
Issue Details
System.Text.Json.JsonSerializer.Serialize(new {Handle = (IntPtr) 123});
results in
{
"Handle": { }
}
Instead of the current behavior, I would expect one of two behaviors:
- Either, the
IntPtris serialized just fine (e.g. as a number). This would probably make the most sense since other serialization mechanisms serialize this type just fine. - Or, an exception is thrown (e.g.
NotSupportedException).
Happens on .net 5.0.102
| Author: | Dimension4 |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
I don't think we should support serializing IntPtr OOTB since it really isn't marshallable information. On the other hand, throwing exceptions would likely be a breaking change, even if we only do it for IntPtr. (I'm assuming that S.T.J. will do this in principle for any property whose type doesn't have a converter registered, @layomia can you confirm this is the case?).
The workaround seems pretty straightforward: just register a custom converter for IntPtr. Assuming users have a valid use case, I think it's probably better to guide them down a path of thinking about what marshalling a pointer means, rather than serving some general-purpose converter out of the box.
In light of the above, I think we should probably close this issue.
@eiriktsarpalis I'd say we can throw NotSupportedException for types we explicitly don't want to support. We don't mind making the breaking change, especially if our current behavior is "lossy" (and perhaps surprising) like in this case. Some examples where we made such changes are with SerializationInfo (https://github.com/dotnet/runtime/pull/42605) and Type (https://github.com/dotnet/runtime/pull/34249). We also have future plans to go from undefined/unknown behavior, to throwing NotSupportedException (https://github.com/dotnet/runtime/pull/42605#issuecomment-697049134).
Ok, in that case throwing for IntPtr is something we could consider changing.
Triage: we'd be supportive of changing this into throwing NotSupportedException. The mechanism that we use for disallowing types is DisallowedTypeConverterFactory. See also this https://github.com/dotnet/runtime/pull/42605#issuecomment-697874991 on how to add a new error message (if necessary).
Remember to be consistent between IntPtr and UIntPtr, regardless of whether your final decision be to support it or to reject it. I'd also suggest confirming the presence of a unit test to validate that void* and other real pointer types are rejected or otherwise ignored during the serialization process.
Hey everyone. I have done some research of the issue and figured out that unsafe pointers were already being rejected with InvalidOperationException thrown along. Should this behavior be covered with a unit test in addition to the main issue? Also I found that System.Text.Json tests were failed when the target framework was net461 because System.Runtime.CompilerServices.Unsafe assembly was not found. Is it okay and I have to work only with net6.0? Although I'm new here , I'd like to work with the bug. Don't you mind?
Although I'm new here , I'd like to work with the bug. Don't you mind?
Sure, this is marked up-for-grabs so happy to accept community contributions.
Sure, this is marked up-for-grabs so happy to accept community contributions.
Great. Could you assign it to me?
Fixed by https://github.com/dotnet/runtime/pull/47899.