Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

X++ FormJsonSerializer

(0) ShareShare
ReportReport
Posted on by 238

Hey,

I have a problem with serializing json string into object.

I have a example class like 

[DataContractAttribute]
public class MyObject
{
    boolean myBool=true;
    str     description;


    /// 
    ///
    /// 
    public void new()
    {
    }

    [
        DataMemberAttribute('myBool'),
        AifCollectionTypeAttribute('myBool', Types::Integer),
        AifCollectionTypeAttribute('return', Types::Integer)
    ]
    public boolean parmMyBool(boolean _myBool = myBool)
    {
        
        myBool = _myBool;
        
        return myBool;
    }

    [
        DataMemberAttribute('description'),
        AifCollectionTypeAttribute('description', Types::String),
        AifCollectionTypeAttribute('return', Types::String)
    ]
        public str parmDescription(str _description = description)
    {
        description = _description;
        return description;
    }

}

When i try to serialize 

string like 

"strjson": "{\"myBool\":false,\"description\":\"desc1234\"}"
 
the boolean value is not setting. Other hand when i change strjson into  "strjson": "{\"myBool\":\"false\",\"description\":\"desc1234\"}" working totaly fine, 
I digged into code and found  : FormJsonserializer class and method named "deserializeObjectInternal()" at 744
else if (jsonReader.TokenType == Newtonsoft.Json.JsonToken::Boolean ||
    jsonReader.TokenType == Newtonsoft.Json.JsonToken::Date ||
    jsonReader.TokenType == Newtonsoft.Json.JsonToken::Float ||
    jsonReader.TokenType == Newtonsoft.Json.JsonToken::Integer ||
    jsonReader.TokenType == Newtonsoft.Json.JsonToken::String)
{
    // This is the case for basic type values
    if(jsonReader.Value && deserializedObject && currentJsonProperty)
    {
    }
}
jsonReader.Value comes = false but logic operator should be control it has a value or not. How can i fix that issue anyone can help me about it ?
 Version : Dyanmics 365 FO
  • Ferhat.S Profile Picture
    238 on at
    RE: X++ FormJsonSerializer

    Hey Martin,

    I might be change the design but its a lot afford. But other hand i tried other things like

    [DataContractAttribute]
    public class MyObject
    {
        boolean myBool;
        str     description;
        int     myNumber;
    
    
        /// 
        ///
        /// 
        public void new()
        {
            myBool = true;
            myNumber = 1;
        }
    
        [
            DataMemberAttribute('myBool')
        ]
        public boolean parmMyBool(boolean _myBool = myBool)
        {
            
            myBool = _myBool;
            
            return myBool;
        }
    
        [
            DataMemberAttribute('description')
        ]
        public str parmDescription(str _description = description)
        {
            description = _description;
            return description;
        }
        
        [
            DataMemberAttribute('myNumber')
        ]
        public int parmMyNumber(int _myNumber = myNumber)
        {
            myNumber = _myNumber;
            return myNumber;
        }
    
    }

    also we cant set myNumber value to "0"  due to same issue.

    I found a a bit weired way to make it work  but its not feels safe.

    [ExtensionOf(classStr(FormJsonSerializer))]
    final class MyFormJsonSerializer_Extension
    {
        public static Object deserializeObject(ClassId _objectTypeId, str _serializedValue)
        {
            Object deserializedObject;
            _serializedValue = strReplace(_serializedValue,":false",':"false"');
            _serializedValue = strReplace(_serializedValue,":false,",':"false",');
    
            deserializedObject = next deserializeObject(_objectTypeId,_serializedValue);
    
            return deserializedObject;
        }
    
    }

    Is that make a sense copying full Form JsonSerializer class into custom class and change code ?

  • Martin Dráb Profile Picture
    231,923 Most Valuable Professional on at
    RE: X++ FormJsonSerializer

    Can't you simply keep 'false' as the default values of myBool? Then if false is received or myBool isn't included in JSON at all, the contract will contain false, as expected.

  • Ferhat.S Profile Picture
    238 on at
    RE: X++ FormJsonSerializer

    Hey Mariano,

    Thank you for your comment.

    But its not working. Still "jsonReader.Value" it has a value and its false(boolean).

  • Mariano Gracia Profile Picture
    on at
    RE: X++ FormJsonSerializer

    You don't have to specify AifCollectionTypeAttribute for base types like boolean, string, integer, real and so on... remove the entries with AifCollectionTypeAttribute and it should work

    [DataContractAttribute]
    public class MyObject
    {
        boolean myBool;
        str     description;
    
    
        /// 
        ///
        /// 
        public void new()
        {
            myBool = true;
        }
    
        [
            DataMemberAttribute('myBool')
        ]
        public boolean parmMyBool(boolean _myBool = myBool)
        {
            
            myBool = _myBool;
            
            return myBool;
        }
    
        [
            DataMemberAttribute('description')
        ]
        public str parmDescription(str _description = description)
        {
            description = _description;
            return description;
        }
    
    }

  • Martin Dráb Profile Picture
    231,923 Most Valuable Professional on at
    RE: X++ FormJsonSerializer

    Moved to Dynamics 365 Finance forum. Excessive indentation removed from the second code snippet.

  • Ferhat.S Profile Picture
    238 on at
    RE: X++ FormJsonSerializer

    Hey Martin,

    Sorry for missunderstanding.

    Dynamics 365 FO

  • Martin Dráb Profile Picture
    231,923 Most Valuable Professional on at
    RE: X++ FormJsonSerializer

    When you saying that your version of AX is "current", do you mean Dynamics AX 2012 R3? Or do you actually mean D365FO instead of Dynamics AX?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,209 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,923 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans