Comsos DB对象反序列化不起作用,但是序列化

时间:2018-07-11 13:56:01

标签: c# azure json.net azure-cosmosdb

在我当前的项目中,我想将派生对象保存到Cosmos DB。因此,我必须将TypeNameHandling设置添加到JSON.net默认值。所以目前这是我的设置:

        JsonConvert.DefaultSettings = () => new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            TypeNameHandling = TypeNameHandling.All
        };

序列化效果很好,例如,这是我的Cosmos DB集合的结果:

{
"$type": "Voice.Models.Account, Voice.Models",
"userId": "auth0|5b45f7746d829f1018605826",
"chargebeeCustomerId": "B4bhXvQxUjHunR2D",
"mail": "me41@niklasraab.de",
"billingSettedUp": true,
"isAmazonAccount": false,
"reviewCreatedHookUrls": {
    "$type": "System.Collections.Generic.List`1[[System.String, System.Private.CoreLib]], System.Private.CoreLib",
    "$values": []
},
"campaigns": {
    "$type": "System.Collections.Generic.List`1[[Voice.Models.Campaign, Voice.Models]], System.Private.CoreLib",
    "$values": [
        {
            "$type": "Voice.Models.BusinessReviewCampaign, Voice.Models",
            "internalReviewUrl": "amz123",
            "userCanDecideWhichPlatform": false,
            "googlePlacesInputText": "Testarossa Winery, College Avenue, Los Gatos, CA, USA",
            "googlePlacesId": "ChIJCY6Cbwg2joAR2mloLv0NqsU",
            "qrCodeUrl": "https://storagevoice.blob.core.windows.net/qr-codes/amz123.jpeg",
            "subscriptionOrAddonId": "starter-edition",
            "skipReviewIfSatisfied": false,
            "question": "Wie hat Ihnen ihr heutiger Besuch bei uns gefallen?",
            "thankYouText": "Vielen Dank für Ihr Feedback",
            "questionNegativ": "Es tut uns leid, dass wir Sie nicht begeistern konnten, was können wir beim nächsten mal besser machen?",
            "questionPositiv": "Wir sind erfreut, dass unser Service Sie begeistert. Wie würden Sie unseren Service in Worte fassen?",
            "name": "Kampagne 1",
            "showMailField": false,
            "showPhoneField": false,
            "showNameField": false,
            "mailFieldIsMandatory": false,
            "phoneFieldIsMandatory": false,
            "nameFieldIsMandatory": false,
            "coupons": {
                "$type": "System.Collections.Generic.List`1[[System.String, System.Private.CoreLib]], System.Private.CoreLib",
                "$values": []
            },
            "reUseCoupons": false,
            "reviewPortals": {
                "$type": "System.Collections.Generic.List`1[[Voice.Models.ReviewPortal, Voice.Models]], System.Private.CoreLib",
                "$values": []
            },
            "minStarsForGoodReview": 5,
            "id": "03794bef-927c-4682-976b-279e9b3c5051",
            "created": "0001-01-01T00:00:00+00:00",
            "updated": "0001-01-01T00:00:00+00:00",
            "type": "Voice.Models.BusinessReviewCampaign, Voice.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        }
    ]
},
"id": "8636cdb8-e14d-446a-8e13-685b61b32a4b",
"created": "2018-07-11T14:26:34.8705129+02:00",
"updated": "2018-07-11T14:26:34.8705181+02:00",
"type": "Voice.Models.Account, Voice.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"_rid": "E158AP+b4ACZAAAAAAAAAA==",
"_self": "dbs/E158AA==/colls/E158AP+b4AA=/docs/E158AP+b4ACZAAAAAAAAAA==/",
"_etag": "\"0500199d-0000-0000-0000-5b45f85e0000\"",
"_attachments": "attachments/",
"_ts": 1531312222

}

例如campaigns会引起问题。在那里您可以看到类型Voice.Models.BusinessReviewCampaign, Voice.Models,但是如果我查询数据库,则该对象是基类Campaign的对象。

type属性是我的财产,便于查询。希望这不会打扰。

谁能解释为什么反序列化选择了错误的类型(基类型)?

1 个答案:

答案 0 :(得分:0)

简便的解决方案,请勿使用默认设置。在上一次更新中,您可以通过DocumentClient类传递序列化设置。