Skip to content

Serialization and Deserialization

linvi edited this page May 19, 2016 · 8 revisions

Overview

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

Serialization

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

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 an Exception.

Clone this wiki locally