-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from SparkyTD/function-serializer
Add custom JsonConverters to ensure that JS function strings are evaluated into function when deserialized by JS
- Loading branch information
Showing
5 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/Blazor-ApexCharts/Internal/Converters/FunctionStringConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ApexCharts.Internal; | ||
|
||
/// <summary> | ||
/// Ensures that JS functions are serialized with the key '@eval' so they can be appropriately evaluated on the client side | ||
/// Example: | ||
/// <code> | ||
/// myFunction: { | ||
/// "@eval": "function(value) { return value; }" | ||
/// } | ||
/// </code> | ||
/// </summary> | ||
internal class FunctionStringConverter : JsonConverter<string> | ||
{ | ||
public override bool CanConvert(Type typeToConvert) => true; | ||
|
||
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("@eval"); | ||
writer.WriteStringValue(value); | ||
writer.WriteEndObject(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Blazor-ApexCharts/Internal/Converters/FunctionValueOrListConverterConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ApexCharts.Internal; | ||
|
||
/// <summary> | ||
/// Ensures that JS function arrays are serialized with the key '@eval' so they can be appropriately evaluated on the client side | ||
/// Example: | ||
/// <code> | ||
/// myFunction: { | ||
/// "@eval": [ "function(value) { return value; }" ] | ||
/// } | ||
/// </code> | ||
/// </summary> | ||
internal class FunctionValueOrListConverterConverter : JsonConverter<CustomFunction> | ||
{ | ||
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(CustomFunction); | ||
|
||
public override CustomFunction Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, CustomFunction value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("@eval"); | ||
if (value == null || value.Count == 0) | ||
JsonSerializer.Serialize(writer, null, options); | ||
else if (value.Count == 1) | ||
JsonSerializer.Serialize(writer, value[0], options); | ||
else | ||
JsonSerializer.Serialize<List<string>>(writer, value, options); | ||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters