Skip to content

Commit

Permalink
Implemented configuration optoins for serialization. This closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyLikness committed Sep 4, 2020
1 parent 95e104a commit 3f55453
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 41 deletions.
16 changes: 15 additions & 1 deletion Releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@

[Back to README](./README.md)

- [0.8 Alpha](#08_Alpha)
- [0.8 Alpha](#08-Alpha)

## 0.8 Alpha

- [0.8.6-alpha](#086-alpha)
- [0.8.5-alpha](#085-alpha)
- [0.8.4-alpha](#084-alpha)
- [0.8.3-alpha](#083-alpha)
- [0.8.2-alpha](#082-alpha)
- [0.8.1-alpha](#081-alpha)
- [0.8.0-alpha](#080-alpha)

### 0.8.6-alpha

**_Breaking Change_**

An overload of the [`Serializer`](./docs/api/ExpressionPowerTools.Serialization.Serializer.cs.md) was changed. The non-typed `Deserialize` requires a
`IQueryHost` to understand the type being deserialized, but the typed overload can default to LINQ-to-objects.

**Serialization**

- Added [`ConfigurationBuilder`](./docs/api/ExpressionPowerTools.Serialization.Configuration.ConfigurationBuilder.cs.md) as the
optimal way to build configurations.
- Added [`DefaultConfiguration`](./docs/api/ExpressionPowerTools.Serialization.Configuration.DefaultConfiguration.cs.md) to
allow default configurations.

### 0.8.5-alpha

Expand Down
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();
}
}
}
}
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;
}
}
}
}
3 changes: 3 additions & 0 deletions src/ExpressionPowerTools.Serialization/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE in the repository root for license information.

using ExpressionPowerTools.Core.Signatures;
using ExpressionPowerTools.Serialization.Configuration;
using ExpressionPowerTools.Serialization.Serializers;
using ExpressionPowerTools.Serialization.Signatures;

Expand All @@ -20,6 +21,8 @@ public void RegisterDefaultServices(IServiceRegistration registration)
{
registration.RegisterSingleton<IReflectionHelper>(
new ReflectionHelper());
registration.Register<IConfigurationBuilder, ConfigurationBuilder>();
registration.RegisterSingleton<IDefaultConfiguration>(new DefaultConfiguration());
}
}
}
Loading

0 comments on commit 3f55453

Please sign in to comment.