Replies: 1 comment 11 replies
-
I'm encountering a similar issue, I just upgraded from 1.8.2 to 2.1.2, and I'm trying to deserialize my
But if I convert the
@giannik @Piedone what I'm doing wrong here? 🤔 public class UserApiModel : IValidatableObject
{
private string _phoneNumber;
[JsonIgnore]
public string? UserId { get; set; }
[Required(ErrorMessage = $"{nameof(UserName)} is required.")]
public string UserName { get; set; }
[Required(ErrorMessage = $"{nameof(Email)} is required.")]
public string Email { get; set; }
[Required(ErrorMessage = $"{nameof(PhoneNumber)} is required.")]
public string PhoneNumber
{
get => _phoneNumber;
set
{
_phoneNumber = value;
InitializeUserDetails();
}
}
[Required(ErrorMessage = $"{nameof(IsEnabled)} is required.")]
public bool IsEnabled { get; set; } = false;
[JsonIgnore]
public bool TwoFactorEnabled { get; set; } = false;
[JsonIgnore]
public bool IsLockoutEnabled { get; set; } = true;
[NotNullOrEmptyCollection($"{nameof(RoleNames)} list must contain at least one element.")]
public List<string> RoleNames { get; set; }
public UserDetailsApiModel UserDetails { get; set; }
public void InitializeUserDetails()
{
if (UserDetails == null)
{
UserDetails = new UserDetailsApiModel();
}
UserDetails.SetPhone(PhoneNumber);
}
// Method implemented because of "IValidatableObject" interface, and necessary to validate the complex object contained in this class
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
List<ValidationResult> result = new List<ValidationResult>();
if (UserDetails != null)
{
Validator.TryValidateObject(UserDetails, new ValidationContext(UserDetails), result, true);
}
return result;
}
// Implicit cast
public static implicit operator UserApiModel(OrchardCore.Users.Models.User user)
{
string? userDetailsJson = user.Properties["UserDetails"]?.ToString(); // ------ Here I have values
if (string.IsNullOrEmpty(userDetailsJson))
{
throw new InvalidOperationException("UserDetails JSON is null or empty.");
}
var jsonOptions = ShellScope.Services
.GetRequiredService<IOptions<DocumentJsonSerializerOptions>>()
.Value.SerializerOptions;
UserDetails? userDetails = JsonSerializer.Deserialize<UserDetails>(userDetailsJson, jsonOptions);
if (userDetails == null) // --------- Here the deserialization doesn't work
{
throw new InvalidOperationException("Failed to deserialize UserDetails.");
}
return new UserApiModel
{
UserId = user.UserId,
UserName = user.UserName,
Email = user.Email,
PhoneNumber = user.PhoneNumber,
IsEnabled = user.IsEnabled,
TwoFactorEnabled = user.TwoFactorEnabled,
IsLockoutEnabled = user.IsLockoutEnabled,
RoleNames = user.RoleNames.ToList(),
UserDetails = new UserDetailsApiModel
{
Typology = Enum.Parse<Typology>(userDetails.Typology.Text),
Name = userDetails.Name.Text,
Surname = userDetails.Surname.Text,
WhatsApp = userDetails.WhatsApp.Text,
Street = userDetails.Street.Text,
StreetNumber = userDetails.StreetNumber.Text,
City = userDetails.City.Text,
ZipCode = userDetails.ZipCode.Text,
Website = new WebsiteapiModel
{
Url = userDetails.Website.Url,
Text = userDetails.Website.Text
},
Facebook = new FacebookApiModel
{
Url = userDetails.Facebook.Url,
Text = userDetails.Facebook.Text
},
Instagram = new InstagramApiModel
{
Url = userDetails.Instagram.Url,
Text = userDetails.Instagram.Text
},
Description = userDetails.Description.Text,
StripeAccountId = userDetails.StripeAccountId.Text,
Logo = new LogoApiModel
{
Path = userDetails.Logo.Paths.FirstOrDefault()
}
}
};
}
} Thank you |
Beta Was this translation helpful? Give feedback.
11 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm having some issues lately with Serialization of derived C# types using DocumentJsonSerializerOptions .
In Orchard Core you can define the derived types for serialization from this serviceL
But I noticed lately (this was not happening about a month ago )that when you try to serialize the derived type that has been defined from the startup services that when you serialize it doesn't contain the type info metadata to later on deserialize it back to the derived type.
As an example the following code serializes the ContentDeploymentStep without the type info metadata :
Can someone please confirm if it happens on their side?
Beta Was this translation helpful? Give feedback.
All reactions