Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/shared/common/src/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 21 additions & 3 deletions pkgs/shared/common/src/Json/LdJsonConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigSegmentsStatus>()
#else
Enum.GetValues(typeof(BigSegmentsStatus))
#endif
)
{
if (ToIdentifier(k) == value)
{
Expand Down Expand Up @@ -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<EvaluationErrorKind>()
#else
Enum.GetValues(typeof(EvaluationErrorKind))
#endif
)
{
if (ToIdentifier(k) == value)
{
Expand Down Expand Up @@ -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<EvaluationReasonKind>()
#else
Enum.GetValues(typeof(EvaluationErrorKind))
#endif
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Incorrect Enum Type in Conditional Compilation Branch

The EvaluationReasonKindConverter.FromIdentifier method's #else branch for older .NET versions incorrectly calls Enum.GetValues(typeof(EvaluationErrorKind)) instead of typeof(EvaluationReasonKind). This causes the method to iterate over the wrong enum, leading to incorrect value matching and exceptions on those platforms.

Fix in Cursor Fix in Web

{
if (ToIdentifier(k) == value)
{
Expand Down
19 changes: 19 additions & 0 deletions pkgs/shared/common/src/Json/LdJsonSerialization.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Text.Json;
#if NET7_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace LaunchDarkly.Sdk.Json
{
Expand All @@ -12,6 +15,10 @@ namespace LaunchDarkly.Sdk.Json
/// </remarks>
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
/// <summary>
/// Converts an object to its JSON representation.
/// </summary>
Expand All @@ -23,6 +30,10 @@ public static class LdJsonSerialization
/// <typeparam name="T">type of the object being serialized</typeparam>
/// <param name="instance">the instance to serialize</param>
/// <returns>the object's JSON encoding as a string</returns>
#if NET7_0_OR_GREATER
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
#endif
public static string SerializeObject<T>(T instance) where T : IJsonSerializable =>
JsonSerializer.Serialize(instance);

Expand All @@ -37,6 +48,10 @@ public static string SerializeObject<T>(T instance) where T : IJsonSerializable
/// <typeparam name="T">type of the object being serialized</typeparam>
/// <param name="instance">the instance to serialize</param>
/// <returns>the object's JSON encoding as a byte array</returns>
#if NET7_0_OR_GREATER
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
#endif
public static byte[] SerializeObjectToUtf8Bytes<T>(T instance) where T : IJsonSerializable =>
JsonSerializer.SerializeToUtf8Bytes(instance);

Expand All @@ -52,6 +67,10 @@ public static byte[] SerializeObjectToUtf8Bytes<T>(T instance) where T : IJsonSe
/// <param name="json">the object's JSON encoding as a string</param>
/// <returns>the deserialized instance</returns>
/// <exception cref="JsonException">if the JSON encoding was invalid</exception>
#if NET7_0_OR_GREATER
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)]
#endif
public static T DeserializeObject<T>(string json) where T : IJsonSerializable =>
JsonSerializer.Deserialize<T>(json);
}
Expand Down
13 changes: 13 additions & 0 deletions pkgs/shared/common/src/Json/LdJsonSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions pkgs/shared/common/src/LaunchDarkly.CommonSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<AssemblyName>LaunchDarkly.CommonSdk</AssemblyName>
<OutputType>Library</OutputType>
<LangVersion>7.3</LangVersion>
<!-- the system.text.json source generator require version 9.0 or greater -->
<LangVersion Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">9.0</LangVersion>
<PackageId>LaunchDarkly.CommonSdk</PackageId>
<Description>LaunchDarkly common code for .NET and Xamarin clients</Description>
<Company>LaunchDarkly</Company>
Expand All @@ -34,6 +36,7 @@

<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\LaunchDarkly.CommonSdk.xml</DocumentationFile>
<RootNamespace>LaunchDarkly.Sdk</RootNamespace>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion pkgs/shared/common/src/LdValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ public static ObjectBuilder BuildObject() =>
/// <exception cref="JsonException">if the string could not be parsed as JSON</exception>
/// <see cref="ToJsonString"/>
public static LdValue Parse(string jsonString) =>
#if NET7_0_OR_GREATER
JsonSerializer.Deserialize(jsonString, LdJsonSerializerContext.Default.LdValue);
#else
LdJsonSerialization.DeserializeObject<LdValue>(jsonString);

#endif
#endregion

#region Public properties
Expand Down Expand Up @@ -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
}
}

Expand Down
Loading