Skip to content

Commit

Permalink
update 1.0.1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Jul 4, 2024
1 parent 847c6d1 commit 15894be
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 62 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Refer to the test code for how to use it.
net7, net8

## Version
* 1.0.1.12
* remove - xToJoin
* change - xJoin, if empty collection, return string.empty.
*
* 1.0.1.11
* change - xIsEmpty dose not support Number type.
* remove - xIsNumber<T>(this T obj), remain xIsNumber(this string str)
Expand Down
103 changes: 51 additions & 52 deletions src/XSerializeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using MongoDB.Bson;

namespace eXtensionSharp
{
Expand Down Expand Up @@ -34,57 +33,57 @@ public static string xToJson<T>(this T entity, JsonSerializerOptions options = n
return JsonSerializer.Serialize(entity, options);
}

public static T xToDeserialize<T>(this string jsonString, JsonSerializerOptions options = null)
{
if (options.xIsEmpty())
{
options = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
Converters = { new ObjectIdConverter() }
};
}
return JsonSerializer.Deserialize<T>(jsonString, options);
}

public static string xToSerialize<T>(this T entity, JsonSerializerOptions options = null)
where T : class
{
if (options.xIsEmpty())
{
options = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
Converters = { new ObjectIdConverter() }
};
}

return JsonSerializer.Serialize(entity, options);
}
// public static T xToDeserialize<T>(this string jsonString, JsonSerializerOptions options = null)
// {
// if (options.xIsEmpty())
// {
// options = new JsonSerializerOptions()
// {
// PropertyNameCaseInsensitive = true,
// ReferenceHandler = ReferenceHandler.IgnoreCycles,
// Converters = { new ObjectIdConverter() }
// };
// }
// return JsonSerializer.Deserialize<T>(jsonString, options);
// }
//
// public static string xToSerialize<T>(this T entity, JsonSerializerOptions options = null)
// where T : class
// {
// if (options.xIsEmpty())
// {
// options = new JsonSerializerOptions()
// {
// PropertyNameCaseInsensitive = true,
// ReferenceHandler = ReferenceHandler.IgnoreCycles,
// Converters = { new ObjectIdConverter() }
// };
// }
//
// return JsonSerializer.Serialize(entity, options);
// }
}

internal class ObjectIdConverter : JsonConverter<ObjectId>
{
public override void Write(Utf8JsonWriter writer, ObjectId value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}

public override ObjectId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var stringValue = reader.GetString();
if (ObjectId.TryParse(stringValue, out var objectId))
{
return objectId;
}
throw new JsonException($"Unable to convert \"{stringValue}\" to ObjectId.");
}

public override bool CanConvert(Type typeToConvert)
{
return typeof(ObjectId).IsAssignableFrom(typeToConvert);
}
}
// internal class ObjectIdConverter : JsonConverter<ObjectId>
// {
// public override void Write(Utf8JsonWriter writer, ObjectId value, JsonSerializerOptions options)
// {
// writer.WriteStringValue(value.ToString());
// }
//
// public override ObjectId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
// {
// var stringValue = reader.GetString();
// if (ObjectId.TryParse(stringValue, out var objectId))
// {
// return objectId;
// }
// throw new JsonException($"Unable to convert \"{stringValue}\" to ObjectId.");
// }
//
// public override bool CanConvert(Type typeToConvert)
// {
// return typeof(ObjectId).IsAssignableFrom(typeToConvert);
// }
// }
}
7 changes: 1 addition & 6 deletions src/XStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static string xSubstring(this string str, int startIndex, int length = 0)
/// </example>
public static string xJoin<T>(this IEnumerable<T> src, string separator = ",")
{
if (src.xIsEmpty()) return string.Empty;
return string.Join(separator, src);
}

Expand Down Expand Up @@ -345,12 +346,6 @@ public static T xExtractNumber<T>(this string value) where T : struct
return result.xToNumber<T>();
}

public static string xToJoin(this string[] values, string separator = ",")
{
if (values.xIsEmpty()) return string.Empty;
return string.Join(separator, values);
}

/// <summary>
/// seperate to string arry
/// </summary>
Expand Down
4 changes: 0 additions & 4 deletions src/eXtensionSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@
<Link>README.md</Link>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.27.0" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions test/XStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,20 @@ public async Task string_compress_async_test()

Assert.That(text, Is.EqualTo(decompressTest));
}



[Test]
public void xjoin_test()
{
var array = new[] { "hello", "world" };
var str = array.xJoin();
Assert.That(str, Is.EqualTo("hello,world"));

var empty_array = Array.Empty<string>();
var empty_str = string.Empty;
Assert.That(empty_str, Is.EqualTo(string.Empty));

}
}
}

0 comments on commit 15894be

Please sign in to comment.