diff --git a/pkgs/shared/common/src/Context.cs b/pkgs/shared/common/src/Context.cs index 0d48a871..87edafce 100644 --- a/pkgs/shared/common/src/Context.cs +++ b/pkgs/shared/common/src/Context.cs @@ -922,7 +922,11 @@ public override string ToString() { return "(invalid Context: " + Error + ")"; } +#if NET7_0_OR_GREATER + return System.Text.Json.JsonSerializer.Serialize(this, LdJsonSerializerContext.Default.Context); +#else return LdJsonSerialization.SerializeObject(this); +#endif } private LdValue GetTopLevelAddressableAttributeSingleKind(string name) diff --git a/pkgs/shared/common/src/Json/LdJsonConverters.cs b/pkgs/shared/common/src/Json/LdJsonConverters.cs index 8c336fac..228e7b55 100644 --- a/pkgs/shared/common/src/Json/LdJsonConverters.cs +++ b/pkgs/shared/common/src/Json/LdJsonConverters.cs @@ -196,7 +196,13 @@ public static void WriteJsonValue(BigSegmentsStatus instance, Utf8JsonWriter wri internal static BigSegmentsStatus FromIdentifier(string value) { - foreach (BigSegmentsStatus k in Enum.GetValues(typeof(BigSegmentsStatus))) + foreach (BigSegmentsStatus k in +#if NET5_0_OR_GREATER + Enum.GetValues() +#else + Enum.GetValues(typeof(BigSegmentsStatus)) +#endif + ) { if (ToIdentifier(k) == value) { @@ -243,7 +249,13 @@ public static void WriteJsonValue(EvaluationErrorKind instance, Utf8JsonWriter w internal static EvaluationErrorKind FromIdentifier(string value) { - foreach (EvaluationErrorKind k in Enum.GetValues(typeof(EvaluationErrorKind))) + foreach (EvaluationErrorKind k in +#if NET5_0_OR_GREATER + Enum.GetValues() +#else + Enum.GetValues(typeof(EvaluationErrorKind)) +#endif + ) { if (ToIdentifier(k) == value) { @@ -294,7 +306,13 @@ public void WriteJsonValue(EvaluationReasonKind instance, Utf8JsonWriter writer) internal static EvaluationReasonKind FromIdentifier(string value) { - foreach (EvaluationReasonKind k in Enum.GetValues(typeof(EvaluationErrorKind))) + foreach (EvaluationReasonKind k in +#if NET5_0_OR_GREATER + Enum.GetValues() +#else + Enum.GetValues(typeof(EvaluationReasonKind)) +#endif + ) { if (ToIdentifier(k) == value) { diff --git a/pkgs/shared/common/src/Json/LdJsonSerialization.cs b/pkgs/shared/common/src/Json/LdJsonSerialization.cs index 4c126b9f..bc51e7ca 100644 --- a/pkgs/shared/common/src/Json/LdJsonSerialization.cs +++ b/pkgs/shared/common/src/Json/LdJsonSerialization.cs @@ -1,5 +1,8 @@ using System; using System.Text.Json; +#if NET7_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +#endif namespace LaunchDarkly.Sdk.Json { @@ -12,6 +15,10 @@ namespace LaunchDarkly.Sdk.Json /// public static class LdJsonSerialization { +#if NET7_0_OR_GREATER + internal const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved."; + internal const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications."; +#endif /// /// Converts an object to its JSON representation. /// @@ -23,6 +30,10 @@ public static class LdJsonSerialization /// type of the object being serialized /// the instance to serialize /// the object's JSON encoding as a string +#if NET7_0_OR_GREATER + [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] + [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] +#endif public static string SerializeObject(T instance) where T : IJsonSerializable => JsonSerializer.Serialize(instance); @@ -37,6 +48,10 @@ public static string SerializeObject(T instance) where T : IJsonSerializable /// type of the object being serialized /// the instance to serialize /// the object's JSON encoding as a byte array +#if NET7_0_OR_GREATER + [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] + [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] +#endif public static byte[] SerializeObjectToUtf8Bytes(T instance) where T : IJsonSerializable => JsonSerializer.SerializeToUtf8Bytes(instance); @@ -52,6 +67,10 @@ public static byte[] SerializeObjectToUtf8Bytes(T instance) where T : IJsonSe /// the object's JSON encoding as a string /// the deserialized instance /// if the JSON encoding was invalid +#if NET7_0_OR_GREATER + [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] + [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] +#endif public static T DeserializeObject(string json) where T : IJsonSerializable => JsonSerializer.Deserialize(json); } diff --git a/pkgs/shared/common/src/Json/LdJsonSerializerContext.cs b/pkgs/shared/common/src/Json/LdJsonSerializerContext.cs new file mode 100644 index 00000000..78bfc053 --- /dev/null +++ b/pkgs/shared/common/src/Json/LdJsonSerializerContext.cs @@ -0,0 +1,13 @@ +#if NET7_0_OR_GREATER +using System.Text.Json.Serialization; + +namespace LaunchDarkly.Sdk.Json +{ + [JsonSerializable(typeof(LdValue))] + [JsonSerializable(typeof(Context))] + internal partial class LdJsonSerializerContext : JsonSerializerContext + { + + } +} +#endif diff --git a/pkgs/shared/common/src/LaunchDarkly.CommonSdk.csproj b/pkgs/shared/common/src/LaunchDarkly.CommonSdk.csproj index a74fd8e4..ee731f5c 100644 --- a/pkgs/shared/common/src/LaunchDarkly.CommonSdk.csproj +++ b/pkgs/shared/common/src/LaunchDarkly.CommonSdk.csproj @@ -13,6 +13,8 @@ LaunchDarkly.CommonSdk Library 7.3 + + 9.0 LaunchDarkly.CommonSdk LaunchDarkly common code for .NET and Xamarin clients LaunchDarkly @@ -34,6 +36,7 @@ bin\$(Configuration)\$(TargetFramework)\LaunchDarkly.CommonSdk.xml LaunchDarkly.Sdk + true diff --git a/pkgs/shared/common/src/LdValue.cs b/pkgs/shared/common/src/LdValue.cs index a79cbf2a..f752abb5 100644 --- a/pkgs/shared/common/src/LdValue.cs +++ b/pkgs/shared/common/src/LdValue.cs @@ -282,8 +282,11 @@ public static ObjectBuilder BuildObject() => /// if the string could not be parsed as JSON /// public static LdValue Parse(string jsonString) => +#if NET7_0_OR_GREATER + JsonSerializer.Deserialize(jsonString, LdJsonSerializerContext.Default.LdValue); +#else LdJsonSerialization.DeserializeObject(jsonString); - +#endif #endregion #region Public properties @@ -550,7 +553,11 @@ public string ToJsonString() case LdValueType.Bool: return _boolValue ? "true" : "false"; default: +#if NET7_0_OR_GREATER + return JsonSerializer.Serialize(this, LdJsonSerializerContext.Default.LdValue); +#else return JsonSerializer.Serialize(this); +#endif } }