Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
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 229,275 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 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 229,275 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 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 229,275 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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans