Пример #1
0
        private static void TestSerializationOnAllSerializers(Action <ISerializer, string> action, bool testWithoutGraphIdsAsWell = true)
        {
            var serializers = new List <ISerializer>();

            serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.Performance));
            serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.PrettyXml));
            serializers.Add(SerializationTestHelper.GetBinarySerializer());
            serializers.Add(SerializationTestHelper.GetJsonSerializer());

            if (testWithoutGraphIdsAsWell)
            {
                var basicJsonSerializer = SerializationTestHelper.GetJsonSerializer();
                basicJsonSerializer.PreserveReferences = false;
                basicJsonSerializer.WriteTypeInfo      = false;
                serializers.Add(basicJsonSerializer);
            }

            foreach (var serializer in serializers)
            {
                var typeName = serializer.GetType().GetSafeFullName();

                Log.Info();
                Log.Info();
                Log.Info();
                Log.Info("=== TESTING SERIALIZER: {0} ===", typeName);
                Log.Info();
                Log.Info();
                Log.Info();

                action(serializer, typeName);
            }
        }
Пример #2
0
            private void CustomizedJsonParsing(string json, Action <object[]> assertAction)
            {
                var parameters     = new object[] { 0, 10, false, null, null };
                var parameterTypes = new[] { typeof(int), typeof(int), typeof(bool), typeof(IEnumerable <SortDescriptor>), typeof(FilterDescriptor) };
                var parameterNames = new Dictionary <string, int> {
                    { "skip", 0 }, { "take", 1 }, { "includeDeleted", 2 }, { "sort", 3 }, { "filter", 4 }
                };

                var serializer = SerializationTestHelper.GetJsonSerializer();

                serializer.PreserveReferences = false;
                serializer.WriteTypeInfo      = false;

                using (var memoryStream = new MemoryStream())
                {
                    var writer = new StreamWriter(memoryStream);
                    writer.Write(json);
                    writer.Flush();

                    memoryStream.Position = 0L;

                    var reader = new StreamReader(memoryStream);
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        jsonReader.Read();

                        if (jsonReader.TokenType != JsonToken.StartObject)
                        {
                            throw new InvalidOperationException("Input needs to be wrapped in an object");
                        }

                        jsonReader.Read();

                        while (jsonReader.TokenType == JsonToken.PropertyName)
                        {
                            var parameterName = jsonReader.Value as string;

                            jsonReader.Read();
                            int parameterIndex;

                            if ((parameterName != null) && parameterNames.TryGetValue(parameterName, out parameterIndex))
                            {
                                parameters[parameterIndex] = serializer.Deserialize(parameterTypes[parameterIndex], jsonReader, null);
                            }
                            else
                            {
                                jsonReader.Skip();
                            }

                            jsonReader.Read();
                        }
                    }
                }

                assertAction(parameters);
            }
Пример #3
0
        private static void TestSerializationOnAllSerializers(Action <ISerializer, ISerializationConfiguration, string> action,
                                                              bool testWithoutGraphIdsAsWell = true, ISerializationManager serializationManager = null)
        {
            if (serializationManager == null)
            {
                serializationManager = new SerializationManager();
            }

            var serializerConfigurations = new Dictionary <Type, List <ISerializationConfiguration> >();

            serializerConfigurations[typeof(XmlSerializer)] = new List <ISerializationConfiguration>(new[]
            {
                new SerializationConfiguration()
            });

            serializerConfigurations[typeof(BinarySerializer)] = new List <ISerializationConfiguration>(new[]
            {
                new SerializationConfiguration()
            });

            serializerConfigurations[typeof(JsonSerializer)] = new List <ISerializationConfiguration>(new[]
            {
                new JsonSerializationConfiguration
                {
                    UseBson = false
                },
                new JsonSerializationConfiguration
                {
                    UseBson = true
                },
            });

            var serializers = new List <ISerializer>();

            serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.Performance, serializationManager));
            serializers.Add(SerializationTestHelper.GetXmlSerializer(XmlSerializerOptimalizationMode.PrettyXml, serializationManager));
            serializers.Add(SerializationTestHelper.GetBinarySerializer(serializationManager));
            serializers.Add(SerializationTestHelper.GetJsonSerializer(serializationManager));

            if (testWithoutGraphIdsAsWell)
            {
                var basicJsonSerializer = SerializationTestHelper.GetJsonSerializer(serializationManager);
                basicJsonSerializer.PreserveReferences = false;
                basicJsonSerializer.WriteTypeInfo      = false;
                serializers.Add(basicJsonSerializer);
            }

            foreach (var serializer in serializers)
            {
                var type     = serializer.GetType();
                var typeName = type.GetSafeFullName(false);

                var configurations = serializerConfigurations[type];
                foreach (var configuration in configurations)
                {
                    Log.Info();
                    Log.Info();
                    Log.Info();
                    Log.Info("=== TESTING SERIALIZER: {0} ===", typeName);
                    Log.Info();
                    Log.Info();
                    Log.Info();

                    action(serializer, configuration, typeName);
                }
            }
        }