Newtonsoft.Json deserialize issue [Answered]

DDasic8/28/2022
I have a json string. For example this one:
{"type":"accept","id":0,"ms_id":"00000","user_id":"000000000000","item_name":"Test Item","acc_name":"account"}

How can I deserialize some fields to a structure field in my class? For instance:
public class Order
{
    [JsonProperty("id")] public int Id { get; init; }
    [JsonProperty("item_name")] public string ItemName { get; init; }

    [JsonProperty("ms_id")] public long MessageId { get; init; }

    public Account Account { get; init; }
}

public struct Account
{
    [JsonProperty("user_id")] public long UserId { get; init; }
    [JsonProperty("acc_name")] public long Nickname { get; init; }
}

How can I deserialize the string to put these two fields in an Account field?
YYawnder8/28/2022
You'd need a custom deserialiser for that.
Jjcotton428/28/2022
afaik neither Newtonsoft, nor System.Text.Json can do what you want direclty
YYawnder8/28/2022
What I would do is have the input model flat, then transform it to your DTO manually rather than making a custom converter. (if you have no control over the input string, that is.)
DDasic8/28/2022
Okay, thank you so much for answers
YYawnder8/28/2022
It's not about structs. It's about the target class not having the same structure than the source.
YYawnder8/28/2022
(Source has A, B, C, and the target class has A, D while D contains B, C.)
AAccord8/28/2022
✅ This post has been marked as answered!