locked
Not getting similar result in Json and Object System.Text.Json RRS feed

  • Question

  • User2041008840 posted

    Hello, 
    I was using the Newtonsoft.json for long time but after the .net 5 release I am thinking to move to System.Text.Json 
    json result 

    '{"id":3,"email":"prathamesh@gmail.com","password":"123","createdOn":"2020-01-01T00:00:00","staffID":1,"setupID":1,"roleID":1,"role":null,"setup":null,"staff":null}'
    public class UserLogin
        {
            public int ID { get; set; }
            [DataType(DataType.EmailAddress)]
            [Required]
            public string Email { get; set; }
            [Required]
            [StringLength(20)]
            [MinLength(8, ErrorMessage = "Password must contain: Minimum 8 characters atleast 1 Uppercase, 1 lowercase and 1 Number")]
            public string Password { get; set; }
            public DateTime CreatedOn { get; set; } = DateTime.Now;
            public int StaffID { get; set; }
            public int SetupID { get; set; }
            public int RoleID { get; set; }
            public virtual Role Role { get; set; }
            public virtual Setup Setup { get; set; }
            public virtual Staff Staff { get; set; }
        }


    using System.Text.Json
    so tried this code

      public async Task<UserLogin> GetUserLoginsBySignInAsync(string email, string password)
            {
                HttpClient http = new HttpClient();
                var json = await http.GetStringAsync($"{apiUrl.BaseUrl}api/UserLogins/signin?email={email}&password={password}");
                return JsonSerializer.Deserialize<UserLogin>(json);
            }

    in return it just taking only one property after deserialize it 

    but in Newtonsoft.json works fine
     public async Task<UserLogin> GetUserLoginsBySignInAsync(string email, string password)
            {
                HttpClient http = new HttpClient();
                var json = await http.GetStringAsync($"{apiUrl.BaseUrl}api/UserLogins/signin?email={email}&password={password}");
                return JsonConvert.DeserializeObject<UserLogin>(json);
            }
    and it returns all properties

    i found this error in console - Uncaught TypeError: Cannot read property 'length' of undefined for simple JSON array

    How do i get the same result as Newtonsoft.Json?

    Thursday, January 14, 2021 5:36 AM

Answers

  • User-1545767719 posted

    By default key name in JSON string and property name in class are case-sensitive when the deserializer of System.Text.Json is used. Try the following class definition:

    public class UserLogin
    {
        public int id { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public DateTime createdOn { get; set; } = DateTime.Now;
        public int staffID { get; set; }
        public int setupID { get; set; }
        public int roleID { get; set; }
        public virtual Role role { get; set; }
        public virtual Setup setup { get; set; }
        public virtual Staff staff { get; set; }
    }

    or

    JsonSerializer.Deserialize<UserLogin>(json, 
        new JsonSerializerOptions {  PropertyNameCaseInsensitive = true });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2021 6:48 AM
  • User-1330468790 posted

    Hi Prathamesh shende,

     

    As you mentioned that you are migrating from Newtonsoft.Json to System.Text.Json. You might need this documentation:

    How to migrate from Newtonsoft.Json to System.Text.Json

    The documentation lists a table of differences between Newtonsoft.Json and System.Text.Json and differences in default JsonSerializer behavior compared to Newtonsoft.Json, e.g. Case-insensitive deserialization.

     

    Hope this helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2021 11:44 AM

All replies

  • User-1545767719 posted

    By default key name in JSON string and property name in class are case-sensitive when the deserializer of System.Text.Json is used. Try the following class definition:

    public class UserLogin
    {
        public int id { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public DateTime createdOn { get; set; } = DateTime.Now;
        public int staffID { get; set; }
        public int setupID { get; set; }
        public int roleID { get; set; }
        public virtual Role role { get; set; }
        public virtual Setup setup { get; set; }
        public virtual Staff staff { get; set; }
    }

    or

    JsonSerializer.Deserialize<UserLogin>(json, 
        new JsonSerializerOptions {  PropertyNameCaseInsensitive = true });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2021 6:48 AM
  • User-1330468790 posted

    Hi Prathamesh shende,

     

    As you mentioned that you are migrating from Newtonsoft.Json to System.Text.Json. You might need this documentation:

    How to migrate from Newtonsoft.Json to System.Text.Json

    The documentation lists a table of differences between Newtonsoft.Json and System.Text.Json and differences in default JsonSerializer behavior compared to Newtonsoft.Json, e.g. Case-insensitive deserialization.

     

    Hope this helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2021 11:44 AM