Ejemplo n.º 1
0
        public void RefLoopAndMemberErrors()
        {
            var rootType = typeof(ErrorTypes.With<ErrorTypes.Parent>);
            var valueProperty = rootType.GetProperty(nameof(ErrorTypes.With<ErrorTypes.Parent>.Value));
            var parentType = typeof(ErrorTypes.Parent);
            var childProperty = parentType.GetProperty(nameof(ErrorTypes.Parent.Child));
            var parentProperty = typeof(ErrorTypes.Child).GetProperty(nameof(ErrorTypes.Child.Parent));
            var path = new MemberPath(rootType)
                            .WithProperty(valueProperty);

            var loopPath = path.WithProperty(childProperty)
                               .WithProperty(parentProperty)
                               .WithProperty(childProperty);
            var referenceLoop = new ReferenceLoop(loopPath);
            var refLoopErrors = ErrorBuilder.Start()
                                     .CreateIfNull(rootType)
                                     .Add(referenceLoop)
                                     .Finnish();
            var typeMustNotifyError = TypeMustNotifyError.GetOrCreate(parentType);
            var typeErrors = new TypeErrors( parentType, typeMustNotifyError);
            var memberErrors = new MemberErrors(path, typeErrors);
            var notifyErrors = ErrorBuilder.Start()
                                           .CreateIfNull(rootType)
                                           .Add(memberErrors)
                                           .Finnish();
            var merged = ErrorBuilder.Merge(refLoopErrors, notifyErrors);
            Assert.AreEqual(2, merged.Errors.Count);
            Assert.AreEqual(8, merged.AllErrors.Count);
        }
Ejemplo n.º 2
0
        public void RefLoopAndMemberErrors()
        {
            var rootType       = typeof(ErrorTypes.With <ErrorTypes.Parent>);
            var valueProperty  = rootType.GetProperty(nameof(ErrorTypes.With <ErrorTypes.Parent> .Value));
            var parentType     = typeof(ErrorTypes.Parent);
            var childProperty  = parentType.GetProperty(nameof(ErrorTypes.Parent.Child));
            var parentProperty = typeof(ErrorTypes.Child).GetProperty(nameof(ErrorTypes.Child.Parent));
            var path           = new MemberPath(rootType)
                                 .WithProperty(valueProperty);

            var loopPath = path.WithProperty(childProperty)
                           .WithProperty(parentProperty)
                           .WithProperty(childProperty);
            var referenceLoop = new ReferenceLoop(loopPath);
            var refLoopErrors = ErrorBuilder.Start()
                                .CreateIfNull(rootType)
                                .Add(referenceLoop)
                                .Finnish();
            var typeMustNotifyError = TypeMustNotifyError.GetOrCreate(parentType);
            var typeErrors          = new TypeErrors(parentType, typeMustNotifyError);
            var memberErrors        = new MemberErrors(path, typeErrors);
            var notifyErrors        = ErrorBuilder.Start()
                                      .CreateIfNull(rootType)
                                      .Add(memberErrors)
                                      .Finnish();
            var merged = ErrorBuilder.Merge(refLoopErrors, notifyErrors);

            Assert.AreEqual(2, merged.Errors.Count);
            Assert.AreEqual(8, merged.AllErrors.Count);
        }
            public void WhenError()
            {
                var rootType = typeof(ErrorTypes.With<ErrorTypes.Parent>);
                var valueProperty = rootType.GetProperty(nameof(ErrorTypes.With<ErrorTypes.Parent>.Value));
                var childProperty = typeof(ErrorTypes.Parent).GetProperty(nameof(ErrorTypes.Parent.Child));
                var parentProperty = typeof(ErrorTypes.Child).GetProperty(nameof(ErrorTypes.Child.Parent));
                var path = new MemberPath(rootType)
                                .WithProperty(valueProperty)
                                .WithProperty(childProperty)
                                .WithProperty(parentProperty)
                                .WithProperty(childProperty);
                var referenceLoop = new ReferenceLoop(path);
                var typeErrors = ErrorBuilder.Start()
                                         .CreateIfNull(rootType)
                                         .Add(referenceLoop)
                                         .Finnish();
                var errorBuilder = new StringBuilder();
                errorBuilder.AppendNotSupported(typeErrors);
                var expected = "The property Parent.Child of type Child is in a reference loop.\r\n" +
                               "  - The loop is With<Parent>.Value.Child.Parent.Child...\r\n" +
                               "The property With<Parent>.Value of type Parent is not supported.\r\n" +
                               "The property Parent.Child of type Child is not supported.\r\n" +
                               "The property Child.Parent of type Parent is not supported.\r\n";
                var actual = errorBuilder.ToString();
                Assert.AreEqual(expected, actual);

                Assert.AreEqual(1, typeErrors.Errors.Count);
                Assert.AreEqual(7, typeErrors.AllErrors.Count);
            }
            public void WhenError()
            {
                var rootType       = typeof(ErrorTypes.With <ErrorTypes.Parent>);
                var valueProperty  = rootType.GetProperty(nameof(ErrorTypes.With <ErrorTypes.Parent> .Value));
                var childProperty  = typeof(ErrorTypes.Parent).GetProperty(nameof(ErrorTypes.Parent.Child));
                var parentProperty = typeof(ErrorTypes.Child).GetProperty(nameof(ErrorTypes.Child.Parent));
                var path           = new MemberPath(rootType)
                                     .WithProperty(valueProperty)
                                     .WithProperty(childProperty)
                                     .WithProperty(parentProperty)
                                     .WithProperty(childProperty);
                var referenceLoop = new ReferenceLoop(path);
                var typeErrors    = ErrorBuilder.Start()
                                    .CreateIfNull(rootType)
                                    .Add(referenceLoop)
                                    .Finnish();
                var errorBuilder = new StringBuilder();

                errorBuilder.AppendNotSupported(typeErrors);
                var expected = "The property Parent.Child of type Child is in a reference loop.\r\n" +
                               "  - The loop is With<Parent>.Value.Child.Parent.Child...\r\n" +
                               "The property With<Parent>.Value of type Parent is not supported.\r\n" +
                               "The property Parent.Child of type Child is not supported.\r\n" +
                               "The property Child.Parent of type Parent is not supported.\r\n";
                var actual = errorBuilder.ToString();

                Assert.AreEqual(expected, actual);

                Assert.AreEqual(1, typeErrors.Errors.Count);
                Assert.AreEqual(7, typeErrors.AllErrors.Count);
            }
 public static string Serialize <T>(T objectToSerialize,
                                    bool preserveTypeInfo           = false,
                                    List <JsonConverter> converters = null,
                                    bool indent = false,
                                    ReferenceLoop referenceLoopHandling = ReferenceLoop.Ignore)
 {
     return(JsonConvert.SerializeObject(objectToSerialize, PrepareSettings(preserveTypeInfo, converters, indent, referenceLoopHandling)));
 }
 public static void SerializeCompressed <T>(T objectToSerialize, Stream stream,
                                            bool preserveTypeInfo           = false,
                                            List <JsonConverter> converters = null,
                                            bool indent = false,
                                            ReferenceLoop referenceLoopHandling = ReferenceLoop.Ignore)
 {
     using (var compressor = new GZipStream(stream, CompressionMode.Compress, true))
         using (var writer = new StreamWriter(compressor))
         {
             var serializer = JsonSerializer.CreateDefault(PrepareSettings(preserveTypeInfo, converters, indent, referenceLoopHandling));
             serializer.Serialize(writer, objectToSerialize);
         }
 }
            public static void Serialize <T>(T objectToSerialize,
                                             Stream stream,
                                             bool preserveTypeInfo           = false,
                                             List <JsonConverter> converters = null,
                                             bool indent = false,
                                             ReferenceLoop referenceLoopHandling = ReferenceLoop.Ignore)
            {
                var serializer = JsonSerializer.CreateDefault(PrepareSettings(preserveTypeInfo, converters, indent, referenceLoopHandling));

                using (var writer = new StreamWriter(stream))
                {
                    serializer.Serialize(writer, objectToSerialize);
                }
            }
Ejemplo n.º 8
0
        internal static JsonSerializerSettings PrepareSettings(bool preserveTypeInfo, List <JsonConverter> converters, bool indent = false,
                                                               ReferenceLoop referenceLoopHandling = ReferenceLoop.Ignore)
        {
            var settings = new JsonSerializerSettings
            {
                DateFormatHandling    = DateFormatHandling.MicrosoftDateFormat,
                Converters            = converters ?? new List <JsonConverter>(),
                Formatting            = indent ? Formatting.Indented : Formatting.None,
                ReferenceLoopHandling = (ReferenceLoopHandling)(int)referenceLoopHandling,
            };

            if (preserveTypeInfo)
            {
                settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
                settings.TypeNameHandling           = TypeNameHandling.Auto;
                //bunun ile $type etiketi eklenip polimorfik objelere izin veriliyor.
                settings.ObjectCreationHandling = ObjectCreationHandling.Replace;
            }
            return(settings);
        }