mirror of https://github.com/abpframework/abp.git
committed by
GitHub
11 changed files with 212 additions and 34 deletions
@ -0,0 +1,41 @@ |
|||||
|
using System; |
||||
|
using System.Text.Json; |
||||
|
using System.Text.Json.Serialization; |
||||
|
|
||||
|
namespace Volo.Abp.Json.SystemTextJson.JsonConverters; |
||||
|
|
||||
|
public class AbpNullableStringToGuidConverter : JsonConverter<Guid?> |
||||
|
{ |
||||
|
private JsonSerializerOptions _writeJsonSerializerOptions; |
||||
|
|
||||
|
public override Guid? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
||||
|
{ |
||||
|
if (reader.TokenType == JsonTokenType.String) |
||||
|
{ |
||||
|
var guidString = reader.GetString(); |
||||
|
string[] formats = { "N", "D", "B", "P", "X" }; |
||||
|
foreach (var format in formats) |
||||
|
{ |
||||
|
if (Guid.TryParseExact(guidString, format, out var guid)) |
||||
|
{ |
||||
|
return guid; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (reader.TryGetGuid(out var guid2)) |
||||
|
{ |
||||
|
return guid2; |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public override void Write(Utf8JsonWriter writer, Guid? value, JsonSerializerOptions options) |
||||
|
{ |
||||
|
_writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this); |
||||
|
var entityConverter = (JsonConverter<Guid?>)_writeJsonSerializerOptions.GetConverter(typeof(Guid?)); |
||||
|
|
||||
|
entityConverter.Write(writer, value, _writeJsonSerializerOptions); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System; |
||||
|
using System.Text.Json; |
||||
|
using System.Text.Json.Serialization; |
||||
|
|
||||
|
namespace Volo.Abp.Json.SystemTextJson.JsonConverters; |
||||
|
|
||||
|
public class AbpStringToGuidConverter : JsonConverter<Guid> |
||||
|
{ |
||||
|
private JsonSerializerOptions _writeJsonSerializerOptions; |
||||
|
|
||||
|
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
||||
|
{ |
||||
|
if (reader.TokenType == JsonTokenType.String) |
||||
|
{ |
||||
|
var guidString = reader.GetString(); |
||||
|
string[] formats = { "N", "D", "B", "P", "X" }; |
||||
|
foreach (var format in formats) |
||||
|
{ |
||||
|
if (Guid.TryParseExact(guidString, format, out var guid)) |
||||
|
{ |
||||
|
return guid; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return reader.GetGuid(); |
||||
|
} |
||||
|
|
||||
|
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options) |
||||
|
{ |
||||
|
_writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this); |
||||
|
var entityConverter = (JsonConverter<Guid>)_writeJsonSerializerOptions.GetConverter(typeof(Guid)); |
||||
|
|
||||
|
entityConverter.Write(writer, value, _writeJsonSerializerOptions); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,85 @@ |
|||||
|
using System; |
||||
|
using System.Text.Json; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Json.SystemTextJson.JsonConverters; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Json; |
||||
|
|
||||
|
public class AbpStringToGuid_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Test_Read() |
||||
|
{ |
||||
|
var options = new JsonSerializerOptions() |
||||
|
{ |
||||
|
Converters = |
||||
|
{ |
||||
|
new AbpStringToGuidConverter(), |
||||
|
new AbpNullableStringToGuidConverter() |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
var guid = Guid.Parse("762DDB84-5225-4853-A566-FF0B3AF57585"); |
||||
|
var testClass = JsonSerializer.Deserialize<TestClass>("{" + |
||||
|
$"\"Id\": \"{guid:N}\", " + |
||||
|
$"\"NullableId\": \"{guid:D}\", " + |
||||
|
$"\"NullableId2\": \"{guid:B}\", " + |
||||
|
$"\"NullableId3\": \"{guid:P}\", " + |
||||
|
$"\"NullableId4\": \"{guid:X}\", " + |
||||
|
"\"NullableId5\": \"\", " + |
||||
|
"\"NullableId6\": null}", options); |
||||
|
testClass.ShouldNotBeNull(); |
||||
|
testClass.Id.ShouldBe(guid); |
||||
|
testClass.NullableId.ShouldBe(guid); |
||||
|
testClass.NullableId2.ShouldBe(guid); |
||||
|
testClass.NullableId3.ShouldBe(guid); |
||||
|
testClass.NullableId4.ShouldBe(guid); |
||||
|
testClass.NullableId5.ShouldBeNull(); |
||||
|
testClass.NullableId6.ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Test_Write() |
||||
|
{ |
||||
|
var options = new JsonSerializerOptions() |
||||
|
{ |
||||
|
Converters = |
||||
|
{ |
||||
|
new AbpStringToGuidConverter(), |
||||
|
new AbpNullableStringToGuidConverter() |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
var guid = Guid.Parse("762DDB84-5225-4853-A566-FF0B3AF57585"); |
||||
|
var json = JsonSerializer.Serialize(new TestClass() |
||||
|
{ |
||||
|
Id = guid, |
||||
|
NullableId = null, |
||||
|
NullableId2 = guid, |
||||
|
NullableId3 = null, |
||||
|
NullableId4 = guid, |
||||
|
NullableId5 = null, |
||||
|
NullableId6 = guid |
||||
|
}, options); |
||||
|
|
||||
|
json.ShouldBe($"{{\"Id\":\"{guid:D}\",\"NullableId\":null,\"NullableId2\":\"{guid:D}\",\"NullableId3\":null,\"NullableId4\":\"{guid:D}\",\"NullableId5\":null,\"NullableId6\":\"{guid:D}\"}}"); |
||||
|
} |
||||
|
|
||||
|
class TestClass |
||||
|
{ |
||||
|
public Guid Id { get; set; } |
||||
|
|
||||
|
public Guid? NullableId { get; set; } |
||||
|
|
||||
|
public Guid? NullableId2 { get; set; } |
||||
|
|
||||
|
public Guid? NullableId3 { get; set; } |
||||
|
|
||||
|
public Guid? NullableId4 { get; set; } |
||||
|
|
||||
|
public Guid? NullableId5 { get; set; } |
||||
|
|
||||
|
public Guid? NullableId6 { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -1,6 +1,9 @@ |
|||||
import { InjectionToken } from '@angular/core'; |
import { InjectionToken } from '@angular/core'; |
||||
import { SetTokenResponseToStorageFn } from '../models'; |
import { SetTokenResponseToStorageFn } from '../models'; |
||||
|
|
||||
|
/** |
||||
|
* @deprecated The token should not be used anymore. |
||||
|
*/ |
||||
export const SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY = new InjectionToken<SetTokenResponseToStorageFn>( |
export const SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY = new InjectionToken<SetTokenResponseToStorageFn>( |
||||
'SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY', |
'SET_TOKEN_RESPONSE_TO_STORAGE_FN_KEY', |
||||
); |
); |
||||
|
|||||
Loading…
Reference in new issue