简体   繁体   English

如何使用 jsonapi 1.0 在属性中输入 map 复杂类型

[英]how to map complex type in Attribute with jsonapi 1.0

I have a class SubscriptionType and in its attribute is a complex class called InfoText , LongInfoText and Name they all have en and ur property.我有一个 class SubscriptionType ,它的属性是一个复杂的 class ,称为InfoTextLongInfoTextName他们都有enur属性。 when i use JsonApiNet 3.0.0 library to deserialize my json from a wep api.当我使用JsonApiNet 3.0.0库从 wep api 反序列化我的 json 时。 all objects are retrieved expect the complex type that exists in attributes.检索所有对象,除了属性中存在的复杂类型。 null values are shown.显示了 null 值。 I have tried to use my own resolver but it didn't not work.我尝试使用我自己的解析器,但它没有用。 can someone help me in this.有人可以帮助我吗? Thank you谢谢

My resolver我的解析器

 public class NamingThingsIsHardResolver : JsonApiPropertyResolver
    {
        public override PropertyInfo ResolveJsonApiAttribute(Type type, string attributeName)
        {
            if (type == typeof(SubscriptionType) && attributeName == "name")
            {
                return type.GetProperty("Name");
            }

            return base.ResolveJsonApiAttribute(type, attributeName);
        }
    }

my type我的风格

public class Subscription
{
    public string Id { get; set; }
    public string ContractId { get; set; }
    public string PaymentType { get; set; }
    public string Status { get; set; }
    public SubscriptionType SubscriptionType { get; set; }
}
    public class SubscriptionType
{
    public string Id { get; set; }      
    public string BusinessModelType { get; set; }
    public InfoText InfoText;
    public string LineType { get; set; }
    public LongInfoText LongInfoText;
     //[JsonApiRelationship("name")] // [JsonApiAttribute("name")] //also not working
    public string Name;
}

my complex type我的复杂类型

public class Name 
{ 
    public string En { get; set; } 
    public string Ur { get; set; } 
}

 public class InfoText
    {
        public string En { get; set; }
        public string Ur { get; set; }
    }

 public class LongInfoText
    {
        public string En { get; set; }
        public string Ur { get; set; }
    }

my json this json is simplified.我的 json这个 json 被简化了。 some attributes and relationships are removed一些属性和关系被删除

 {
    "data": [
        {
            "attributes": {
                "contract-id": "103",
                "payment-type": "prepaid",
                "status": "active",
            },
            "id": "103",
            "links": {
                "self": "/api/v1/subscriptions/103"
            },
            "relationships": {              
                "subscription-type": {
                    "data": {
                        "id": "1",
                        "type": "subscription-types"
                    },
                    "links": {
                        "related": "/api/v1/subscriptions/103/subscription-type"
                    }
                }
                
            },
            "type": "subscriptions"
        }
    ],
    "included": [
        {
            "attributes": {
                "business-model-type": "prepaid",
                "info-text": {
                    "en": null,
                    "ur": null
                },
                "line-type": "mobile",
                "long-info-text": {
                    "en": null,
                    "ur": null
                },
                "name": {
                    "en": "Start prepaid",
                    "ur": "Start Prepaid"
                }
            },
            "id": "1",
            "links": {
                "self": "/api/v1/subscription-types/1"
            },
            "relationships": {
                "billing-rate-plans": {
                    "links": {
                        "related": "/api/v1/subscription-types/1/billing-rate-plans"
                    }
                }
            },
            "type": "subscription-types"
        }
    ]
}

I didnot find solution in JsonApiNet library, so i created a json deserializer like below.我在 JsonApiNet 库中没有找到解决方案,所以我创建了一个 json 解串器,如下所示。 i am not happy with it but it works.我对此不满意,但它有效。 please do improve this answer thanks.请改进这个答案谢谢。

my method in SubscriptionType.cs我在 SubscriptionType.cs 中的方法

 public void InjectAttributes(JsonApiAttributes attributes) {

            // InfoText
            var infoText = attributes["info-text"].ToString();
            InfoText = JsonConvert.DeserializeObject<Lang>(infoText);

            // LongInfoText
            var longInfoText = attributes["long-info-text"].ToString();
            LongInfoText = JsonConvert.DeserializeObject<Lang>(longInfoText);

            // Name
            var name = attributes["name"].ToString();
            Name = JsonConvert.DeserializeObject<Lang>(name);

        }

and i called it like this.我这样称呼它。

var document = JsonApi.Document<List<Subscription>>(response.Content.ReadAsStringAsync().Result);

List<Subscription> subscriptions = document.Resource;

foreach (var subscription in subscriptions)
{
    subscription.SubscriptionType.InjectAttributes(document.Included[0].Attributes);
}

a separate c# object class is also created还创建了一个单独的 c# object class

public class Lang
    {
        public string En { get; set; }
        public string Ur { get; set; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM