Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Decimal instruction. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Jun 18, 2019
1 parent f8d9323 commit a302821
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Super.Serialization.Testing.Application/DecimalInstructionTests.cs
@@ -0,0 +1,31 @@
using FluentAssertions;
using System.Text.Json.Serialization;
using Xunit;

namespace Super.Serialization.Testing.Application
{
public sealed class DecimalInstructionTests
{
[Fact]
void Verify()
{
const decimal value = -12.34567m;
Writer.Default.Get(value)
.Open()
.Should()
.Equal(JsonSerializer.ToUtf8Bytes(value));
}

sealed class Writer : SingleInstructionWriter<decimal>
{
public static Writer Default { get; } = new Writer();

Writer() : base(DecimalInstruction.Default) {}
}

public class Benchmarks : Benchmark<decimal>
{
public Benchmarks() : base(DecimalInstruction.Default, -12.34567m) {}
}
}
}
2 changes: 1 addition & 1 deletion Super.Serialization.Testing.Application/Program.cs
Expand Up @@ -7,7 +7,7 @@ public class Program
static void Main(params string[] arguments)
{
Configuration.Default.Get(arguments)
.To(Run.A<DoubleInstructionTests.Benchmarks>);
.To(Run.A<DecimalInstructionTests.Benchmarks>);
}
}
}
16 changes: 16 additions & 0 deletions Super.Serialization/IInstruction.cs
Expand Up @@ -315,4 +315,20 @@ public uint Get(Composition<double> parameter)

public uint Get(double parameter) => 128;
}

sealed class DecimalInstruction : IInstruction<decimal>
{
public static DecimalInstruction Default { get; } = new DecimalInstruction();

DecimalInstruction() {}

public uint Get(Composition<decimal> parameter)
=> Utf8Formatter.TryFormat(parameter.Instance, parameter.Output.AsSpan((int)parameter.Index),
out var count)
? (uint)count
: throw new
InvalidOperationException($"Could not format '{parameter.Instance}' into its UTF-8 equivalent.");

public uint Get(decimal parameter) => 31;
}
}

0 comments on commit a302821

Please sign in to comment.