Skip to content

Commit

Permalink
Recode
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoFeiDu committed Dec 28, 2021
1 parent 2e784c3 commit 038b6cc
Show file tree
Hide file tree
Showing 53 changed files with 172 additions and 172 deletions.
6 changes: 3 additions & 3 deletions src/Zaabee.Binary/Binary.Helper.Bytes.FromBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static partial class BinaryHelper
[ObsoleteAttribute(@"BinaryFormatter serialization is obsolete and should not be used.
See https://aka.ms/binaryformatter for more information.")]
public static TValue? FromBytes<TValue>(byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: (TValue?)FromBytes(bytes);

Expand All @@ -24,8 +24,8 @@ public static partial class BinaryHelper
See https://aka.ms/binaryformatter for more information.")]
public static object? FromBytes(byte[]? bytes)
{
if (bytes.IsNullOrEmpty()) return default;
using var ms = new MemoryStream(bytes!);
if (bytes is null || bytes.Length is 0) return default;
using var ms = new MemoryStream(bytes);
return FromStream(ms);
}
}
2 changes: 1 addition & 1 deletion src/Zaabee.Binary/Binary.Helper.Bytes.ToBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public static partial class BinaryHelper
public static byte[] ToBytes(object? value) =>
value is null
? Array.Empty<byte>()
: ToStream(value).ReadToEnd();
: ToStream(value).ToArray();
}
8 changes: 4 additions & 4 deletions src/Zaabee.Binary/Binary.Helper.Stream.FromStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static partial class BinaryHelper
[ObsoleteAttribute(@"BinaryFormatter serialization is obsolete and should not be used.
See https://aka.ms/binaryformatter for more information.")]
public static TValue? FromStream<TValue>(Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: (TValue?)FromStream(stream!);
: (TValue?)FromStream(stream);

/// <summary>
/// Deserializes a stream into an object graph.
Expand All @@ -24,9 +24,9 @@ public static partial class BinaryHelper
See https://aka.ms/binaryformatter for more information.")]
public static object? FromStream(Stream? stream)
{
if (stream.IsNullOrEmpty()) return default;
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
_binaryFormatter ??= new BinaryFormatter();
var result = _binaryFormatter.Deserialize(stream!);
var result = _binaryFormatter.Deserialize(stream);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Zaabee.Binary/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ value is null
: BinaryHelper.ToStream(value);

public TValue? FromStream<TValue>(Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: BinaryHelper.FromStream<TValue>(stream!);
: BinaryHelper.FromStream<TValue>(stream);

public Stream ToStream(Type type, object? value) =>
value is null
? Stream.Null
: BinaryHelper.ToStream(value);

public object? FromStream(Type type, Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: BinaryHelper.FromStream(stream!);
: BinaryHelper.FromStream(stream);

public byte[] ToBytes<TValue>(TValue? value) =>
value is null
? Array.Empty<byte>()
: BinaryHelper.ToBytes(value);

public TValue? FromBytes<TValue>(byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: BinaryHelper.FromBytes<TValue>(bytes!);
: BinaryHelper.FromBytes<TValue>(bytes);

public byte[] ToBytes(Type type, object? value) =>
value is null
? Array.Empty<byte>()
: BinaryHelper.ToBytes(value);

public object? FromBytes(Type type, byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: BinaryHelper.FromBytes(bytes!);
: BinaryHelper.FromBytes(bytes);
}
4 changes: 2 additions & 2 deletions src/Zaabee.Binary/Zaabee.Binary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2022.6.0</PackageVersion>
<Version>2022.6.0</Version>
<PackageVersion>2022.6.2</PackageVersion>
<Version>2022.6.2</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageProjectUrl>https://github.com/Mutuduxf/Zaabee.Serializers/tree/master/src/Zaabee.Binary</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static partial class DataContractHelper
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
public static TValue? FromBytes<TValue>(byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
?default:
(TValue?)FromBytes(typeof(TValue), bytes);

Expand All @@ -21,8 +21,8 @@ public static partial class DataContractHelper
/// <returns></returns>
public static object? FromBytes(Type type, byte[]? bytes)
{
if (bytes.IsNullOrEmpty()) return default;
using var ms = new MemoryStream(bytes!);
if (bytes is null || bytes.Length is 0) return default;
using var ms = new MemoryStream(bytes);
return FromStream(type, ms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public static byte[] ToBytes<TValue>(TValue? value) =>
public static byte[] ToBytes(Type type, object? value)
{
using var ms = ToStream(type, value);
return ms.ReadToEnd();
return ms.ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static partial class DataContractHelper
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
public static TValue? FromStream<TValue>(Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: (TValue?)FromStream(typeof(TValue), stream)!;

Expand All @@ -21,8 +21,8 @@ public static partial class DataContractHelper
/// <returns></returns>
public static object? FromStream(Type type, Stream? stream)
{
if (stream.IsNullOrEmpty()) return default;
var result = GetSerializer(type).ReadObject(stream!);
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = GetSerializer(type).ReadObject(stream);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public static string ToXml<TValue>(TValue? value) =>
public static string ToXml(Type type, object? value)
{
using var ms = ToStream(type, value);
return Encoding.UTF8.GetString(ms.ReadToEnd());
return Encoding.UTF8.GetString(ms.ToArray());
}
}
16 changes: 8 additions & 8 deletions src/Zaabee.DataContractSerializer/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ value is null
: DataContractHelper.ToBytes(type, value);

public TValue? FromBytes<TValue>(byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: DataContractHelper.FromBytes<TValue>(bytes!);
: DataContractHelper.FromBytes<TValue>(bytes);

public object? FromBytes(Type type, byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: DataContractHelper.FromBytes(type, bytes!);
: DataContractHelper.FromBytes(type, bytes);

public string ToText<TValue>(TValue? value) =>
value is null
Expand Down Expand Up @@ -53,12 +53,12 @@ value is null
: DataContractHelper.ToStream(type, value);

public TValue? FromStream<TValue>(Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: DataContractHelper.FromStream<TValue>(stream!);
: DataContractHelper.FromStream<TValue>(stream);

public object? FromStream(Type type, Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: DataContractHelper.FromStream(type, stream!);
: DataContractHelper.FromStream(type, stream);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2022.6.0</PackageVersion>
<Version>2022.6.0</Version>
<PackageVersion>2022.6.2</PackageVersion>
<Version>2022.6.2</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageProjectUrl>https://github.com/Mutuduxf/Zaabee.Serializers/tree/master/src/Zaabee.DataContractSerializer</PackageProjectUrl>
Expand Down
8 changes: 4 additions & 4 deletions src/Zaabee.Jil/Jil.Helper.Bytes.FromBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static partial class JilHelper
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
public static TValue? FromBytes<TValue>(byte[]? bytes, Options? options = null, Encoding? encoding = null) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: FromJson<TValue>(GetString(encoding, bytes!), options);
: FromJson<TValue>(GetString(encoding, bytes), options);

/// <summary>
/// Use encoding to decode the bytes into string and deserialize it.
Expand All @@ -24,7 +24,7 @@ public static partial class JilHelper
/// <param name="encoding"></param>
/// <returns></returns>
public static object? FromBytes(Type type, byte[]? bytes, Options? options = null, Encoding? encoding = null) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: FromJson(type, GetString(encoding, bytes!), options);
: FromJson(type, GetString(encoding, bytes), options);
}
8 changes: 4 additions & 4 deletions src/Zaabee.Jil/Jil.Helper.Stream.FromStream.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static partial class JilHelper
public static async Task<TValue?> FromStreamAsync<TValue>(Stream? stream, Options? options = null,
Encoding? encoding = null, CancellationToken cancellationToken = default)
{
if (stream.IsNullOrEmpty()) return default;
var json = GetString(encoding, await stream!.ReadToEndAsync(cancellationToken));
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var json = GetString(encoding, await stream.ReadToEndAsync(cancellationToken));
var result = FromJson<TValue>(json, options);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
Expand All @@ -33,8 +33,8 @@ public static partial class JilHelper
public static async Task<object?> FromStreamAsync(Type type, Stream? stream, Options? options = null,
Encoding? encoding = null, CancellationToken cancellationToken = default)
{
if (stream.IsNullOrEmpty()) return default;
var json = GetString(encoding, await stream!.ReadToEndAsync(cancellationToken));
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var json = GetString(encoding, await stream.ReadToEndAsync(cancellationToken));
var result = FromJson(type, json, options);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/Zaabee.Jil/Jil.Helper.Stream.FromStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static partial class JilHelper
/// <returns></returns>
public static TValue? FromStream<TValue>(Stream? stream, Options? options = null, Encoding? encoding = null)
{
if (stream.IsNullOrEmpty()) return default;
var result = FromJson<TValue>(GetString(encoding, stream!.ReadToEnd()), options);
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = FromJson<TValue>(GetString(encoding, stream.ReadToEnd()), options);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
}
Expand All @@ -28,8 +28,8 @@ public static partial class JilHelper
/// <returns></returns>
public static object? FromStream(Type type, Stream? stream, Options? options = null, Encoding? encoding = null)
{
if (stream.IsNullOrEmpty()) return default;
var result = FromJson(type, GetString(encoding, stream!.ReadToEnd()), options);
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = FromJson(type, GetString(encoding, stream.ReadToEnd()), options);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Zaabee.Jil/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ value is null
: JilHelper.ToBytes(value, _options, _encoding);

public TValue? FromBytes<TValue>(byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: JilHelper.FromBytes<TValue>(bytes!, _options, _encoding);

public object? FromBytes(Type type, byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: JilHelper.FromBytes(type, bytes!, _options, _encoding);

Expand Down Expand Up @@ -59,12 +59,12 @@ value is null
: JilHelper.ToStream(value, _options, _encoding);

public TValue? FromStream<TValue>(Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: JilHelper.FromStream<TValue>(stream!, _options, _encoding);

public object? FromStream(Type type, Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: JilHelper.FromStream(type, stream!, _options, _encoding);
}
4 changes: 2 additions & 2 deletions src/Zaabee.Jil/Zaabee.Jil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2022.6.0</PackageVersion>
<Version>2022.6.0</Version>
<PackageVersion>2022.6.2</PackageVersion>
<Version>2022.6.2</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageProjectUrl>https://github.com/Mutuduxf/Zaabee.Serializers/tree/master/src/Zaabee.Jil</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static partial class MessagePackHelper
public static async ValueTask<TValue?> FromStreamAsync<TValue>(Stream? stream,
MessagePackSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
if (stream.IsNullOrEmpty()) return default;
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = await MessagePackSerializer.DeserializeAsync<TValue>(stream, options, cancellationToken);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
Expand All @@ -14,7 +14,7 @@ public static partial class MessagePackHelper
public static async ValueTask<object?> FromStreamAsync(Type type, Stream? stream,
MessagePackSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
if (stream.IsNullOrEmpty()) return default;
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = await MessagePackSerializer.DeserializeAsync(type, stream, options, cancellationToken);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static partial class MessagePackHelper
public static TValue? FromStream<TValue>(Stream? stream, MessagePackSerializerOptions? options = null,
CancellationToken cancellationToken = default)
{
if (stream.IsNullOrEmpty()) return default;
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = MessagePackSerializer.Deserialize<TValue>(stream, options, cancellationToken);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
Expand All @@ -14,7 +14,7 @@ public static partial class MessagePackHelper
public static object? FromStream(Type type, Stream? stream, MessagePackSerializerOptions? options = null,
CancellationToken cancellationToken = default)
{
if (stream.IsNullOrEmpty()) return default;
if (stream is null || stream.CanSeek && stream.Length is 0) return default;
var result = MessagePackSerializer.Deserialize(type, stream, options, cancellationToken);
stream.TrySeek(0, SeekOrigin.Begin);
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/Zaabee.MessagePack/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ value is null
: MessagePackHelper.ToBytes(type, value, _options);

public TValue? FromBytes<TValue>(byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: MessagePackHelper.FromBytes<TValue>(bytes, _options);

public object? FromBytes(Type type, byte[]? bytes) =>
bytes.IsNullOrEmpty()
bytes is null || bytes.Length is 0
? default
: MessagePackHelper.FromBytes(type, bytes, _options);

Expand All @@ -38,12 +38,12 @@ value is null
: MessagePackHelper.ToStream(type, value, _options);

public TValue? FromStream<TValue>(Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: MessagePackHelper.FromStream<TValue>(stream, _options);

public object? FromStream(Type type, Stream? stream) =>
stream.IsNullOrEmpty()
stream is null || stream.CanSeek && stream.Length is 0
? default
: MessagePackHelper.FromStream(type, stream, _options);
}
Loading

0 comments on commit 038b6cc

Please sign in to comment.