-
Notifications
You must be signed in to change notification settings - Fork 220
Serialization and Deserialization
As part of release 0.9.13.0, Tweetinvi will partially support serialization and deserialization. I would like to express a special thanks to @tsconn23 for his help on the matter.
As this is an ongoing process, you can check the progress with the following issues: #75 Enabled Models to be Serializable
Tweetinvi 1.1 contains a new static Tweetinvi.JsonSerializer
static class.
This class has been designed to simplify the serialization/deserialization of Tweetinvi objects.
Serializing objects is very easy, you simply have to call the following method :
var tweets = Timeline.GetHomeTimeline();
var json = Tweetinvi.JsonSerializer.ToJson(tweets);
The .ToJson
is an extension methods for any type of objects.
var json = tweets.ToJson();
To deserialiaze json into objects you can use the ConvertJsonTo
method.
var tweetDTOs = Tweetinvi.JsonSerializer.ConvertJsonTo<ITweetDTO[]>(json);
var tweets = Tweet.GenerateTweetsFromDTO(tweetDTOs);
Again you can access this via the .ConvertJsonTo
string extension method.
var tweetDTOs = json.ConvertJsonTo<ITweetDTO[]>();
var tweets = Tweet.GenerateTweetsFromDTO(tweetDTOs);
Tweetinvi comes with all the default classes. But you might want to be able to deserialize your own class or any class not supported by the serializer. To help people with this problem we added the .Map
static method that give people the ability to specify how the serialization/deserialization should work for your class.
// This example shows that the serializer should use the `TweetDTO` property of `ITweet` to serialize.
Tweetinvi.JsonSerializer.Map<ITweet, ITweetDTO>(u => u.TweetDTO, Tweet.TweetFactory.GenerateTweetFromJson);
-
u => u.TweetDTO
informs Tweetinvi that it should should use the -
TweetDTO
property ofITweet
to serialize.Tweet.TweetFactory.GenerateTweetFromJson
informs Tweetinvi that it should useGenerateTweetFromJson
to create an ITweet from a DTO.
The library currently supports IUserDTO
and ITweetDTO
serialization into json
.
To do so please use Newtonsoft.Json
which you already need to use to run the Tweetinvi.
// Serialize an ITweetDTO
ITweet tweet = ...;
var json = JsonConvert.SerializeObject(tweet.TweetDTO);
// Serialize an IUserDTO
IUser user = ...; // or IAuthenticatedUser
var json = JsonConvert.SerializeObject(user.UserDTO);
Deserialization is also supported for IUserDTO
and ITweetDTO
.
To use it you will have to use the JsonPropertiesConverterRepository
converters.
// Deserialize as class `UserDTO`
var userDTO = JsonConvert.DeserializeObject<UserDTO>(json, JsonPropertiesConverterRepository.Converters);
// Deserialize as an interface `ITweetDTO`
var tweetDTO = JsonConvert.DeserializeObject<ITweetDTO>(json, JsonPropertiesConverterRepository.Converters);
Note :
JsonPropertiesConverterRepository
is a static class containing all the converters required by Tweetinvi for the deserialization. Without specifying it, the deserialization will most probably throw anException.