Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CSharpTypes/CSharpTypes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Saithe.NewtonsoftJson\Saithe.NewtonsoftJson.fsproj" />
<ProjectReference Include="..\Saithe.SystemTextJson\Saithe.SystemTextJson.fsproj" />
<ProjectReference Include="..\Saithe\Saithe.fsproj" />
</ItemGroup>
</Project>
52 changes: 11 additions & 41 deletions CSharpTypes/CustomerId.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
using Newtonsoft.Json;
using Saithe;
using System;
using System.ComponentModel;
using System;

namespace CSharpTypes
{
/// <summary>
/// Customer identifier, simple wrapper around long value. Since it wraps long we need to use the JsonConverter
/// </summary>
[JsonConverter(typeof(ValueTypeJsonConverter<CustomerId>))]
public struct CustomerId : IEquatable<CustomerId>
{
public readonly long Value;

public CustomerId(long value)
{
this.Value = value;
}
namespace CSharpTypes;

public readonly static CustomerId Empty = new CustomerId();

public bool Equals(CustomerId other)
{
if (ReferenceEquals(null, other)) return false;
return Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (obj is CustomerId)
return Equals((CustomerId)obj);
return false;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Value.ToString();
}
}
}
/// <summary>
/// Customer identifier, simple wrapper around long value. Since it wraps long we need to use the JsonConverter
/// </summary>
[Newtonsoft.Json.JsonConverter(typeof(Saithe.NewtonsoftJson.ValueTypeJsonConverter<CustomerId>)),
System.Text.Json.Serialization.JsonConverter(typeof(Saithe.SystemTextJson.ValueTypeLongJsonConverter<CustomerId>))]
public record struct CustomerId(long Value)
{
public override string ToString() => Value.ToString();
}
73 changes: 11 additions & 62 deletions CSharpTypes/OrderId.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,13 @@
using Newtonsoft.Json;
using Saithe;
using System;
using System;

namespace CSharpTypes
{
/// <summary>
/// Order identifier, simple wrapper around long value. Since it wraps long we need to use the JsonConverter
/// </summary>
[JsonConverter(typeof(ParseTypeJsonConverter<OrderId>))]
public struct OrderId : IEquatable<OrderId>
{
public readonly long Value;

public OrderId(long value)
{
this.Value = value;
}
namespace CSharpTypes;

public readonly static OrderId Empty = new OrderId();

public bool Equals(OrderId other)
{
if (ReferenceEquals(null, other)) return false;
return Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (obj is OrderId)
return Equals((CustomerId)obj);
return false;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Value.ToString();
}
public static bool TryParse(string str, out OrderId result)
{
result = Empty;
if (string.IsNullOrEmpty(str))
{
return false;
}
long res;
if (long.TryParse(str, out res))
{
result = new OrderId(res);
return true;
}
return false;
}
public static OrderId Parse(string str)
{
OrderId res;
if (TryParse(str, out res))
return res;
throw new Exception("Could not parse product id");
}
}
}
/// <summary>
/// Order identifier, simple wrapper around long value. Since it wraps long we need to use the JsonConverter
/// </summary>
[Newtonsoft.Json.JsonConverter(typeof(Saithe.NewtonsoftJson.ValueTypeJsonConverter<OrderId>)),
System.Text.Json.Serialization.JsonConverter(typeof(Saithe.SystemTextJson.ValueTypeLongJsonConverter<OrderId>))]
public record struct OrderId(long Value)
{
public override string ToString() => Value.ToString();
}
65 changes: 32 additions & 33 deletions CSharpTypes/ParseValueException.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
using System;

namespace CSharpTypes
namespace CSharpTypes;

[System.Serializable]
public class ParseValueException : FormatException
{
[System.Serializable]
public class ParseValueException : FormatException
/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
public ParseValueException()
{
/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
public ParseValueException()
{
}
}

/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
/// <param name="message">A <see cref="T:System.String"/> that describes the exception. </param>
public ParseValueException(string message) : base(message)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
/// <param name="message">A <see cref="T:System.String"/> that describes the exception. </param>
public ParseValueException(string message) : base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
/// <param name="message">A <see cref="T:System.String"/> that describes the exception. </param>
/// <param name="inner">The exception that is the cause of the current exception. </param>
public ParseValueException(string message, Exception inner) : base(message, inner)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
/// <param name="message">A <see cref="T:System.String"/> that describes the exception. </param>
/// <param name="inner">The exception that is the cause of the current exception. </param>
public ParseValueException(string message, Exception inner) : base(message, inner)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
/// <param name="context">The contextual information about the source or destination.</param>
/// <param name="info">The object that holds the serialized object data.</param>
protected ParseValueException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:MyException"/> class
/// </summary>
/// <param name="context">The contextual information about the source or destination.</param>
/// <param name="info">The object that holds the serialized object data.</param>
protected ParseValueException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
}
}
}
51 changes: 14 additions & 37 deletions CSharpTypes/ParseValueType.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
using System;
using System.ComponentModel;
using Saithe;
using System.ComponentModel;

namespace CSharpTypes
namespace CSharpTypes;

[TypeConverter(typeof(Saithe.ParseTypeConverter<ParseValueType>)),
Newtonsoft.Json.JsonConverter(typeof(Saithe.NewtonsoftJson.ParseTypeJsonConverter<ParseValueType>)),
System.Text.Json.Serialization.JsonConverter(typeof(Saithe.SystemTextJson.ParseTypeJsonConverter<ParseValueType>))]
public record ParseValueType (string Value)
{
[TypeConverter(typeof(ParseTypeConverter<ParseValueType>))]
public class ParseValueType : IEquatable<ParseValueType>
public static ParseValueType Parse(string value)
{
public readonly string Value;

public ParseValueType(string value)
{
Value = value;
}

public static ParseValueType Parse(string value)
{
var res = value.Split('_');
if (res.Length == 2 && res[0] == "P") return new ParseValueType(res[1]);
throw new ParseValueException($"Expected value to be in form: P_* but was '{value}'");
}

public override string ToString() => $"P_{Value}";

public override bool Equals(object obj)
{
return Value.Equals(obj as ParseValueType);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}

public bool Equals(ParseValueType other)
{
if (ReferenceEquals(null, other)) return false;
return Value.Equals(other.Value);
}
var res = value.Split('_');
if (res.Length == 2 && res[0] == "P") return new ParseValueType(res[1]);
throw new ParseValueException($"Expected value to be in form: P_* but was '{value}'");
}
}

public override string ToString() => $"P_{Value}";
}
73 changes: 21 additions & 52 deletions CSharpTypes/ProductId.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,33 @@
using System;
using System.ComponentModel;
using Saithe;

namespace CSharpTypes
{
[TypeConverter(typeof(ParseTypeConverter<ProductId>))]
public struct ProductId: IEquatable<ProductId>
{
public readonly long Value;
namespace CSharpTypes;

public ProductId(long value)
{
this.Value = value;
}
[TypeConverter(typeof(Saithe.ParseTypeConverter<ProductId>)),
Newtonsoft.Json.JsonConverter(typeof(Saithe.NewtonsoftJson.ParseTypeJsonConverter<ProductId>)),
System.Text.Json.Serialization.JsonConverter(typeof(Saithe.SystemTextJson.ParseTypeJsonConverter<ProductId>))]
public record struct ProductId (long Value)
{
public readonly static ProductId Empty = new ProductId();

public readonly static ProductId Empty = new ProductId();
public override string ToString() => $"ProductId/{Value}";

public bool Equals(ProductId other)
{
if (ReferenceEquals(null, other)) return false;
return Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (obj is ProductId)
return Equals((ProductId)obj);
return false;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return $"ProductId/{Value}";
}
public static bool TryParse(string str, out ProductId result)
public static bool TryParse(string str, out ProductId result)
{
result = Empty;
if (string.IsNullOrEmpty(str))
{
result = Empty;
if (string.IsNullOrEmpty(str))
{
return false;
}
var split = str.Split('/');
long res;
if (split.Length == 2
&& split[0] == "ProductId"
&& long.TryParse(split[1], out res))
{
result = new ProductId(res);
return true;
}
return false;
}
public static ProductId Parse(string str)
var split = str.Split('/');
if (split.Length == 2
&& split[0] == "ProductId"
&& long.TryParse(split[1], out var res))
{
ProductId res;
if (TryParse(str, out res))
return res;
throw new Exception("Could not parse product id");
result = new ProductId(res);
return true;
}
return false;
}
}
public static ProductId Parse(string str) => TryParse(str, out var res) ? res : throw new Exception("Could not parse product id");
}
24 changes: 24 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project ToolsVersion="15.0">

<PropertyGroup>
<Authors>wallymathieu</Authors>
<Company></Company>
<Product>Saithe</Product>
<Description>Serializing value types into json</Description>
<PackageLicenseUrl>http://opensource.org/licenses/MIT</PackageLicenseUrl>
<VersionPrefix>3.0.0</VersionPrefix>
<VersionSuffix>alpha-1</VersionSuffix>

<Version Condition=" '$(VersionSuffix)' != '' ">$(VersionPrefix)-$(VersionSuffix)</Version>
<Version Condition=" '$(VersionSuffix)' == '' ">$(VersionPrefix)</Version>
<AssemblyVersion>$(VersionPrefix).0</AssemblyVersion>
<FileVersion>$(VersionPrefix).0</FileVersion>

<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageProjectUrl>https://github.com/NewtonsoftJsonExt/Saithe/</PackageProjectUrl>
<RepositoryUrl>https://github.com/NewtonsoftJsonExt/Saithe/</RepositoryUrl>

<RepositoryType>git</RepositoryType>
</PropertyGroup>

</Project>
Loading