Skip to content

Commit

Permalink
Allow initial System.Text.Json.JsonSerializerOptions to be a copy fro…
Browse files Browse the repository at this point in the history
…m existing options
  • Loading branch information
CptWesley authored and jeremydmiller committed May 16, 2024
1 parent 2bd9c19 commit d1ef0fd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/storeoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,5 @@ var store = DocumentStore.For(_ =>
_.NameDataLength = 100;
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L313-L322' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_setting-name-data-length' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L314-L323' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_setting-name-data-length' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/schema/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var store = DocumentStore.For(opts =>
opts.AutoCreateSchemaObjects = AutoCreate.None;
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L46-L72' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_autocreateschemaobjects' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L47-L73' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_autocreateschemaobjects' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

To prevent unnecessary loss of data, even in development, on the first usage of a document type, Marten will:
Expand Down
2 changes: 1 addition & 1 deletion docs/schema/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var store = DocumentStore.For(opts =>
opts.AutoCreateSchemaObjects = AutoCreate.None;
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L46-L72' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_autocreateschemaobjects' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L47-L73' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_autocreateschemaobjects' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

As long as you're using a permissive auto creation mode (i.e., not _None_), you should be able to code in your application model
Expand Down
34 changes: 33 additions & 1 deletion src/CoreTests/StoreOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Linq;
using System.Text.Json;
using JasperFx.CodeGeneration;
using Marten;
using Marten.Services;
Expand Down Expand Up @@ -392,6 +393,37 @@ public void SettingCustomDataSourceFactory_ShouldSetTenancyIfItsNotDefinedYet()
options.Tenancy.ShouldBeOfType<DefaultTenancy>();
}

[InlineData(true)]
[InlineData(false)]
[Theory]
public void use_base_system_text_json_serialization_options(bool indented)
{
// Given
var options = new StoreOptions();

// When
options.UseSystemTextJsonForSerialization(new JsonSerializerOptions
{
WriteIndented = indented,
});

// Then
var json = options.Serializer().ToJson(new
{
Field1 = 10,
Field2 = 20,
});

if (indented)
{
json.ShouldContain('\n');
}
else
{
json.ShouldNotContain('\n');
}
}


private class DummyNpgsqlDataSourceFactory: INpgsqlDataSourceFactory
{
Expand Down
21 changes: 15 additions & 6 deletions src/Marten/Services/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,29 @@ namespace Marten.Services;
/// </summary>
public class SystemTextJsonSerializer: ISerializer
{
private readonly JsonSerializerOptions _clean = new();
private readonly JsonSerializerOptions _clean;
private readonly JsonSerializerOptions _options;
private readonly JsonSerializerOptions _optionsDeserialize;
private readonly JsonSerializerOptions _withTypes;

private readonly JsonSerializerOptions _options = new();

private readonly JsonSerializerOptions _optionsDeserialize = new();

private readonly JsonSerializerOptions _withTypes = new();
private Casing _casing = Casing.Default;
private EnumStorage _enumStorage = EnumStorage.AsInteger;

private JsonDocumentOptions _optionsJsonDocumentDeserialize;

public SystemTextJsonSerializer()
: this(null)
{
}

public SystemTextJsonSerializer(JsonSerializerOptions? options)
{
options ??= new();
_clean = new(options);
_options = new(options);
_optionsDeserialize = new(options);
_withTypes = new(options);

_optionsDeserialize.Converters.Add(new SystemObjectNewtonsoftCompatibleConverter());

_optionsDeserialize.PropertyNamingPolicy =
Expand Down
17 changes: 15 additions & 2 deletions src/Marten/StoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,23 @@ public void Serializer(ISerializer serializer)
EnumStorage enumStorage = EnumStorage.AsInteger,
Casing casing = Casing.Default,
Action<JsonSerializerOptions>? configure = null)
=> UseSystemTextJsonForSerialization(null, enumStorage, casing, configure);

/// <summary>
/// Configure the System.Text.Json serializer settings
/// </summary>
/// <param name="options">The base settings.</param>
/// <param name="enumStorage">Enum storage style</param>
/// <param name="casing">Casing style to be used in serialization</param>
public void UseSystemTextJsonForSerialization(
JsonSerializerOptions? options,
EnumStorage enumStorage = EnumStorage.AsInteger,
Casing casing = Casing.Default,
Action<JsonSerializerOptions>? configure = null)
{
var serializer = new SystemTextJsonSerializer() { EnumStorage = enumStorage, Casing = casing, };
var serializer = new SystemTextJsonSerializer(options) { EnumStorage = enumStorage, Casing = casing, };

if(configure is not null)
if (configure is not null)
serializer.Configure(configure);

Serializer(serializer);
Expand Down

0 comments on commit d1ef0fd

Please sign in to comment.