-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STJ: support custom converters for view models
- Loading branch information
Showing
15 changed files
with
248 additions
and
194 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
47 changes: 47 additions & 0 deletions
47
src/Framework/Framework/ViewModel/Serialization/DotvvmJsonOptionsProvider.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,47 @@ | ||
using System; | ||
using System.Text.Json; | ||
using DotVVM.Framework.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace DotVVM.Framework.ViewModel.Serialization; | ||
|
||
/// <summary> Creates and provides System.Text.Json serialization options for ViewModel serialization </summary> | ||
public interface IDotvvmJsonOptionsProvider | ||
{ | ||
/// <summary> Options used for view model serialization, includes the <see cref="ViewModelJsonConverter" /> </summary> | ||
JsonSerializerOptions ViewModelJsonOptions { get; } | ||
/// <summary> Options used for serialization of other objects like the ModelState in the invalid VM response. </summary> | ||
JsonSerializerOptions PlainJsonOptions { get; } | ||
|
||
/// <summary> The the main converter used for viewmodel serialization and deserialization (in initial requests and commands) </summary> | ||
IDotvvmJsonConverter GetRootViewModelConverter(Type type); | ||
} | ||
|
||
|
||
public class DotvvmJsonOptionsProvider : IDotvvmJsonOptionsProvider | ||
{ | ||
private Lazy<JsonSerializerOptions> _viewModelOptions; | ||
public JsonSerializerOptions ViewModelJsonOptions => _viewModelOptions.Value; | ||
private Lazy<JsonSerializerOptions> _plainJsonOptions; | ||
public JsonSerializerOptions PlainJsonOptions => _plainJsonOptions.Value; | ||
|
||
private Lazy<ViewModelJsonConverter> _viewModelConverter; | ||
|
||
public DotvvmJsonOptionsProvider(DotvvmConfiguration configuration) | ||
{ | ||
var debug = configuration.Debug; | ||
_viewModelConverter = new Lazy<ViewModelJsonConverter>(() => configuration.ServiceProvider.GetRequiredService<ViewModelJsonConverter>()); | ||
_viewModelOptions = new Lazy<JsonSerializerOptions>(() => | ||
new JsonSerializerOptions(DefaultSerializerSettingsProvider.Instance.SettingsHtmlUnsafe) { | ||
Converters = { _viewModelConverter.Value }, | ||
WriteIndented = debug | ||
} | ||
); | ||
_plainJsonOptions = new Lazy<JsonSerializerOptions>(() => | ||
!debug ? DefaultSerializerSettingsProvider.Instance.SettingsHtmlUnsafe | ||
: new JsonSerializerOptions(DefaultSerializerSettingsProvider.Instance.SettingsHtmlUnsafe) { WriteIndented = true } | ||
); | ||
} | ||
|
||
public IDotvvmJsonConverter GetRootViewModelConverter(Type type) => _viewModelConverter.Value.GetDotvvmConverter(type); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Framework/Framework/ViewModel/Serialization/IDotvvmJsonConverter.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,22 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace DotVVM.Framework.ViewModel.Serialization; | ||
|
||
/// <summary> System.Text.Json converter which supports population of existing objects. Implementations of this interface are also expected to implement <see cref="IDotvvmJsonConverter{T}" /> and inherit from <see cref="JsonConverter{T}" /> </summary> | ||
public interface IDotvvmJsonConverter | ||
{ | ||
public object? ReadUntyped(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, DotvvmSerializationState state); | ||
public object? PopulateUntyped(ref Utf8JsonReader reader, Type typeToConvert, object? value, JsonSerializerOptions options, DotvvmSerializationState state); | ||
public void WriteUntyped(Utf8JsonWriter writer, object? value, JsonSerializerOptions options, DotvvmSerializationState state, bool requireTypeField = true, bool wrapObject = true); | ||
} | ||
|
||
/// <summary> System.Text.Json converter which supports population of existing objects. </summary> | ||
public interface IDotvvmJsonConverter<T>: IDotvvmJsonConverter | ||
{ | ||
public T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, DotvvmSerializationState state); | ||
public T Populate(ref Utf8JsonReader reader, Type typeToConvert, T value, JsonSerializerOptions options, DotvvmSerializationState state); | ||
public void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options, DotvvmSerializationState state, bool requireTypeField = true, bool wrapObject = true); | ||
} | ||
|
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
Oops, something went wrong.