launchdarkly / dotnet-sdk-common

Code shared between dotnet-server-sdk and dotnet-client-sdk
Other
3 stars 10 forks source link

How to serialize using System.Text.Json #64

Closed isc30 closed 2 years ago

isc30 commented 2 years ago

Hi, I have a codebase that is getting rid of Newtonsoft.Json and I need assistance on how to serialize LD objects properly with System.Text.Json

eli-darkly commented 2 years ago

Hi. This depends on what LaunchDarkly SDK you're using, and what version of it, and on your application's target framework.

If you're using version 6.0 or higher of our server-side .NET SDK, and if you are using any .NET runtime except .NET Framework 4.5.x, then this should just work. As described here, LaunchDarkly types like User and LdValue already have the necessary attributes for System.Text.Json to understand how to serialize them automatically. The same is true for version 2.0 or higher of our client-side .NET SDK (I can't tell which SDK you might be referring to, because you posted this issue on the "LaunchDarkly.Common" package that is used by both).

If you're using an older version of either one— which isn't advisable, because the last 5.x version of the server-side .NET SDK will be out of support 3 months from now— then things are more difficult, because the older versions only supported automatic serialization by Newtonsoft.Json (and, since those older versions had Newtonsoft.Json as a dependency, they would make it impossible for your application to completely get rid of that library). I can think of an inconvenient workaround but I'd rather wait on that part till I know the answers to those first questions.

eli-darkly commented 2 years ago

Also, it's unclear to me whether you have already tried something and found that it didn't work, in which case I would need to know what you did and what you saw.

isc30 commented 2 years ago

Hi, thanks for the clarifications, I managed to make it work by updating the sdk version and using the common one instead of the newtonsoft one, thanks!

I'm using net461 as a targetframework

eli-darkly commented 2 years ago

Sorry, what do you mean by "using the common one instead of the newtonsoft one"? I mean, I'm glad you got it to work, but I want to make sure you're not doing something unnecessary— you would not normally need to reference LaunchDarkly.CommonSdk directly, it is pulled in automatically as a dependency of the SDK package.