Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(De)serialization of objects with property-name same as object-name stopped working correctly since V3.2

See original GitHub issue

Given the classes

public class Person
    public string LastName { get; set; } = string.Empty;
    public string FirstName { get; set; } = string.Empty;
    public string Salutation { get; set; } = string.Empty;
public class Item
    public string Description { get; set; } = string.Empty;
    public string Person { get; set; } = string.Empty;
    public byte[] Content { get; set; }

the code

var item = new Item();
item.Person = "Testperson";
item.Description = "Testing ExtendedXmlSerializer";
item.Content = new byte[] { 12, 13, 14, 15, 16 };

string content = serializer.Serialize(new XmlWriterSettings { Indent = true }, item);

var deserializedItem = serializer.Deserialize<Item>(new XmlReaderSettings(), content);

results in deserializedItem.Person being string.Empty and deserializedItem.Content being null

The resulting xml after serialization looks like

<?xml version="1.0" encoding="utf-8"?>
<Item xmlns="clr-namespace:TestingRetryItem;assembly=TestingRetryItem">
  <Description>Testing ExtendedXmlSerializer</Description>
  <Person xmlns:exs="" exs:member="">Testperson</Person>

The problem here seems to be exs:member="" because after manually deleting this from the resulting xml the deserialization works. There is no exception thrown neither at serialization nor deserialization. The order of the properties is relevant as well, because e.g having the Item class look like

public class Item
    public string Description { get; set; } = string.Empty;
    public byte[] Content { get; set; }
    public string Person { get; set; } = string.Empty;

the Content property isn’t null anymore. It just seems that after accessing a property with a name equal to a class-name the deserialization stops without throwing an exception.

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

Heslachercommented, Jan 25, 2023

You don’t need to apologize. I have fixed the issue by using EnableReferences() for serialization and omitting it for deserialization. I have tested it and it works. Thanks again for your help and this wonderful project.

Mike-E-angelocommented, Jan 25, 2023

Thank you very much! I appreciate that. I definitely feel beholden to this project and all the developers that depend on it, so when things don’t work I take that responsibility seriously.

I like your other solution as well. I didn’t even think of that, and hopefully, if someone in the future runs into this problem they have a couple of workarounds they can possibly implement.

Anyways, thank you for your understanding and kind words. You made my day. 🙏 Closing for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonProperty - Use different name for deserialization, but ...
I am retrieving JSON from an API. I am using newtonsoft (this is right?) to deserialize this into a list of objects....
Read more >
One naïve man's struggle with TypeScript class serialization
JavaScript seems to have been built with serialization inherent in the language: JSON is a first-class way to serialize and represent JavaScript objects....
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & ...
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
Read more >
How to serialize properties of derived classes with System. ...
Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET.
Read more >
Serialization and Deserialization in Java with Example
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found