Example #1
0
        public static string JsonSerialize <T>(this T obj,
                                               IApplication httpApp, IHttpRequest request)
        {
            var converter = new Serialization.ExtrudeConvert(request, httpApp);
            var jsonObj   = JsonConvert.SerializeObject(obj,
                                                        new JsonSerializerSettings
            {
                Converters = new JsonConverter[] { converter }.ToList(),
            });

            return(jsonObj);
        }
Example #2
0
        public async Task SerializeAsync(Stream responseStream,
                                         IApplication httpApp, IHttpRequest request,
                                         ParameterInfo paramInfo, object obj)
        {
            var jsonCastersHttpApp = httpApp
                                     .GetType()
                                     .GetAttributesInterface <ICastJson>()
                                     .ToArray();

            var encoding = request.TryGetAcceptCharset(out Encoding writerEncoding) ?
                           writerEncoding
                :
                           new UTF8Encoding(false);

            using (var textWriter = new StreamWriter(responseStream, encoding))
            {
                var jsonWriter = new JsonTextWriter(textWriter);

                var serializer = new JsonSerializer();
                var converter  = new Serialization.ExtrudeConvert(request, httpApp);
                serializer.Converters.Add(converter);

                await WriteToStreamAsync(jsonWriter, serializer, obj);
            }

            async Task WriteToStreamAsync(JsonTextWriter jsonWriter, JsonSerializer serializer, object obj)
            {
                if (obj.IsNull())
                {
                    await jsonWriter.WriteNullAsync();

                    return;
                }

                await jsonWriter.WriteStartObjectAsync();

                var members = obj.GetType()
                              .GetPropertyAndFieldsWithAttributesInterface <IProvideApiValue>();

                foreach (var(member, apiValueProvider) in members)
                {
                    var memberValue = member.GetPropertyOrFieldValue(obj);
                    if (memberValue.IsNull() && IgnoreNull)
                    {
                        continue;
                    }
                    await WriteAsync(member.GetAttributesInterface <ICastJson>(),
                                     onCouldNotCast : () =>
                    {
                        var objType = obj.GetType();
                        return(WriteAsync(objType.GetAttributesInterface <ICastJson>(),
                                          () => WriteAsync(jsonCastersHttpApp,
                                                           () =>
                        {
                            var memberType = member.GetPropertyOrFieldType();
                            return WriteAsync(memberType.GetAttributesInterface <ICastJson>(),
                                              async() =>
                            {
                                if (memberType.IsArray)
                                {
                                    await jsonWriter.WritePropertyNameAsync(apiValueProvider.PropertyName);
                                    await WriteArrayAsync(memberType);
                                    return;
                                }
                                if (memberType.TryGetAttributeInterface(out IProvideSerialization serializationProvider))
                                {
                                    await jsonWriter.WritePropertyNameAsync(apiValueProvider.PropertyName);
                                    using (var cacheStream = new MemoryStream())
                                    {
                                        await serializationProvider.SerializeAsync(cacheStream,
                                                                                   httpApp, request, paramInfo, memberValue);
                                        var rawJson = cacheStream.ToArray().GetString(encoding);
                                        await jsonWriter.WriteRawValueAsync(rawJson);
                                    }
                                    return;
                                }
                                await jsonWriter.WriteCommentAsync(
                                    $"Cannot find {nameof(ICastJson)} interface for " +
                                    $"{member.DeclaringType.FullName}..{member.Name}");
                                return;
                            });
                        })));