Skip to content

Commit

Permalink
added Guid deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Oct 9, 2023
1 parent 791d6d3 commit 8d0016e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
52 changes: 52 additions & 0 deletions _src/Implementation/Converters/GuidConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Newtonsoft.Json;

namespace Implementation.Converters
{
/// <summary>
/// Converts a <see cref="Guid"/> to and from its <see cref="System.String"/> representation.
/// </summary>
public class GuidConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>Returns <c>true</c> if this instance can convert the specified object type; otherwise <c>false</c>.</returns>
public override bool CanConvert(Type objectType)
{
return objectType.IsAssignableFrom(typeof(Guid));
}

/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

Check warning on line 29 in _src/Implementation/Converters/GuidConverter.cs

View workflow job for this annotation

GitHub Actions / Build, Test & Deploy

Nullability of type of parameter 'existingValue' doesn't match overridden member (possibly because of nullability attributes).
{
try
{
return serializer.Deserialize<Guid>(reader);
}
catch
{
return Guid.Empty;
}
}

/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)

Check warning on line 47 in _src/Implementation/Converters/GuidConverter.cs

View workflow job for this annotation

GitHub Actions / Build, Test & Deploy

Nullability of type of parameter 'value' doesn't match overridden member (possibly because of nullability attributes).
{
serializer.Serialize(writer, value);
}
}
}
21 changes: 21 additions & 0 deletions _src/Implementation/Repositories/AEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Implementation.Converters;
using Infrastructure.Repositories;
using Newtonsoft.Json;

namespace Implementation.Repositories
{
public abstract class AEntity : IEntity
{
[JsonConverter(typeof(GuidConverter))]
public Guid Id { get; set; }

protected AEntity()
{
if (Id == Guid.Empty)
{
Id = Guid.NewGuid();
}
}
}
}
1 change: 1 addition & 0 deletions _src/Infrastructure/Repositories/IEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;

namespace Infrastructure.Repositories
{
Expand Down
2 changes: 1 addition & 1 deletion _src/_Tests/ImplementationTest/ImplementationTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\JRT\0021-users.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public async Task JRT_0011_Given_EmptyRepository_When_CreateEntityCalled_Then_En
Age = 25
};
await repository.Create(user).SaveChangesAsync();
var jsonString = $"[{{\"Id\":\"{user.Id}\",\"Name\":\"{user.Name}\",\"Age\":{user.Age}}}]";

var jsonString = $"[{{\"Name\":\"{user.Name}\",\"Age\":{user.Age},\"Id\":\"{user.Id}\"}}]";
Assert.True(File.Exists(fileName));
Assert.Equal(jsonString, await File.ReadAllTextAsync(fileName));
}
Expand All @@ -76,14 +76,14 @@ public void JRT_0011_Given_EmptyRepository_When_CreateEntityCalledSynchronously_
try
{
using var repository = CreateRepositoryFactory(@".\data").CreateJsonRepository<IUser, User>($"users-{id}");
var user = new User
var user = new User()
{
Name = "Peter",
Age = 25
};
repository.Create(user);
repository.SaveChanges();
var jsonString = $"[{{\"Id\":\"{user.Id}\",\"Name\":\"{user.Name}\",\"Age\":{user.Age}}}]";
var jsonString = $"[{{\"Name\":\"{user.Name}\",\"Age\":{user.Age},\"Id\":\"{user.Id}\"}}]";
var text = File.ReadAllText(fileName);
Assert.True(File.Exists(fileName));
Assert.Equal(jsonString, text);
Expand All @@ -102,9 +102,11 @@ public async Task JRT_0021_Given_JsonFile_When_GetAllEntitiesCalled_Then_AllEnti
{
var fileName = @".\Resources\JRT\0021-users.json";
await using var repository = CreateRepositoryFactory(@".\Resources\JRT").CreateJsonRepository<IUser, User>("0021-users");
var allUsers = await repository.GetAllEntitiesAsync();
var allUsers = (await repository.GetAllEntitiesAsync()).ToList();
var guid = Guid.Parse("061e971d-5ca2-4b9f-998f-66766b06c4ce");
Assert.True(File.Exists(fileName));
Assert.Equal(4, allUsers.Count());
Assert.Equal(4, allUsers.Count);
Assert.Equal(allUsers.First().Id, guid);
}

private IApplicationSettings CreateMockApplicationSettings(string folder)
Expand Down
4 changes: 2 additions & 2 deletions _src/_Tests/ImplementationTest/RepositoryTests/Model/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ImplementationTest.RepositoryTests.Model
{
public interface IUser : IEntity
{
public string Name { get; set; }
public int Age { get; set; }
string Name { get; set; }
int Age { get; set; }
}
}
16 changes: 14 additions & 2 deletions _src/_Tests/ImplementationTest/RepositoryTests/Model/User.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using System;
using System.Collections.Generic;
using Implementation.Converters;
using Implementation.Repositories;
using Infrastructure.Repositories;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ImplementationTest.RepositoryTests.Model
{
public class User : IUser
public class User : AEntity, IUser
{
public Guid Id { get; } = Guid.NewGuid();
// [JsonConverter(typeof(GuidConverter))]
// public Guid Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }

public User() : base()
{

}

}
}

0 comments on commit 8d0016e

Please sign in to comment.