runtime icon indicating copy to clipboard operation
runtime copied to clipboard

JsonSerializer ignores IntPtr

Open Dimension4 opened this issue 5 years ago • 10 comments

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 IntPtr is 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

Dimension4 avatar Jan 30 '21 21:01 Dimension4

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 IntPtr is 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:

area-System.Text.Json, untriaged

Milestone: -

ghost avatar Jan 30 '21 21:01 ghost

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 avatar Feb 02 '21 17:02 eiriktsarpalis

@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).

layomia avatar Feb 02 '21 18:02 layomia

Ok, in that case throwing for IntPtr is something we could consider changing.

eiriktsarpalis avatar Feb 02 '21 19:02 eiriktsarpalis

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).

eiriktsarpalis avatar Feb 03 '21 16:02 eiriktsarpalis

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.

GrabYourPitchforks avatar Feb 04 '21 06:02 GrabYourPitchforks

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?

SkiFoD avatar Feb 04 '21 17:02 SkiFoD

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.

eiriktsarpalis avatar Feb 04 '21 19:02 eiriktsarpalis

Sure, this is marked up-for-grabs so happy to accept community contributions.

Great. Could you assign it to me?

SkiFoD avatar Feb 04 '21 21:02 SkiFoD

Fixed by https://github.com/dotnet/runtime/pull/47899.

layomia avatar Apr 09 '21 15:04 layomia