4

I am trying to serialize some JSON input to a data contract in Microsoft Dynamics 365 Finance. A simple data contract class works fine, but I cannot get data contract extensions to work. Does any one have experience with this or perhaps a working example?

The only related information I managed to find on this topic comes from this forum post. Follow some hyperlinks and you will end up with the official Microsoft documentation (Ref# 199219), which states that this should be supported.

All variations of the data contract attributes below compile fine, but proved unsuccessful for me:

  • Using DataContract and DataMember instead of DataContractAttribute and DataMemberAttribute.
  • Combining DataContract and DataContractAttribute on a single method. (Produces runtime error about double serialization attribute.)
  • Repeating the DataContractAttribute on the extension class.

Additional experiments with the JSON deserializer class through its various constructor options also proved unsuccessful:

  • Passing a list of known types ClassA and ClassA_Extension.
  • Passing a list of known types ClassA_Extension and ClassA (in case the list order had an impact).
  • Passing a settings object and explicitly setting IgnoreExtensionDataObject to false (this appears to be the default).
  • Passing the extension class type as first parameter.

Update

A ticket was raised with Microsoft to investigate the issue. In their response they mentioned that they were able to reproduce this. They also declared that this was "by design" and "will not be fixed".

Our final solution will most likely be the following:

  1. Build a mapping of DataMemberAttribute values and the corresponding data contract method.
  2. Use a JavaScriptSerializer object to turn the JSON into a nested .NET dictionary object.
  3. Iterate over the dictionary object and populate the data contract with the help of the mapping.

Example

Below is a miminal example to demonstrate my issue. The values of the variables value1 and value2 are populated as expected, but variable value3 remains empty.

Data contract

[DataContractAttribute('Class A')]
public class ClassA
{
    protected str value1;
    protected str value2;

    [DataMemberAttribute('Value1')]
    public str value1(str _value1 = value1)
    {
        value1 = _value1;
        return value1;
    }

    [DataMemberAttribute('Value2')]
    public str value2(str _value2 = value2)
    {
        value2 = _value2;
        return value2;
    }

}

Data contract extension

[ExtensionOf(classStr(ClassA))]
public final class ClassA_Extension
{
    private str value3;

    [DataMemberAttribute('Value3')]
    public str value3(str _value3 = value3)
    {
        value3 = _value3;
        return value3;
    }

}

Serialization code with hard-coded input

public class ClassTest
{
    public static void main(Args _args)
    {
        str inputJSON =   @'{
                                "Value1": "abc",
                                "Value2": "def",
                                "Value3": "ghi"
                            }';

        ClassA ret = new ClassA();

        System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding::UTF8.GetBytes(inputJSON));

        System.Runtime.Serialization.Json.DataContractJsonSerializer dcjSer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(ret.GetType());

        ret = dcjSer.ReadObject(ms);

        ms.Close();
    }

}

Result

enter image description here

Sander
  • 3,942
  • 2
  • 17
  • 22

1 Answers1

1

It looks like the serializer is having issues. You might be able to pass a Type array similar to how FormRunConfigurationPropertyClassList does it?

enter image description here

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
  • Thank you for the input! I had a look at the JSON serializer and provided some additional parameters. Unfortunately without success... I have added the things I tried to the question description. – Sander Jan 24 '20 at 09:59
  • It looks like a bug :( – Alex Kwitny Jan 24 '20 at 18:23
  • 1
    That is what I feared. If I get any feedback through a Microsoft ticket, then I will update this question. – Sander Jan 25 '20 at 19:38
  • Microsoft feedback has been added as an update section in the question... – Sander Feb 11 '20 at 09:47
  • 2
    @Sander I tweeted about it to see if anyone in the `AX` community has any additional thoughts or workarounds. May not get traction though. https://twitter.com/AlexOnDAX/status/1227269448949616640?s=20 – Alex Kwitny Feb 11 '20 at 16:34
  • Thank you for the extra effort! Unfortunately Joris' response settles it: serialization through .NET features is not supported. – Sander Feb 12 '20 at 18:07
  • Yup, sounds like it. You'll have to use another method or some of the built in things. – Alex Kwitny Feb 12 '20 at 19:21