Exemplo n.º 1
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
                          );
            }
        }