Skip to content

Commit

Permalink
Adding NullValueHandling setting to Json serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
just3 committed Oct 2, 2018
1 parent 6422c3d commit 71870e9
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Memstate.JsonNet/JsonSerializerAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace Memstate.JsonNet
{
Expand All @@ -21,7 +21,8 @@ public JsonSerializerAdapter()
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
MissingMemberHandling = MissingMemberHandling.Ignore,
CheckAdditionalContent = false
CheckAdditionalContent = false,
NullValueHandling = NullValueHandling.Ignore // Omitting this line causes failure

This comment has been minimized.

Copy link
@Simcon

Simcon Oct 2, 2018

Owner

Adding NullValueHandling setting passes the test

};

_serializer = JsonSerializer.Create(settings);
Expand Down Expand Up @@ -52,7 +53,7 @@ public IEnumerable<T> ReadObjects<T>(Stream stream)
public void WriteObject(Stream serializationStream, object @object)
{
var streamWriter = new StreamWriter(serializationStream);

var writer = new JsonTextWriter(streamWriter);

if (@object is JournalRecord[])
Expand All @@ -68,7 +69,7 @@ public void WriteObject(Stream serializationStream, object @object)
_serializer.Serialize(writer, @object);
streamWriter.WriteLine();
}

writer.Flush();
streamWriter.Flush();
}
Expand Down
1 change: 1 addition & 0 deletions src/Memstate.Test/Memstate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Remove="AssemblyInfo.cs" />
Expand Down
153 changes: 153 additions & 0 deletions src/Memstate.Test/OneOrMany{T}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
namespace Schema.NET
{
using System;
using System.Collections;
using System.Collections.Generic;

public interface IValue
{
/// <summary>
/// Gets the non-null object representing the instance.
/// </summary>
object Value { get; }
}

/// <summary>
/// A single or list of values.
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <seealso cref="ICollection{T}" />
public struct OneOrMany<T> : IEnumerable<T>, IValue
{
private readonly List<T> collection;
private readonly T item;

/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="item">The single item value.</param>
public OneOrMany(T item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}

this.collection = null;
this.item = item;
}

/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="collection">The collection of values.</param>
public OneOrMany(IEnumerable<T> collection)
: this(collection == null ? null : new List<T>(collection))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="OneOrMany{T}"/> struct.
/// </summary>
/// <param name="list">The list of values.</param>
public OneOrMany(List<T> list)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}

if (list.Count == 1)
{
this.collection = null;
this.item = list[0];
}
else
{
this.collection = list;
this.item = default;
}
}

/// <summary>
/// Gets the number of elements contained in the <see cref="OneOrMany{T}"/>.
/// </summary>
public int Count
{
get
{
if (this.HasOne)
{
return 1;
}
else if (this.HasMany)
{
return this.collection.Count;
}

return 0;
}
}

/// <summary>
/// Gets the non-null object representing the instance.
/// </summary>
object IValue.Value
{
get
{
if (this.HasOne)
{
return this.item;
}

return this.collection;
}
}

/// <summary>
/// Gets a value indicating whether this instance has a single item value.
/// </summary>
/// <value><c>true</c> if this instance has a single item value; otherwise, <c>false</c>.</value>
public bool HasOne => this.item != null;

/// <summary>
/// Gets a value indicating whether this instance has more than one value.
/// </summary>
/// <value><c>true</c> if this instance has more than one value; otherwise, <c>false</c>.</value>
public bool HasMany => this.collection != null;

/// <summary>
/// Performs an implicit conversion from <typeparamref name="T"/> to <see cref="OneOrMany{T}"/>.
/// </summary>
/// <param name="item">The single item value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator OneOrMany<T>(T item) => new OneOrMany<T>(item);

/// <summary>
/// Performs an implicit conversion from <typeparamref name="T[]"/> to <see cref="OneOrMany{T}"/>.
/// </summary>
/// <param name="array">The array of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator OneOrMany<T>(T[] array) => new OneOrMany<T>(array);

/// <summary>
/// Performs an implicit conversion from <see cref="List{T}"/> to <see cref="OneOrMany{T}"/>.
/// </summary>
/// <param name="list">The list of values.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator OneOrMany<T>(List<T> list) => new OneOrMany<T>(list);

/// <summary>
/// Returns an enumerator that iterates through the <see cref="OneOrMany{T}"/>.
/// </summary>
/// <returns>An enumerator for the <see cref="OneOrMany{T}"/>.</returns>
public IEnumerator<T> GetEnumerator() => (this.collection ?? new List<T>() { this.item }).GetEnumerator();

/// <summary>
/// Returns an enumerator that iterates through the <see cref="OneOrMany{T}"/>.
/// </summary>
/// <returns>An enumerator for the <see cref="OneOrMany{T}"/>.</returns>
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
}
}
12 changes: 12 additions & 0 deletions src/Memstate.Test/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public void Can_serialize_poco_with_no_default_constructor(ISerializer serialize
Assert.AreEqual("homer", clone.Name);
}

[Test, TestCaseSource(nameof(Serializers))]
public void failing_poco_serialization(ISerializer serializer)
{
var poco = new FailingPoco { BirthDate = null };
var clone = serializer.Clone(poco);
Assert.AreEqual(null, clone.BirthDate);
}
}

internal class FailingPoco
{
public Schema.NET.OneOrMany<System.DateTimeOffset?>? BirthDate { get; set; }
}

internal class Poco {
Expand Down

0 comments on commit 71870e9

Please sign in to comment.