public void Can_detect_dto_with_no_Version() { using (JsConfig.With(modelFactory: type => { if (typeof(IHasVersion).IsAssignableFrom(type)) { return(() => { var obj = (IHasVersion)type.CreateInstance(); obj.Version = 0; return obj; }); } return(() => type.CreateInstance()); })) { var dto = new Dto { Name = "Foo" }; var fromDto = dto.ToJson().FromJson <DtoV1>(); Assert.That(fromDto.Version, Is.EqualTo(0)); Assert.That(fromDto.Name, Is.EqualTo("Foo")); var dto1 = new DtoV1 { Name = "Foo 1" }; var fromDto1 = dto1.ToJson().FromJson <DtoV1>(); Assert.That(fromDto1.Version, Is.EqualTo(1)); Assert.That(fromDto1.Name, Is.EqualTo("Foo 1")); } }
private static void TestRoundTripValue(Dto dto) { var json = dto.ToJson(); var fromJson = json.FromJson <Dto>(); Assert.That(fromJson.CustomFormatTypeProperty.Value, Is.EqualTo(dto.CustomFormatTypeProperty.Value)); }
public void CallReset_AfterSerializingOnce_WithCustomSerializationForProperty_MustClearCustomSerialization() { var dto = new Dto { CustomFormatTypeProperty = new CustomFormatType(12345) }; JsConfig <CustomFormatType> .DeSerializeFn = str => new CustomFormatType(int.Parse(str)); var json = dto.ToJson(); JsConfig.Reset(); var fromJson = json.FromJson <Dto>(); Assert.That(fromJson.CustomFormatTypeProperty.Value, Is.EqualTo(0)); }
public async Task Test() { IDistributedCache distributedCache = _serviceProvider.GetService <IDistributedCache>(); Dto dto = new Dto { Id = Guid.NewGuid(), Name = "小明", Time = DateTimeOffset.Now, Money = 100 }; string key = $"{GetType().FullName}:{nameof(Test)}"; await distributedCache.RemoveAsync(key); await distributedCache.SetObjectAsync(key, dto); var newDto = await distributedCache.GetObjectAsync <Dto>(key); Assert.AreEqual(dto.ToJson(), newDto.ToJson()); await distributedCache.SetObjectAsync(key, dto.Time); var cacheString = await distributedCache.GetStringAsync(key); Assert.AreEqual(dto.Time.ToString("o"), cacheString); var newTime = await distributedCache.GetObjectAsync <DateTimeOffset>(key); Assert.AreEqual(dto.Time.ToUnixTimeMilliseconds(), newTime.ToUnixTimeMilliseconds()); await distributedCache.SetObjectAsync(key, dto.Id); cacheString = await distributedCache.GetStringAsync(key); Assert.AreEqual(dto.Id.ToString(), cacheString); var newId = await distributedCache.GetObjectAsync <Guid>(key); Assert.AreEqual(dto.Id, newId); await distributedCache.SetObjectAsync(key, dto.Money); cacheString = await distributedCache.GetStringAsync(key); Assert.AreEqual(dto.Money.ToString(), cacheString); var newMoney = await distributedCache.GetObjectAsync <decimal>(key); Assert.AreEqual(dto.Money, newMoney); }
public void Can_detect_dto_with_no_Version() { using (JsConfig.With(modelFactory:type => { if (typeof(IHasVersion).IsAssignableFrom(type)) { return () => { var obj = (IHasVersion)type.CreateInstance(); obj.Version = 0; return obj; }; } return () => type.CreateInstance(); })) { var dto = new Dto { Name = "Foo" }; var fromDto = dto.ToJson().FromJson<DtoV1>(); Assert.That(fromDto.Version, Is.EqualTo(0)); Assert.That(fromDto.Name, Is.EqualTo("Foo")); var dto1 = new DtoV1 { Name = "Foo 1" }; var fromDto1 = dto1.ToJson().FromJson<DtoV1>(); Assert.That(fromDto1.Version, Is.EqualTo(1)); Assert.That(fromDto1.Name, Is.EqualTo("Foo 1")); } }