Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

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

(1) ShareShare
ReportReport
Posted on by 93
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 93 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 230,371 Most Valuable Professional 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 93 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 230,371 Most Valuable Professional 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 93 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 230,371 Most Valuable Professional 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).

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans