GithubHelp home page GithubHelp logo

Comments (17)

304NotModified avatar 304NotModified commented on July 20, 2024 1

PS @axunonb Nice logo!

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

Looks like a duplicate to https://github.com/sinairv/YAXLib/issues/45 which was fixed and closed.

from yaxlib.

304NotModified avatar 304NotModified commented on July 20, 2024

Any idea which version it has been fixed?

from yaxlib.

sinairv avatar sinairv commented on July 20, 2024

I'm not quite sure. It's probably gone out with 2.14 or 2.15.

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

In version 2.15 the following test would not throw a NullReferenceException any more but it would fail. It's not part of the unit tests.
Expected: null
But was: <System.Object>

[Test]
public void DictionaryWithNullValue()
{
    const string theKey = "TheKey";
    var d = new Dictionary<string, object>() {{ theKey, null}};
    var serializer = new YAXSerializer(typeof(Dictionary<string, object>), YAXExceptionHandlingPolicies.DoNotThrow, YAXExceptionTypes.Warning, YAXSerializationOptions.SerializeNullObjects);
    var got = serializer.Serialize(d);
    var gotDes = serializer.Deserialize(got) as Dictionary<string, object>;
    Assert.AreEqual(d[theKey], gotDes[theKey]);
}

from yaxlib.

304NotModified avatar 304NotModified commented on July 20, 2024

@axunonb do you mean it's fixed in 2.15 and unit tested? Or is the unit test not yet in the code?

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

Unfortunately not yet part of the unit tests.

from yaxlib.

304NotModified avatar 304NotModified commented on July 20, 2024

Ah well so if you could PR this unit test, then we can for sure close this one :)

Test looks good :)

@sinairv or should YAXExceptionHandlingPolicies be set to something else?

from yaxlib.

sinairv avatar sinairv commented on July 20, 2024

The test is failing in the build. See AppVeyor logs.

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

Regarding this issue and https://github.com/sinairv/YAXLib/issues/50 I'd like to open a discussion about introducing an YaxIsNullable attribute (similar to this).

The idea expressed in pseudo code was:

Case 1:

Serialization:

if (class member has value of null) && !typeof(string) && !typeof(NullableValueType)
{
    if YAXDontSerializeIfNull
    {
	    do not serialize
    }
    else if YaxIsNullable
    {
        serialize with xsi:nil = "true"    
    }
}

Deserialization:

if ((element for class member is missing) || ((element has xsi:nil = "true") && (member IsNullable))
{
    deserialize as null
}

Case 2

Serialization:

if (class member has value of null) && (typeof(string) || typeof(NullableValueType))
{
    if YAXDontSerializeIfNull
    {
        do not serialize
    }
    else if YaxIsNullable
    {
        serialize with xsi:nil = "true" 
    }
    else
    {
        serialize as empty
    }
}

Deserialization:

if ((element for class member is missing) || (element for class member is empty) || (element has xsi:nil = "true"))
{
   if (member is (typeof(string) || typeof(NullableValueType || has YaxIsNullable))
   {
       deserialize as null
   }
   else
   {
       throw exception
   }
}

For classes implementing IList which have the new YaxIsNullable attribute, null values would be serialized explicitly with xsi:nil = "true", and be deserialized like above.

What do you think?

from yaxlib.

sinairv avatar sinairv commented on July 20, 2024

This seems quite reasonable to me. I particularly liked usage of standard xsi:nil here. I just have a couple of questions/concerns:

  1. I didn't quite get why it should allow strings and nullable value types to produce empty elements, but not for other complex types.
  2. We need to have a strategy for members serialised as attribute. For the case of those members serialised as an XML element adding the xsi:nil attribute is possible. But it's not the case for attributes. One possible strategy (that I personally have mixed feelings about it) is once an attribute is nullable and null, it's going to be turned into an XML element to accomodate the xsi:nil attribute. Not ideal but a possible approach. What do you think?

from yaxlib.

304NotModified avatar 304NotModified commented on July 20, 2024

I like this discussion but IMO it should be in a new issue as it was hard finding it again. This issue should be closed.

Also what about NOT serializing anything for null values? (no element at all)

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

@sinairv

why it should allow strings and nullable value types to produce empty elements

Good point. The idea was that only the YaxIsNullable attribute and null strings should produce the xsi:nil = "true".

But it's not the case for attributes.

Yes, unfortunately, and I share your mixed feelings about turning attributes into elements.

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

Alternative:
What if we dropped decoration with YAXDontSerializeIfNull completely and follow @304NotModified's comment: Turn the default behavior upside down and never serialize null, neither as an element, nor as an attribute. Looks clean and simple, and I really like it. But it would be a breaking change - for a good reason, though.

For those who wanted to serialize null explicitly (which could be appreciated by humans editing XML), following the "upside down" strategy sth. like YAXExplicitlySerializeIfNull could be introduced for elements, leading to serialization with attribute xsi:nil = "true". And for attributes: Making xsi:nil = "true" a "special value" feels as "mixed" as turning attributes into elements, doesn't it? I'd rather have no explicit null for attributes, because it's not covered in XML definition.

from yaxlib.

304NotModified avatar 304NotModified commented on July 20, 2024

@axunonb I see the test DictionaryWithNullValue now succeeds nowadays. I'm doubting if we should close this one

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

@axunonb I see the test DictionaryWithNullValue now succeeds nowadays. I'm doubting if we should close this one

To good to be true - null requires some devotion, see #50 (comment) - leave open for now

from yaxlib.

axunonb avatar axunonb commented on July 20, 2024

@304NotModified Serializing to/from this XML

<DictionaryOfStringObject>
  <KeyValuePairOfStringObject>
    <Key>TheKey</Key>
  </KeyValuePairOfStringObject>
</DictionaryOfStringObject>

works. Value is missing in XML, thus treated as null. All okay, so let's close this issue.

from yaxlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.