You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonConstructor attribute missed on System.Text.Json #1925
I missed the Newtonsoft.Json the nice attribute JsonConstructor that is very handy when serializing classes that do not expose public constructor.. like the one below...
public class Envelope<T>
{
public T Result { get; }
public string ErrorMessage { get; }
public DateTime TimeGenerated { get; }
[JsonConstructor] // This constructor is marked as the valid JsonConvert constructor for JsonConvert.DeserializeObject usage.
protected internal Envelope(T result, string errorMessage)
{
Result = result;
ErrorMessage = errorMessage;
TimeGenerated = DateTime.UtcNow;
}
}
public sealed class Envelope : Envelope<string>
{
private Envelope(string error)
: base(null, error)
{
}
public static Envelope Ok() => new Envelope(null);
public static Envelope<T> Ok<T>(T result) => new Envelope<T>(result, default);
public static Envelope Error(string error) => new Envelope(error);
public static Envelope<T> Error<T>(T result) => new Envelope<T>(result, default);
public static Envelope<T> Error<T>(T result, string error) => new Envelope<T>(result, error);
}
Activity
ahsonkhan commentedon Jan 25, 2020
This is similar to https://github.com/dotnet/corefx/issues/38569. Closing as duplicate.