-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from PinguApps/287-requsts-nullable-props
Created stricter JsonSerialisation rules
- Loading branch information
Showing
66 changed files
with
721 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,15 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server, | |
|
||
public async Task Run(string[] args) | ||
{ | ||
var request = new CreateSessionRequest() | ||
_client.SetSession(_session); | ||
|
||
var request = new CreateAccountRequest() | ||
{ | ||
UserId = "664aac1a00113f82e620", | ||
Secret = "80af6605407a3918cd9bb1796b6bfdc5d4b2dc57dad4677432d902e8bef9ba6f" | ||
Email = "[email protected]", | ||
Password = "MyCoolPassword" | ||
}; | ||
|
||
var response = await _client.Account.CreateSession(request); | ||
var response = await _client.Account.Create(request); | ||
|
||
Console.WriteLine(response.Result.Match( | ||
result => result.ToString(), | ||
|
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
8 changes: 8 additions & 0 deletions
8
src/PinguApps.Appwrite.Shared/Attributes/SdkExcludeAttribute.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,8 @@ | ||
using System; | ||
|
||
namespace PinguApps.Appwrite.Shared.Attributes; | ||
|
||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | ||
public class SdkExcludeAttribute : Attribute | ||
{ | ||
} |
126 changes: 126 additions & 0 deletions
126
src/PinguApps.Appwrite.Shared/Converters/IgnoreSdkExcludedPropertiesConverterFactory.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,126 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Attributes; | ||
|
||
namespace PinguApps.Appwrite.Shared.Converters; | ||
|
||
public class IgnoreSdkExcludedPropertiesConverterFactory : JsonConverterFactory | ||
{ | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
if (typeToConvert.IsPrimitive || | ||
typeToConvert.IsEnum || | ||
typeToConvert == typeof(string) || | ||
typeToConvert == typeof(decimal) || | ||
typeToConvert == typeof(DateTime) || | ||
typeToConvert == typeof(DateTimeOffset) || | ||
typeToConvert == typeof(TimeSpan) || | ||
typeToConvert == typeof(Guid) || | ||
typeToConvert == typeof(object)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeof(IEnumerable).IsAssignableFrom(typeToConvert) && typeToConvert != typeof(string)) | ||
{ | ||
return false; | ||
} | ||
|
||
return typeToConvert.IsClass && !typeToConvert.IsAbstract; | ||
} | ||
|
||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var converterType = typeof(IgnoreSdkExcludedPropertiesConverter<>).MakeGenericType(typeToConvert); | ||
|
||
return (JsonConverter)Activator.CreateInstance(converterType)!; | ||
} | ||
|
||
private class IgnoreSdkExcludedPropertiesConverter<T> : JsonConverter<T> where T : class | ||
{ | ||
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
using var jsonDoc = JsonDocument.ParseValue(ref reader); | ||
|
||
var json = jsonDoc.RootElement.GetRawText(); | ||
|
||
var newOptions = new JsonSerializerOptions(options); | ||
|
||
var converterToRemove = newOptions.Converters.FirstOrDefault(x => x.GetType() == typeof(IgnoreSdkExcludedPropertiesConverterFactory)); | ||
|
||
if (converterToRemove is not null) | ||
{ | ||
newOptions.Converters.Remove(converterToRemove); | ||
} | ||
|
||
return JsonSerializer.Deserialize<T>(json, newOptions); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
if (value is null) | ||
{ | ||
writer.WriteNullValue(); | ||
return; | ||
} | ||
|
||
writer.WriteStartObject(); | ||
|
||
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) | ||
.Where(x => x.CanRead && x.GetMethod is not null); | ||
|
||
foreach (var prop in properties) | ||
{ | ||
if (prop.GetCustomAttribute<SdkExcludeAttribute>() is not null) | ||
continue; | ||
|
||
var jsonPropertyNameAttr = prop.GetCustomAttribute<JsonPropertyNameAttribute>(); | ||
|
||
var jsonPropertyName = jsonPropertyNameAttr is not null | ||
? jsonPropertyNameAttr.Name | ||
: (options.PropertyNamingPolicy?.ConvertName(prop.Name) ?? prop.Name); | ||
|
||
var propValue = prop.GetValue(value); | ||
|
||
if (propValue is null && options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull) | ||
continue; | ||
|
||
writer.WritePropertyName(jsonPropertyName); | ||
|
||
var jsonConverterAttr = prop.GetCustomAttribute<JsonConverterAttribute>(); | ||
if (jsonConverterAttr is not null && jsonConverterAttr.ConverterType is not null) | ||
{ | ||
// Instantiate the specified converter | ||
var converterInstance = (JsonConverter?)Activator.CreateInstance(jsonConverterAttr.ConverterType)!; | ||
|
||
// Create a new JsonSerializerOptions instance without the custom converter factory to prevent recursion | ||
var newOptions = new JsonSerializerOptions(options); | ||
|
||
// Remove the custom converter factory to prevent it from being invoked again | ||
var converterToRemove = newOptions.Converters | ||
.FirstOrDefault(c => c.GetType() == typeof(IgnoreSdkExcludedPropertiesConverterFactory)); | ||
|
||
if (converterToRemove != null) | ||
newOptions.Converters.Remove(converterToRemove); | ||
|
||
newOptions.Converters.Add(converterInstance); | ||
|
||
// Serialize the property value using the specified converter and the new options | ||
JsonSerializer.Serialize(writer, propValue, prop.PropertyType, newOptions); | ||
|
||
// Move to the next property after handling with the custom converter | ||
continue; | ||
} | ||
|
||
// If no custom converter is specified, serialize normally | ||
JsonSerializer.Serialize(writer, propValue, prop.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
Oops, something went wrong.