Search code examples
c#jsontwittertweetinvi

How can I convert Tweetinvi's args.json to object


I am using Tweetinvi to get Twitter stream, quite easy/friendly to use but unable to parse Json to a Tweet. Can anybody help me converting/parsing args.Json to a Tweet (using Newtonsoft.json)?

filteredStream.JsonObjectReceived += (sender, args) =>
{
    var tweet = JsonConvert.DeserializeObject<Tweetinvi.Core.Interfaces.ITweet>(args.Json);
};

Above line ends up with following Error because ITweet is an interface:

Could not create an instance of type Tweetinvi.Core.Interfaces.ITweet. Type is an interface or abstract class and cannot be instantiated

Solution

  • Just use the method that is already implemented:

     fs.MatchingTweetReceived += (sender, args) => { var tweet = args.Tweet; };
    

    If you really want to do the Json conversion on your own you can do the following:

     var jsonConvert = TweetinviContainer.Resolve<IJsonObjectConverter>();
     var tweetDTO = jsonConvert.DeserializeObject<ITweetDTO>(json);
     var tweet = Tweet.GenerateTweetFromDTO(tweetDTO);