Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / System.Text.Json JsonS...
Finance forum
Unanswered

System.Text.Json JsonSerializer::Deserialize(string, type) return empty

Posted on by 79
Hi experts,
 
I am calling the API from X++ Dynamics 365 FO. After the POST request is sent, the API will return a JSON with 4 variables to me, which are Access Token, Expires In, Token Type and Scope. However, when I tried to deserialize it, it returns empty as shown in the debug output window.
 
I would like to ask how can I deserialize the JSON string in X++.
 
 
I have the Token class written in X++ as follow:
using System.Attribute;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Token
{
    public str accessToken;
    str tokenType;
    int expiresIn;
    str scope;
    [JsonPropertyName(/access_token/)]
    public str AccessToken(str _accessToken = accessToken)
    {
        accessToken = _accessToken;
        return accessToken;
    }
    [JsonPropertyName(/token_type/)]
    public str TokenType(str _tokenType = tokenType)
    {
        tokenType = _tokenType;
        return tokenType;
    }
    [JsonPropertyName(/expires_in/)]
    public int ExpiresIn(int _expiresIn = expiresIn)
    {
        expiresIn = _expiresIn;
        return expiresIn;
    }
    [JsonPropertyName(/scope/)]
    public str Scope(str _scope = scope)
    {
        scope = _scope;
        return scope;
    }
}
  • Yue Zhen Profile Picture
    Yue Zhen 79 on at
    System.Text.Json JsonSerializer::Deserialize(string, type) return empty
    It is a StreamReader.
     
    System.Net.WebRequest webReq = WebRequest::Create(strFmt("https://%1/connect/token", ads_EinvParms.ads_BaseURL));
    System.IO.Stream streamStr, responseStr;
    System.IO.StreamWriter streamWriter;
    System.Net.WebResponse webResponse;
    System.IO.StreamReader reader;
  • Martin Dráb Profile Picture
    Martin Dráb 224,463 Super User on at
    System.Text.Json JsonSerializer::Deserialize(string, type) return empty
    Yes, your code assumes it's a JSON string. But what is the actual value? If we want to understand why the string failed to parse, we need to know what data we're talking about.
  • Yue Zhen Profile Picture
    Yue Zhen 79 on at
    System.Text.Json JsonSerializer::Deserialize(string, type) return empty
    Good morning, Sir Martin

    System.Text.Json.JsonSerializerOptions options;
    System.Net.WebRequest webReq = WebRequest::Create(strFmt("https://%1/connect/token", ads_EinvParms.ads_BaseURL));
    System.IO.Stream streamStr, responseStr;
    System.IO.StreamWriter streamWriter;
    System.Net.WebResponse webResponse;
    System.IO.StreamReader reader;
    webReq.set_Method("POST");
    webReq.set_ContentType('application/x-www-form-urlencoded');
    streamStr = webReq.GetRequestStream();
    streamWriter = new System.IO.StreamWriter(streamStr);
    streamWriter.Write(strFmt("client_id=%1&client_secret=%2&grant_type=client_credentials&scope=InvoicingAPI", ads_EinvParms.ads_ClientID, ads_EinvParms.ads_ClientSecret));
    streamWriter.Flush();
    streamWriter.Close();
    streamWriter.Dispose();
    webResponse = webReq.GetResponse();
    responseStr = webResponse.GetResponseStream();
    reader = new System.IO.StreamReader(responseStr);
    info(reader.ReadToEnd());

    The value of the first parameter I put is reader.ReadToEnd(), it should be a string.
  • Martin Dráb Profile Picture
    Martin Dráb 224,463 Super User on at
    System.Text.Json JsonSerializer::Deserialize(string, type) return empty
    What is the value of the first parameter?
  • Yue Zhen Profile Picture
    Yue Zhen 79 on at
    System.Text.Json JsonSerializer::Deserialize(string, type) return empty
    I declare the option and add to JsonSerializer.Deserializer. 
    options = new System.Text.Json.JsonSerializerOptions();
    options.NumberHandling = JsonNumberHandling::AllowReadingFromString;
    myToken = System.Text.Json.JsonSerializer::Deserialize(reader.ReadToEnd(), myToken.GetType(), options);
     
    however, it return the error states that "System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
  • Martin Dráb Profile Picture
    Martin Dráb 224,463 Super User on at
    System.Text.Json JsonSerializer::Deserialize(string, type) return empty
    According to documentation of Deserialize(), there is no overload accepting String and Type. Use one of the existing overloads, e.g. Deserialize(String, Type, JsonSerializerContext).

Helpful resources

Quick Links

Can you answer this forum question?

You could make someone's day!

Community Newsletter - May 2024

Kudos to our community stars!

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,623 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 224,463 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans