-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46d7b73
commit 847c6d1
Showing
2 changed files
with
67 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters