Need help about loadstaDataFromString

Saeed Pooladzadeh 231 Reputation points
2021-09-21T10:43:31.817+00:00

Hello,

If I use blazor:

I should use loadStateDataForm Stream or loadStateDataForm String?

using (var fs = File.OpenRead(stateFile))
                {
                    //_instaApi.LoadStateDataFromStream(fs);
                    _instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());

                }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,235 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-09-22T02:05:26.74+00:00

    Based on your previous thread, I think you are using InstagramApiSharp.

    We can see from its source code that LoadStateDataFromString will directly call the SerializationHelper.DeserializeFromString method, while LoadStateDataFromStream will go through some judgments and then call DeserializeFromString under certain conditions.

    In view of the other possibilities preset by the author in the LoadStateDataFromStream method, and what you get now is a Stream, I recommend using LoadStateDataFromStream.

            public void LoadStateDataFromStream(Stream stream)  
            {  
                var data = SerializationHelper.DeserializeFromStream<StateData>(stream);  
                LoadStateDataFromObject(data);  
            }  
            /// <summary>  
            ///     Set state data from provided json string  
            /// </summary>  
            public void LoadStateDataFromString(string json)  
            {  
                var data = SerializationHelper.DeserializeFromString<StateData>(json);  
                LoadStateDataFromObject(data);  
            }  
    

    SerializationHelper Class


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Saeed Pooladzadeh 231 Reputation points
    2021-10-05T00:12:41.367+00:00

    When I use " loadStateFromStream" ,
    When I run nothing happens and just I see:
    this message: Message: Loading state from file
    and there is no exception.

    if (File.Exists(stateFile))
    
                {
    
                    message = "Loading state from file";
    
                    using (var fs = File.OpenRead(stateFile))
    
                    {
    
                        _instaApi.LoadStateDataFromStream(fs);
    
                    }
                }
            }
    

    When I use "_instaApi.LoadStateDataFromString"

    _instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
    
    
    
    if (File.Exists(stateFile))
                {
    
                    message = "Loading state from file";
    
                    using (var fs = File.OpenRead(stateFile))
                    {
                       _instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
    
                    }
                }
    

    I encounter this exception:

    ExceptionMessage: error: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 1, position 1. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.ReadAndMoveToContent() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at InstagramApiSharp.API.InstaApi.LoadStateDataFromString(String json) at InstaSite1.Pages.Enterance.StartInstagram() in

    I'm doing my project in blazor and the API recommends to
    "loadStateFromString"

    What is your idea?

    Saeed

    0 comments No comments