Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Jul 1, 2024
1 parent 46d7b73 commit 847c6d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
81 changes: 63 additions & 18 deletions src/XSerializeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,90 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using MongoDB.Bson;

namespace eXtensionSharp
{
public static class XSerializeExtensions
{
public static T xToEntity<T>(this string jsonString, JsonSerializerOptions options = null)
{
if (options.xIsEmpty()) options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, ReferenceHandler = ReferenceHandler.IgnoreCycles };
if (options.xIsEmpty())
{
options = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles
};
}
return JsonSerializer.Deserialize<T>(jsonString, options);
}

public static async Task<T> xToEntityAsync<T>(this Stream stream, JsonSerializerOptions options = null)
public static string xToJson<T>(this T entity, JsonSerializerOptions options = null)
where T : class
{
if (options.xIsEmpty()) options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, ReferenceHandler = ReferenceHandler.IgnoreCycles };
return await JsonSerializer.DeserializeAsync<T>(stream, options);
if (options.xIsEmpty())
{
options = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles
};
}

return JsonSerializer.Serialize(entity, options);
}

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

public static async Task<IEnumerable<T>> xToEntitiesAsync<T>(this Stream stream, JsonSerializerOptions options = null)
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 };
return await JsonSerializer.DeserializeAsync<IEnumerable<T>>(stream, options);
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 static string xToJson<T>(this T entity, JsonSerializerOptions serializerOptions = null)
where T : class
public override ObjectId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!serializerOptions.xIsEmpty())
return JsonSerializer.Serialize(entity, serializerOptions);
return JsonSerializer.Serialize(entity);
var stringValue = reader.GetString();
if (ObjectId.TryParse(stringValue, out var objectId))
{
return objectId;
}
throw new JsonException($"Unable to convert \"{stringValue}\" to ObjectId.");
}

public static string xToJson<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
public override bool CanConvert(Type typeToConvert)
{
return JsonSerializer.Serialize(dictionary);
return typeof(ObjectId).IsAssignableFrom(typeToConvert);
}
}
}
4 changes: 4 additions & 0 deletions src/eXtensionSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
<Link>README.md</Link>
</Content>
</ItemGroup>

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

</Project>

0 comments on commit 847c6d1

Please sign in to comment.