示例#1
0
        public TweetinviJsonConverter(ITwitterClient client, IJsonObjectConverter jsonObjectConverter)
        {
            _jsonObjectConverter = jsonObjectConverter;
            var factories = client.Factories;

            _defaultSerializers = new Dictionary <Type, IJsonConverter>();
            _serializers        = new Dictionary <Type, Dictionary <Type, IJsonConverter> >();

            // ReSharper disable RedundantTypeArgumentsOfMethod
            Map <ITweet, ITweetDTO>(tweet => tweet.TweetDTO, factories.CreateTweet);
            Map <IUser, IUserDTO>(user => user.UserDTO, factories.CreateUser);
            Map <IAuthenticatedUser, IUserDTO>(user => user.UserDTO, factories.CreateAuthenticatedUser);
            Map <IAccountSettings, IAccountSettingsDTO>(accountSettings => accountSettings.AccountSettingsDTO, factories.CreateAccountSettings);
            Map <IMessage, IMessageEventDTO>(message => message.MessageEventDTO, factories.CreateMessage);
            Map <IMessage, IMessageEventWithAppDTO>(message =>
            {
                return(new MessageEventWithAppDTO
                {
                    MessageEvent = message.MessageEventDTO,
                    App = message.App
                });
            }, factories.CreateMessageFromMessageEventWithApp);
            Map <ITwitterList, ITwitterListDTO>(list => list.TwitterListDTO, factories.CreateTwitterList);

            Map <ITwitterCredentials, ITwitterCredentials>(credentials => credentials, factories.CreateTwitterCredentials);
            Map <ITwitterCredentials, IConsumerOnlyCredentials>(credentials => new ConsumerOnlyCredentials(credentials), factories.CreateTwitterCredentials);
            Map <IConsumerOnlyCredentials, IConsumerOnlyCredentials>(credentials => credentials, factories.CreateConsumerCredentials);
            Map <IConsumerOnlyCredentials, ITwitterCredentials>(credentials => new TwitterCredentials(credentials), factories.CreateConsumerCredentials);

            Map <IMedia, IMedia>(media => media, factories.CreateMedia);
            Map <IUploadedMediaInfo, IUploadedMediaInfo>(mediaInfo => mediaInfo, factories.CreateUploadedMediaInfo);

            Map <ISearchResults, SearchResultsDTO>(result =>
            {
                return(new SearchResultsDTO
                {
                    TweetDTOs = result.Tweets.Select(tweetWithSearchMetadata =>
                    {
                        var tweetDTOJson = _jsonObjectConverter.Serialize(tweetWithSearchMetadata.TweetDTO);
                        var tweetWithSearchMetadataDTO = _jsonObjectConverter.Deserialize <TweetWithSearchMetadataDTO>(tweetDTOJson);
                        tweetWithSearchMetadataDTO.TweetFromSearchMetadata = tweetWithSearchMetadata.SearchMetadata;
                        return tweetWithSearchMetadataDTO;
                    }).ToArray(),
                    SearchMetadata = result.SearchMetadata
                });
            }, factories.CreateSearchResult);
            Map <ICredentialsRateLimits, CredentialsRateLimitsDTO>(rateLimits => rateLimits.CredentialsRateLimitsDTO, factories.CreateRateLimits);
            Map <ITwitterConfiguration, ITwitterConfiguration>(config => config, factories.CreateTwitterConfiguration);
            Map <ISavedSearch, ISavedSearchDTO>(savedSearch => savedSearch.SavedSearchDTO, factories.CreateSavedSearch);
            Map <IOEmbedTweet, IOEmbedTweetDTO>(oEmbedTweet => oEmbedTweet.OembedTweetDTO, factories.CreateOEmbedTweet);
            Map <IRelationshipDetails, IRelationshipDetailsDTO>(relationshipDetails => relationshipDetails.RelationshipDetailsDTO, factories.CreateRelationshipDetails);
            Map <IRelationshipState, IRelationshipStateDTO>(r => r.RelationshipStateDTO, factories.CreateRelationshipState);
            Map <IWebhook, IWebhookDTO>(env => env.WebhookDTO, factories.CreateWebhook);
            Map <IWebhookEnvironment, IWebhookEnvironmentDTO>(env => env.WebhookEnvironmentDTO, factories.CreateWebhookEnvironment);
            Map <IWebhookEnvironmentSubscriptions, IWebhookEnvironmentSubscriptionsDTO>(env => env.WebhookEnvironmentSubscriptionsDTO, factories.CreateWebhookEnvironmentSubscriptions);
            // ReSharper restore RedundantTypeArgumentsOfMethod
        }
示例#2
0
        private string ToJson <T>(T obj, IJsonConverter converter)
        {
            var    type        = typeof(T);
            object toSerialize = obj;

            if (obj is IEnumerable enumerable)
            {
                var genericType = type.GetElementType() ?? type.GetGenericArguments()[0];

                converter ??= GetSerializerFromNonCollectionType(genericType);

                if (converter != null)
                {
                    var list = new List <object>();

                    foreach (var o in enumerable)
                    {
                        list.Add(converter.GetObjectToSerialize(o));
                    }

                    toSerialize = list;
                }
            }
            else
            {
                converter ??= GetSerializerFromNonCollectionType(type);
                if (converter != null)
                {
                    toSerialize = converter.GetObjectToSerialize(obj);
                }
            }

            try
            {
                return(_jsonObjectConverter.Serialize(toSerialize));
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "The type provided is probably not compatible with Tweetinvi Json serializer." +
                          "If you think class should be serializable by default please report on github.com/linvi/tweetinvi.",
                          ex
                          );
            }
        }
        public StringContent Create <T>(T content, JsonConverter[] converters)
        {
            var jsonBody = _jsonObjectConverter.Serialize(content, converters);

            return(new StringContent(jsonBody, Encoding.UTF8, "application/json"));
        }