-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented configuration optoins for serialization. This closes #10
- Loading branch information
1 parent
95e104a
commit 3f55453
Showing
11 changed files
with
441 additions
and
41 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
src/ExpressionPowerTools.Serialization/Configuration/ConfigurationBuilder.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,79 @@ | ||
// Copyright (c) Jeremy Likness. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the repository root for license information. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using ExpressionPowerTools.Serialization.Serializers; | ||
using ExpressionPowerTools.Serialization.Signatures; | ||
|
||
namespace ExpressionPowerTools.Serialization.Configuration | ||
{ | ||
/// <summary> | ||
/// Configuration builder for wiring up configuration fluently. | ||
/// </summary> | ||
public class ConfigurationBuilder : IConfigurationBuilder | ||
{ | ||
/// <summary> | ||
/// The <see cref="SerializationState"/> to build up. | ||
/// </summary> | ||
private readonly SerializationState state = new SerializationState | ||
{ | ||
Options = new JsonSerializerOptions | ||
{ | ||
IgnoreNullValues = true, | ||
IgnoreReadOnlyProperties = true, | ||
}, | ||
CompressTypes = true, | ||
}; | ||
|
||
/// <summary> | ||
/// Once <see cref="Configure"/> is called, this instance is done. | ||
/// </summary> | ||
private bool isValid = true; | ||
|
||
/// <summary> | ||
/// Configure the type compression. | ||
/// </summary> | ||
/// <param name="compressTypes">The flag indicating whether or not to compress types.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public IConfigurationBuilder CompressTypes(bool compressTypes) | ||
{ | ||
CheckValidity(); | ||
state.CompressTypes = compressTypes; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configuration complete. Return the <see cref="SerializationState"/>. | ||
/// </summary> | ||
/// <returns>The <see cref="SerializationState"/>.</returns> | ||
public SerializationState Configure() | ||
{ | ||
isValid = false; | ||
return state; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the <see cref="JsonSerializerOptions"/>. | ||
/// </summary> | ||
/// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param> | ||
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns> | ||
public IConfigurationBuilder WithJsonSerializerOptions(JsonSerializerOptions options) | ||
{ | ||
state.Options = options; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Checks whether configuration is still valid. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">Thrown when already configured.</exception> | ||
private void CheckValidity() | ||
{ | ||
if (!isValid) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/ExpressionPowerTools.Serialization/Configuration/DefaultConfiguration.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,70 @@ | ||
// Copyright (c) Jeremy Likness. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the repository root for license information. | ||
|
||
using System; | ||
using ExpressionPowerTools.Core.Dependencies; | ||
using ExpressionPowerTools.Serialization.Serializers; | ||
using ExpressionPowerTools.Serialization.Signatures; | ||
|
||
namespace ExpressionPowerTools.Serialization.Configuration | ||
{ | ||
/// <summary> | ||
/// The default configuration used when none is explicitly provided. | ||
/// </summary> | ||
public class DefaultConfiguration : IDefaultConfiguration | ||
{ | ||
/// <summary> | ||
/// Synchronization. | ||
/// </summary> | ||
private readonly object lockObj = new object(); | ||
|
||
/// <summary> | ||
/// Holds the default state. | ||
/// </summary> | ||
private SerializationState defaultState; | ||
|
||
/// <summary> | ||
/// Gets a new <see cref="SerializationState"/> with default options. | ||
/// </summary> | ||
/// <returns>The <see cref="SerializationState"/>.</returns> | ||
public SerializationState GetDefaultState() | ||
{ | ||
if (defaultState == null) | ||
{ | ||
lock (lockObj) | ||
{ | ||
if (defaultState == null) | ||
{ | ||
defaultState = ServiceHost.GetService<IConfigurationBuilder>().Configure(); | ||
} | ||
} | ||
} | ||
|
||
SerializationState state; | ||
|
||
lock (lockObj) | ||
{ | ||
state = new SerializationState | ||
{ | ||
CompressTypes = defaultState.CompressTypes, | ||
Options = defaultState.Options, | ||
}; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the configuration builder to configure the default state. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param> | ||
public void SetDefaultState(Func<IConfigurationBuilder, SerializationState> builder) | ||
{ | ||
var state = builder(ServiceHost.GetService<IConfigurationBuilder>()); | ||
lock (lockObj) | ||
{ | ||
defaultState = state; | ||
} | ||
} | ||
} | ||
} |
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.