-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialization of strongly typed id should just store the value directly #110
Comments
The details are important here. How are you serialising it? There are a bunch of converters built-in so that it works as you describe, but you have to enable some of them (as some require external assembly references) |
I'm facing the same issue with System.Text.Json. I've came across this article that describes a workaround, but maybe there's a built-in solution? |
Hey Andrewlock I use System.Json.Text to serialize, usign the default behavior. I am asking about having the default behavior serialize it like a string. |
This is already supported, you can read how to add System.Text.Json converters in the readme😊 |
We have a part in our system still using newtonsoft. So that was that problem. I am still unable to get this to work as serialized Appsettings in asp.net. |
This is already supported, the Newtonsoft.Json converter is added by default 😊 |
As we had the same issue here is an implementation of a StronglyTypedIdConverterFactory to handle the correct serialization/deserialization for System.Text.Json. Maybe there is a chance to annotate StronglyTypedIds by default with such a StronglyTypedIdConverterFactory to support proper System.Text.Json serialization ? Usage is:
implementation is
unitest is
|
I'm not sure why the using StronglyTypedIds;
[StronglyTypedId(converters: StronglyTypedIdConverter.SystemTextJson)]
public partial struct TestTypedId { } Maybe I'm missing something here? 🤔 |
@andrewlock I just encountered the same issue. I believe generated For reference, I use following code: [assembly:StronglyTypedIdDefaults(
backingType: StronglyTypedIdBackingType.Int,
converters: StronglyTypedIdConverter.SystemTextJson | StronglyTypedIdConverter.EfCoreValueConverter
)]
[StronglyTypedId]
public partial struct UserId { } and it generates this: Generated code[System.Text.Json.Serialization.JsonConverter(typeof(UserIdSystemTextJsonConverter))]
readonly partial struct UserId : System.IComparable<UserId>, System.IEquatable<UserId>
{
public int Value { get; }
public UserId(int value)
{
Value = value;
}
public static readonly UserId Empty = new UserId(0);
public bool Equals(UserId other) => this.Value.Equals(other.Value);
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is UserId other && Equals(other);
}
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
public static bool operator ==(UserId a, UserId b) => a.Equals(b);
public static bool operator !=(UserId a, UserId b) => !(a == b);
public int CompareTo(UserId other) => Value.CompareTo(other.Value);
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<UserId, int>
{
public EfCoreValueConverter() : this(null) { }
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
: base(
id => id.Value,
value => new UserId(value),
mappingHints
) { }
}
class UserIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<UserId>
{
public override UserId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new UserId(reader.GetInt32());
}
public override void Write(System.Text.Json.Utf8JsonWriter writer, UserId value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}
} |
Ah yes, didn't saw it... and the required nuget package is in beta. |
Hi @klinki,
Hmmm.... 🤔 As far as I know, that shouldn't make a difference... I've run integration tests that confirm this too. Even if the IDs is defined in a different project, the System.Text.Json converter still detects the converter attribute and can use it. I think the one case it would be an issue is if you're using the System.Text.Json source generator, because in that case you have to "manually" add the converter to the serialization context. So in short, I think it makes sense to make them public anyway, and I've done that in #117.
I'm currently working on a big redesign of the library in this PR: The main idea is to make the library much more maintainable while also giving people a mechanism to customise the generated IDs as much as they like. At some point after that I'll probably move it out of beta |
Today, when you store a stronglytypedid, the result stores it as the object:
Instead (at least optionally), it should store the value as the wrapped value:
The text was updated successfully, but these errors were encountered: