【问题标题】:Comsos DB object deserialization not working but serializationCosmos DB 对象反序列化不起作用但序列化
【发布时间】:2018-07-11 13:56:01
【问题描述】:

在我当前的项目中,我想将派生对象保存到 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 属性是我的财产,便于查询。希望这不会打扰任何事情。

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

【问题讨论】:

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


    【解决方案1】:

    简单的解决方案,不要使用默认设置。通过上次更新,您可以使用 DocumentClient 类传递序列化设置。

    【讨论】:

      猜你喜欢
      • 2012-09-18
      • 1970-01-01
      • 2023-04-06
      • 2020-11-20
      • 2021-01-26
      • 2020-10-16
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多