{"id":21129510,"url":"https://github.com/guorg/gu.serializationasserts","last_synced_at":"2025-09-14T13:41:07.478Z","repository":{"id":144199495,"uuid":"45867146","full_name":"GuOrg/Gu.SerializationAsserts","owner":"GuOrg","description":null,"archived":false,"fork":false,"pushed_at":"2018-03-02T12:50:09.000Z","size":339,"stargazers_count":3,"open_issues_count":5,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-13T18:28:10.768Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GuOrg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-09T21:13:18.000Z","updated_at":"2018-08-15T10:30:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"eb08140e-7b71-4228-88f9-e486dbe5233e","html_url":"https://github.com/GuOrg/Gu.SerializationAsserts","commit_stats":null,"previous_names":["johanlarsson/gu.serializationasserts"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/GuOrg/Gu.SerializationAsserts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuOrg%2FGu.SerializationAsserts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuOrg%2FGu.SerializationAsserts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuOrg%2FGu.SerializationAsserts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuOrg%2FGu.SerializationAsserts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GuOrg","download_url":"https://codeload.github.com/GuOrg/Gu.SerializationAsserts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuOrg%2FGu.SerializationAsserts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275114387,"owners_count":25407925,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-14T02:00:10.474Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T05:24:24.843Z","updated_at":"2025-09-14T13:41:07.454Z","avatar_url":"https://github.com/GuOrg.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gu.SerializationAsserts\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)\n[![NuGet](https://img.shields.io/nuget/v/Gu.SerializationAsserts.svg)](https://www.nuget.org/packages/Gu.SerializationAsserts/)\n[![NuGet](https://img.shields.io/nuget/v/Gu.SerializationAsserts.Newtonsoft.Json.svg)](https://www.nuget.org/packages/Gu.SerializationAsserts.Newtonsoft.Json/)\n[![Build status](https://ci.appveyor.com/api/projects/status/9vyu94ma5vy25ueo/branch/master?svg=true)](https://ci.appveyor.com/project/JohanLarsson/gu-serializationasserts/branch/master)\n\n## Contents\n\n  - [1. Xml](#1-xml)\n    - [1.1. XmlAssert](#11-xmlassert)\n    - [1.1.1. Equal](#111-equal)\n    - [1.1.2 With custom comparer](#112-with-custom-comparer)\n    - [1.2. XmlSerializerAssert](#12-xmlserializerassert)\n      - [1.2.1. RoundTrip](#121-roundtrip)\n      - [1.2.2. Equal](#122-equal)\n      - [1.1.3. ToXml](#113-toxml)\n      - [1.1.4. FromXml](#114-fromxml)\n    - [1.2. DataContractSerializerAssert](#12-datacontractserializerassert)\n    - [1.2.1 HasDataContractAttribute\u0026lt;T\u0026gt;()](#121-hasdatacontractattributet)\n    - [1.2.1 AllPropertiesHasDataMemberAttributes\u0026lt;T\u0026gt;()](#121-allpropertieshasdatamemberattributest)\n    - [1.3. XmlAssertOptions](#13-xmlassertoptions)\n      - [1.3.1 The options are:](#131-the-options-are)\n  - [2. BinaryFormatterAssert](#2-binaryformatterassert)\n  - [3. JSON](#3-json)\n    - [3.1 JsonAssert](#31-jsonassert)\n    - [3.2 JsonSerializerAssert](#32-jsonserializerassert)\n  - [4. FieldAssert](#4-fieldassert)\n  - [5. FieldComparer\u0026lt;T\u0026gt;](#5-fieldcomparert)\n\n## 1. Xml\n### 1.1. XmlAssert\nClass exposing method for comparing xml strings.\n\n### 1.1.1. Equal\n```c#\nXmlAssert.Equal(expectedXml, actualXml);\n```\nCompares two xml strings. If they are not equal an `AssertException` is thrown. \nThe exception has a message that can give hints about what is wrong, sample:\n\n```\n  Xml differ at line 4 index 21.\n  Expected: 4| \u003cValue Attribute=\"1\"\u003e2\u003c/Value\u003e\n  But was:  4| \u003cValue Attribute=\"1\"\u003eWrong\u003c/Value\u003e\n  ----------------------------------^\n```\n\n### 1.1.2 With custom comparer\n\n```c#\n[Test]\npublic void EqualWithCustomElementComparer()\n{\n    var expected = \"\u003cFoo\u003e\" +\n                   \"  \u003cBar\u003e1\u003c/Bar\u003e\" +\n                   \"\u003c/Foo\u003e\";\n\n    var actual = \"\u003cFoo\u003e\" +\n                 \"  \u003cBar\u003e  1.0  \u003c/Bar\u003e\" +\n                 \"\u003c/Foo\u003e\";\n    XmlAssert.Equal(expected, actual, new ElementDoubleValueComparer(), XmlAssertOptions.IgnoreDeclaration | XmlAssertOptions.IgnoreNamespaces);\n}\n\nprivate class ElementDoubleValueComparer : IEqualityComparer\u003cXElement\u003e\n{\n    public bool Equals(XElement x, XElement y)\n    {\n        return double.Parse(x.Value, CultureInfo.InvariantCulture) == double.Parse(y.Value, CultureInfo.InvariantCulture);\n    }\n\n    int IEqualityComparer\u003cXElement\u003e.GetHashCode(XElement obj)\n    {\n        throw new System.NotImplementedException();\n    }\n}\n```\n\n### 1.2. XmlSerializerAssert\nA collection of helpers for tests using [XmlSerializer](https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx). \n\nTo test that an instance can be roundtripped use:\n\n#### 1.2.1. RoundTrip\n1. Serializes actual to xml\n2. Deserializes xml \n3. Compares the instances using `FieldAsser.Equal`\n4. Creates a `ContainerClass\u003cT\u003e` and roundtrips it. The reason we do this is to catch errors with ReadEndElement or reading outside the end of the element.\n5. Returns the roundtripped instance in case you want to do additional asserts.\n\n```c#\nvar actual = new Dummy { Value = 2 };\nXmlSerializerAssert.RoundTrip(actual);\n```\n\n#### 1.2.2. Equal\n\nTo assert that serialization produces the expected xml and that the instance can be roundtripped use:\n```c#\nvar actual = new Dummy { Value = 2 };\nvar expectedXml = \"\u003cDummy\u003e\\r\\n\" +\n                  \"  \u003cValue\u003e2\u003c/Value\u003e\\r\\n\" +\n                  \"\u003c/Dummy\u003e\";\nvar roundtrip = XmlSerializerAssert.Equal(expectedXml, actual, XmlAssertOptions.IgnoreNamespaces | XmlAssertOptions.IgnoreDeclaration);\n```\n\n1. Serializes actual to xml\n2. Calls XmlAssert.Equal(expectedXml, actual, options)\n3. Calls XmlSerializerAssert.Roundtrip(actual)\n4. Returns the roundtripped instance in case you want to do additional asserts.\n\nTo compare equality of the xml produced when serializing two instances use:\n\n```c#\nvar expected = new Dummy { Value = 2 };\nvar actual = new Dummy { Value = 2 };\nXmlSerializerAssert.Equal(expected, actual);\n```\nChecks if two instances produces the same xml. Useful for deep equals.\n\n1. Serializes expected and actual\n2. Calls XmlAssert.Equal(expectedXml, actualXml)\n\n#### 1.1.3. ToXml\nSerialize an object:\n```c#\nvar dummy = new Dummy { Value = 2 };\nstring xml = XmlSerializerAssert.ToXml(dummy);\n```\n\n#### 1.1.4. FromXml\nDeserialize xml to an object:\n```c#\nvar foo = XmlSerializerAssert.FromXml(xml);\n```\n\n### 1.2. DataContractSerializerAssert \nSame as XmlSerializerAssert but uses [DataContractSerializer](https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer(v=vs.110).aspx)\n\n### 1.2.1 HasDataContractAttribute\u0026lt;T\u0026gt;()\nChecks that T has [DataContract]\n\n### 1.2.1 AllPropertiesHasDataMemberAttributes\u0026lt;T\u0026gt;()\nChecks that all properties for T has the [DataMember] attribute.\n\n    \n### 1.3. XmlAssertOptions\nUse `XmlAssertOptions` to specify how the the xml is compared. \nSample:\n\n```c#\nXmlAssert.Equal(expected, actual, XmlAssertOptions.IgnoreDeclaration | XmlAssertOptions.IgnoreNamespaces);\n```\n#### 1.3.1 The options are:\n- Verbatim, the default and strictest mode.\n- IgnoreDeclaration, ignore Xml declaration when comparing.\n- IgnoreNamespaces, perhaps useful for less verbose tests.\n- IgnoreElementOrder\n- IgnoreAttributeOrder\n- IgnoreOrder = IgnoreElementOrder | IgnoreAttributeOrder\n\n## 2. BinaryFormatterAssert \nSame as XmlSerializerAssert but uses [BinaryFormatter](https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx)\n\n## 3. JSON\n\n### 3.1 JsonAssert       \nSame as XmlAssert but for JSON.\n      \n### 3.2 JsonSerializerAssert       \nSame as XmlSerializerAssert but uses [JsonSerializer](http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonserializer.htm)\n\n## 4. FieldAssert\nChecks that all fields are structurally equal recursively. Compares `IEquatable\u003cT\u003e` types using `object.Equals()`.\nCompares collections by element.\n\n```c#\nvar d1 = new Dummy { Value = 1 };\nvar d2 = new Dummy { Value = 1 };\nFieldAssert.Equal(d1, d1);\n```\n\nIf assert fails the output looks like this:\n```\nFound this difference between expected and actual:\nexpected[1].foo[2].bar.baz: 5\n  actual[1].foo[2].bar.baz: 2\n```\n\n## 5. FieldComparer\u0026lt;T\u0026gt;\nNUnit's CollectionAssert takes an IComparer\n\n```c#\nCollectionAssert.AreEqual(actual, expected, FieldComparer.For\u003cFoo\u003e());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguorg%2Fgu.serializationasserts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguorg%2Fgu.serializationasserts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguorg%2Fgu.serializationasserts/lists"}