Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

Make JsonSerializationException.Create public

Open Pzixel opened this issue 6 years ago • 6 comments

I'm writing my own JsonConverter which works perfectly, except for his erros being a bit cryptic. There is an API that allows converters to provide more information, specifially about the place where error occured. Unfortunately, there are internal for some reason.

This PR makes this methods public to solve this issue.

Pzixel avatar Jan 24 '19 15:01 Pzixel

I don't like making internal methods public that aren't designed to be used publicly. Is there any reason you can't copy of the code from JsonSerializationException.Create and JsonPosition.FormatMessage, and throw the error yourself?

JamesNK avatar Mar 30 '19 06:03 JamesNK

Because copying method implementation is a bad practice marks it as "needed publicly"?

Is there any reasons why exception constructor is public but a thin wrapper on top of it is internal, and you suggest to copypaste its implementation?

Pzixel avatar Mar 30 '19 10:03 Pzixel

Using reflection you can still get you access to internal or private methods or constructors. Yes, reflection is not very performant but exceptions are not cheap in the first place, therefore using reflection should not have a big impact in your case and is probably the best compromise here

bergmeister avatar Jun 30 '19 09:06 bergmeister

So? I really hope this gets merged. What's the danger of making those methods public? I don't see any reason why they couldn't be public. Users cannot break it in any way and may find them useful: they acually will when defining their owns JsonConverters, because currently there is no way to express it via API and clearly Reflection backdoor cannot be considered as a reasonable alternative.

I don't think copy-pasting library internals is a good way of making things done either.

Pzixel avatar Aug 29 '19 14:08 Pzixel

I also think that making JsonSerializationException.Create public is better that copy-pasting code. It also ensures that custom JsonConverters populate JsonSerializationExceptions the same way as built-in ones.

lostincomputer avatar Oct 18 '21 05:10 lostincomputer

+1 on making JsonSerializationException.Create public. For the same reason @lostincomputer mentioned in wanting to create a consistent experience with our team's custom JsonConverters

forteddyt avatar Mar 11 '22 21:03 forteddyt