web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

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

X++ FormJsonSerializer

(0) ShareShare
ReportReport
Posted on by 240

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
I have the same question (0)
  • Martin Dráb Profile Picture
    239,998 Most Valuable Professional on at

    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?

  • Ferhat.S Profile Picture
    240 on at

    Hey Martin,

    Sorry for missunderstanding.

    Dynamics 365 FO

  • Martin Dráb Profile Picture
    239,998 Most Valuable Professional on at

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

  • Mariano Gracia Profile Picture
    on at

    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;
        }
    
    }

  • Ferhat.S Profile Picture
    240 on at

    Hey Mariano,

    Thank you for your comment.

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

  • Martin Dráb Profile Picture
    239,998 Most Valuable Professional on at

    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
    240 on at

    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 ?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 414 Super User 2026 Season 1

#2
Subra Profile Picture

Subra 352

#3
Martin Dráb Profile Picture

Martin Dráb 275 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans