web
You’re offline. This is a read only version of the page.
close
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
I have the same question (0)
  • Martin Dráb Profile Picture
    237,959 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
    238 on at

    Hey Martin,

    Sorry for missunderstanding.

    Dynamics 365 FO

  • Martin Dráb Profile Picture
    237,959 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
    238 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
    237,959 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
    238 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 478 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans