-
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.
Adding ArgumentNullExceptionExtensions, JsonAnyTypeConverter, and Obj…
…ectQueryStringExtensions,
- Loading branch information
Showing
13 changed files
with
246 additions
and
32 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* ObjectQueryStringExtensions.cs | ||
* Created: 2024-04-27T19:45:11-04:00 | ||
* Modified: 2024-04-27T19:45:13-04:00 | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Copyright: © 2022 - 2024 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace System.Net.Http; | ||
|
||
public static class ObjectQueryStringExtensions | ||
{ | ||
// public static string ToQueryString<T>(this T t) | ||
// { | ||
// return (t as object).ToQueryString(typeof(T).GetRuntimeProperties().ToArray()); | ||
// } | ||
|
||
public static string ToQueryString(this object o) | ||
{ | ||
var properties = o.GetType().GetRuntimeProperties().ToArray(); | ||
var sb = new StringBuilder(); | ||
foreach (var property in properties) | ||
{ | ||
var value = property.GetValue(o); | ||
|
||
// if (property.GetCustomAttributes<JConverterAttribute>().Any()) | ||
// { | ||
// var converter = property.GetCustomAttribute<JConverterAttribute>().ConverterType; | ||
// var instance = Activator.CreateInstance(converter) as JConverter; | ||
// var method = converter.GetMethod("Write"); | ||
// method.Invoke(instance, new object[] { sb, value, null }); | ||
|
||
// } | ||
|
||
if (value is null) | ||
{ | ||
continue; | ||
} | ||
else if (value is Enum e) | ||
{ | ||
value = e.GetEnumMemberValue() ?? e.GetName() ?? e.ToString(); | ||
} | ||
|
||
if (sb.Length > 0) | ||
{ | ||
sb.Append('&'); | ||
} | ||
|
||
var propName = ( | ||
property.GetCustomAttribute<JPropAttribute>()?.Name | ||
?? property.Name.FromCasing('\0', false) | ||
); | ||
|
||
sb.Append($"{propName}={value}"); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
System/System.Text.Json/EnumMemberValueToStringConverter.cs
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* EnumValueToStringConverter.cs | ||
* Created: 2024-05-05T20:43:59-04:00 | ||
* Modified: 2024-05-05T20:43:59-04:00 | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Copyright: © 2022 - 2024 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
using System.Text.Json; | ||
|
||
namespace System.Text.Json; | ||
|
||
public class EnumMemberValueToStringConverter<TEnum> : JsonConverter<TEnum> | ||
where TEnum : struct, Enum | ||
{ | ||
public override TEnum Read(ref Utf8JsonReader reader, type typeToConvert, Jso options) | ||
{ | ||
if (reader.TokenType != JTokenType.String) | ||
{ | ||
throw new JException($"Expected a string but got {reader.TokenType}"); | ||
} | ||
|
||
var enumString = reader.GetString() ?? throw new JException("Expected a non-null string"); | ||
|
||
foreach (var value in Enums.GetValues<TEnum>()) | ||
{ | ||
if (value.GetEnumMemberValue() == enumString) | ||
{ | ||
return value; | ||
} | ||
} | ||
|
||
throw new JException($"Unable to convert '{enumString}' to {typeof(TEnum).Name}"); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, TEnum value, Jso options) | ||
{ | ||
writer.WriteStringValue(value.GetEnumMemberValue()); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* JsonAnyTypeConverter.cs | ||
* Created: 2024-06-05T13:48:06-04:00 | ||
* Modified: 2024-06-05T13:48:06-04:00 | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Copyright: © 2022 - 2024 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace System.Text.Json; | ||
|
||
public class JsonAnyTypeConverter : JsonAnyTypeConverter<object> { } |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* JsonAnyTypeSerilalizer.cs | ||
* Created: 2024-06-05T13:21:08-04:00 | ||
* Modified: 2024-06-05T13:21:08-04:00 | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Copyright: © 2022 - 2024 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace System.Text.Json; | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
public class JsonAnyTypeConverter<T> : JsonConverter<T> | ||
where T : new() | ||
{ | ||
public const string DefaultDiscriminator = "$type"; | ||
|
||
protected virtual object Construct(type t) => Activator.CreateInstance(t); | ||
|
||
public override T? Read(ref Utf8JsonReader reader, type typeToConvert, Jso options) | ||
{ | ||
var jObject = JDoc.ParseValue(ref reader).RootElement; | ||
var typeName = jObject.GetProperty("$type").GetString(); | ||
var value = (T)Construct(typeToConvert); | ||
|
||
foreach (var property in jObject.EnumerateObject()) | ||
{ | ||
if (property.Name == DefaultDiscriminator) | ||
{ | ||
continue; | ||
} | ||
|
||
var propertyType = type.GetType(typeName); | ||
var propertyValue = Deserialize(property.Value.ToString(), propertyType, options); | ||
var objectProperty = typeToConvert.GetProperty(property.Name); | ||
objectProperty.SetValue(value, propertyValue); | ||
} | ||
return value; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, Jso options) | ||
{ | ||
writer.WriteStartObject(); | ||
var type = value.GetType(); | ||
writer.WriteString(DefaultDiscriminator, type.AssemblyQualifiedName); | ||
foreach (var property in type.GetProperties()) | ||
{ | ||
var propertyValue = property.GetValue(value); | ||
writer.WritePropertyName(property.Name); | ||
Serialize(writer, propertyValue, property.PropertyType, options); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
} |
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
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
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
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
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