Как решить мою проблему с помощью библиотеки jsondiffer NuGet, которая не соответствует?
Пожалуйста, я мог бы использовать любую помощь с этой проблемой.. Я использую JsonDiffer GitHub - aminm-net/JsonDiffer.Netstandard: легкая утилита для сравнения объектов JSON и, следовательно, практически любой сериализуемой сущности[^]
чтобы сравнить два json. Два Json-это одно и то же и происходит от одного и того же вызова API. Я использую просто применение Json для отображения объектов и десериализации его, а также сериализации и хранения, как и ожидалось. Затем я использую IRestResponse и получаю string json, который является моим acutal. После этого я использую JsonDiff, я получаю Json по-другому.. Я не понимаю почему. Пожалуйста, взгляните на всю информацию ниже, я предоставил все, чтобы получить ясную картину. Спасибо :)
//Вот мой метод для сравнения
public class JsonHelper<TModel> { public static void ValidateTwoEqualJsonContent(IRestResponse response, TModel modelobj) { var data = JsonConvert.DeserializeObject<TModel>(response.Content); string expected = JsonConvert.SerializeObject(data); var acutal = response.Content; var a = JToken.Parse(expected); var b = JToken.Parse(acutal); JToken res = JsonDifferentiator.Differentiate(a, b); string result = ""; if (res != null) result = res.ToString(); Assert.IsTrue(res == null, "Differerence of expected and actual are: " + result); } }
//Ответ Json
{ "canEdit": true, "allowedTagTypes": [ 0, 1, 4 ], "item": { "query": null, "type": 0, "filter": null, "ldapPaths": null, "editPermissions": 0, "endpointsCount": 0, "id": 404, "disallowed": false, "name": "Tag" }, "filterConfigData": { "config": [ { "type": 5, "id": "Endpoints.ID.SearchInProgress", "extraData": null, "title": "Search in Progress" }, { "source": { "definition": "dbo.func_Reports_GetPoliciesForUser(N'~USER_ID~', N'~USERS_SERVICE_KEY~')", "keyColumn": "Id", "displayColumn": "Name", "type": 1, "name": "Policies" }, "data": null, "type": 3, "id": "Endpoints.ID.Policies", "extraData": null, "title": "Policies" }, { "type": 1, "id": "Endpoints.ID.MACs", "extraData": null, "title": "MAC Addresses" }, { "source": { "definition": "ClientActivityState", "keyColumn": "Id", "displayColumn": "Name", "type": 2, "name": "ClientActivityState" }, "data": [ { "name": "Endpoint Closed", "value": 11 }, { "name": "Endpoint Completed", "value": 15 }, { "name": "Endpoint Opened", "value": 10 }, { "name": "Endpoint Paused", "value": 13 }, { "name": "Endpoint Searching", "value": 12 }, { "name": "Endpoint Stopped", "value": 14 }, { "name": "Executed", "value": 4 }, { "name": "Failed", "value": 5 }, { "name": "None", "value": 0 }, { "name": "Offline", "value": 19 }, { "name": "Search Canceled", "value": 8 }, { "name": "Search Completed", "value": 9 }, { "name": "Search Paused", "value": 7 }, { "name": "Search Started", "value": 6 }, { "name": "Skipped", "value": 3 }, { "name": "Task Acknowledged", "value": 1 }, { "name": "Task Initiated", "value": 2 }, { "name": "Task Paused", "value": 20 }, { "name": "Upgrade Delayed", "value": 17 }, { "name": "Upgrade Failed", "value": 18 }, { "name": "Upgrade Successful", "value": 16 } ], "type": 3, "id": "Endpoints.ID.ClientActivityState", "extraData": null, "title": "Client Activity State" }, { "source": { "definition": "Import.AceType", "keyColumn": "Id", "displayColumn": "Name", "type": 2, "name": "AceType" }, "data": [ { "name": "Allow", "value": 0 }, { "name": "Deny", "value": 1 }, { "name": "System Alarm", "value": 3 }, { "name": "System Audit", "value": 2 } ], "type": 3, "id": "Endpoints.ID.AceType", "extraData": null, "title": "ACL: ACE Type" }, { "type": 1, "id": "Endpoints.ID.AceWho", "extraData": null, "title": "ACL: Trustee" }, { "source": { "definition": "Import.AceRights", "keyColumn": "Id", "displayColumn": "Name", "type": 2, "editor": "Endpoints.AceRightsFilterSelectorViewModel", "name": "AceRights" }, "data": [ { "name": "Append Data", "value": 4 }, { "name": "Delete", "value": 65536 }, { "name": "Execute", "value": 32 }, { "name": "Full Control", "value": 2032127 }, { "name": "Generic Execute", "value": 1179808 }, { "name": "Generic Read", "value": 1179785 }, { "name": "Generic Write", "value": 1179926 }, { "name": "No Access", "value": 0 }, { "name": "Read Acl", "value": 64 }, { "name": "Read Attributes", "value": 128 }, { "name": "Read Control", "value": 131072 }, { "name": "Read Data", "value": 1 }, { "name": "Read Extended Attributes", "value": 8 }, { "name": "Synchronize", "value": 1048576 }, { "name": "Take Ownership", "value": 524288 }, { "name": "Write Acl", "value": 262144 }, { "name": "Write Attributes", "value": 256 }, { "name": "Write Data", "value": 2 }, { "name": "Write Extended Attributes", "value": 16 } ], "type": 3, "id": "Endpoints.ID.AceRights", "extraData": null, "title": "ACL: Authorization" }, { "type": 1, "id": "Endpoints.ID.FilterTagName", "extraData": null, "title": "Tag Name" }, { "source": { "definition": "dbo.func_Reports_GetTagsForUser(N'~USER_ID~', N'~USERS_SERVICE_KEY~')", "keyColumn": "Id", "displayColumn": "Name", "type": 1, "name": "Tags" }, "data": null, "type": 3, "id": "Endpoints.ID.FilterTags", "extraData": null, "title": "Tags" }, { "type": 1, "id": "Endpoints.Name.EndpointName", "extraData": null, "title": "Endpoint Name" }, { "type": 1, "id": "Endpoints.Version.Version", "extraData": null, "title": "Endpoint Version" }, { "type": 1, "id": "Endpoints.Platform.Platform", "extraData": null, "title": "Endpoint Platform" }, { "source": { "definition": "PlatformType", "keyColumn": "Id", "displayColumn": "Name", "type": 2, "name": "PlatformType" }, "data": [ { "name": "Desktop", "value": 1 }, { "name": "Server", "value": 2 }, { "name": "Unknown", "value": 0 } ], "type": 3, "id": "Endpoints.PlatformType.PlatformType", "extraData": null, "title": "Platform Type" }, { "type": 2, "id": "Endpoints.LastPoll.LastPoll", "extraData": null, "title": "Last Poll Time" }, { "type": 1, "id": "Endpoints.EndpointGuid.EndpointGuid", "extraData": null, "title": "Endpoint GUID" }, { "type": 0, "id": "90be0e17-f727-4438-8118-dcf3d3e58c08", "extraData": null, "title": "Match Quantity" }, { "type": 0, "id": "b140b2d4-9de0-4492-acb7-a4f22cdcf394", "extraData": null, "title": "Protected Quantity" }, { "type": 0, "id": "4496ca24-f4a3-4769-b212-fd6b4c029da1", "extraData": null, "title": "Unprotected Quantity" }, { "type": 0, "id": "8add87db-5d70-433e-a885-7bf4296f3405", "extraData": null, "title": "Number of Searches" }, { "type": 2, "id": "4aa90777-a1c5-4d87-890d-01a62469e34a", "extraData": null, "title": "Search Date/Time" } ], "pagedListItems": null }, "forests": null, "adRoots": null }
//Вот мой модельный класс
public class GetTagByIdDTO { public static string ExpectedData(IRestResponse response) { var data = JsonConvert.DeserializeObject<GetTagByIdDTO>(response.Content); var json = JsonConvert.SerializeObject(data); return json; } public class Datum { public static IRestResponse response; public string name { get; set; } public int value { get; set; } } public class Source { public string definition { get; set; } public string displayColumn { get; set; } public string keyColumn { get; set; } public string name { get; set; } public int type { get; set; } public string editor { get; set; } } public class Config { public object extraData { get; set; } public string id { get; set; } public string title { get; set; } public int type { get; set; } public List<Datum> data { get; set; } public Source source { get; set; } } public class FilterConfigData { public List<Config> config { get; set; } public object pagedListItems { get; set; } } public class Item { public bool disallowed { get; set; } public int editPermissions { get; set; } public int endpointsCount { get; set; } public object filter { get; set; } public int id { get; set; } public object ldapPaths { get; set; } public string name { get; set; } public object query { get; set; } public int type { get; set; } } public bool canEdit { get; set; } public List<int> allowedTagTypes { get; set; } public Item item { get; set; } public FilterConfigData filterConfigData { get; set; } public object forets { get; set; } public object adRoots { get; set; } }
//Использование
JsonHelper<GetTagByIdDTO>.ValidateTwoEqualJsonContent(restResponse, GetTagByIdDTO.ExpectedData(restResponse));
//Вот в чем разница
"*filterConfigData": { "*config": [ { "-data": null, "-source": null }, { "*source": { "-editor": null } }, { "-data": null, "-source": null }, { "*source": { "-editor": null } }, { "*source": { "-editor": null } }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "*source": { "-editor": null } }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "*source": { "-editor": null } }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "-data": null, "-source": null }, { "-data": null, "-source": null } ] }, "-forets": null, "+forests": null }
Что я уже пробовал:
Я попробовал другую библиотеку JsonDiffPath из nuget того же вопроса.
Richard MacCutchan
Используйте отладчик для изучения фактического содержимого двух объектов (a и b), чтобы понять, почему они отличаются.