Skip to content

JsonConstructor attribute missed on System.Text.Json #1925

Closed

Description

@ahedreville

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

  1. added
    untriaged
    on Jan 19, 2020
  2. ahsonkhan commented on Jan 25, 2020

    @ahsonkhan
    Contributor
  3. removed
    untriaged
    on Jan 25, 2020
  4. added this to the 5.0 milestone on Jan 25, 2020
  5. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions