Example #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
        }
Example #2
0
        private void TryRaiseTweetCreatedEvents(string eventName, JObject jsonObjectEvent)
        {
            var json = jsonObjectEvent.ToString();
            var tweetCreatedEvent     = jsonObjectEvent[eventName];
            var tweetCreatedEventJson = tweetCreatedEvent.ToString();
            var tweetDTOs             = _jsonObjectConverter.Deserialize <ITweetDTO[]>(tweetCreatedEventJson);

            tweetDTOs.ForEach(tweetDTO =>
            {
                var tweet = _factories.CreateTweet(tweetDTO);

                var accountActivityEvent = new AccountActivityEvent <ITweet>(tweet)
                {
                    AccountUserId = AccountUserId,
                    EventDate     = tweet.CreatedAt,
                    Json          = json
                };

                var eventArgs = new TweetCreatedEvent(accountActivityEvent);
                this.Raise(TweetCreated, eventArgs);

                if (eventArgs.InResultOf == TweetCreatedRaisedInResultOf.Unknown)
                {
                    this.Raise(EventKnownButNotFullySupportedReceived, new EventKnownButNotSupported(json, eventArgs));
                }
            });
        }
Example #3
0
        private T ConvertJsonTo <T>(string json, IJsonConverter converter) where T : class
        {
            var type = typeof(T);

            try
            {
                if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    Type genericType = null;

                    if (type.GetTypeInfo().IsGenericType)
                    {
                        genericType = type.GetGenericArguments()[0];
                    }
                    else if (typeof(Array).IsAssignableFrom(type))
                    {
                        genericType = type.GetElementType();
                    }

                    converter ??= GetSerializerFromNonCollectionType(genericType);
                    if (genericType != null && converter != null)
                    {
                        var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(genericType));

                        JArray jsonArray = JArray.Parse(json);

                        foreach (var elt in jsonArray)
                        {
                            var eltJson = elt.ToString();
                            list.Add(converter.Deserialize(eltJson));
                        }

                        if (typeof(Array).IsAssignableFrom(type))
                        {
                            var array = Array.CreateInstance(genericType, list.Count);
                            list.CopyTo(array, 0);
                            return(array as T);
                        }

                        return(list as T);
                    }
                }

                converter ??= GetSerializerFromNonCollectionType(type);

                if (converter != null)
                {
                    return(converter.Deserialize(json) as T);
                }

                return(_jsonObjectConverter.Deserialize <T>(json));
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "The type provided is probably not compatible with Tweetinvi Json serializer." +
                          "If you think class should be deserializable by default please report on github.com/linvi/tweetinvi.",
                          ex
                          );
            }
        }
        public ITwitterList CreateTwitterList(string json)
        {
            var listDTO = _jsonObjectConverter.Deserialize <ITwitterListDTO>(json);

            return(CreateTwitterList(listDTO));
        }