예제 #1
0
        public void DeserializeAnnotatedIonToClassNameWithAnnotatedTypeAssemblies()
        {
            IonSerializationOptions options = new IonSerializationOptions()
            {
                AnnotatedTypeAssemblies = new string[]
                {
                    typeof(Truck).Assembly.GetName().Name
                }
            };

            IIonReader reader = IonReaderBuilder.Build(TestObjects.truckIonText);

            IonSerializer ionSerializer = new IonSerializer(options);
            Vehicle       truck         = ionSerializer.Deserialize <Vehicle>(reader);

            AssertIsTruck(truck);

            // Ensure default loaded assemblies are not searched in if annotated type assemblies are given
            options = new IonSerializationOptions
            {
                AnnotatedTypeAssemblies = new string[]
                {
                    "mockAssemblyName"
                }
            };

            reader        = IonReaderBuilder.Build(TestObjects.truckIonText);
            ionSerializer = new IonSerializer(options);
            truck         = ionSerializer.Deserialize <Vehicle>(reader);

            Assert.AreNotEqual(TestObjects.nativeTruck.ToString(), truck.ToString());
        }
예제 #2
0
        public void Scratch()
        {
            IonSerializer ionSerializer;
            MemoryStream  stream;
            Supra         result;


            ionSerializer = new IonSerializer();
            stream        = (MemoryStream)ionSerializer.Serialize(TestObjects.a90);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // 'Amazon.IonObjectMapper.Test.Supra'::{
            //     brand: 'OEM.Manufacturer'::"Toyota"
            // }
            result = ionSerializer.Deserialize <Supra>(stream);
            Console.WriteLine(result);
            // <Supra>{ Toyota }


            ionSerializer = new IonSerializer(new IonSerializationOptions {
                WriterFactory = new JsonIonWriterFactory()
            });
            stream = (MemoryStream)ionSerializer.Serialize(TestObjects.a90);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     brand: "Toyota"
            // }
            result = ionSerializer.Deserialize <Supra>(stream);
            Console.WriteLine(result);
            // <Supra>{ Toyota }
        }
예제 #3
0
        public void Scratch()
        {
            IonSerializer ionSerializer;
            IIonReader    reader;
            BaseType      result;

            string ionText = "'Amazon.IonObjectMapper.Test.DerivedType'::{}";

            ionSerializer = new IonSerializer();
            reader        = IonReaderBuilder.Build(ionText);
            result        = ionSerializer.Deserialize <BaseType>(reader);
            Console.WriteLine(result.GetType());
            // Amazon.IonObjectMapper.Test.DerivedType

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                AnnotatedTypeAssemblies = new string[] { "thisAssemblyDoesNotExist" }
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <BaseType>(reader);
            Console.WriteLine(result.GetType());
            // Amazon.IonObjectMapper.Test.BaseType

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                AnnotatedTypeAssemblies = new string[] { typeof(DerivedType).Assembly.GetName().Name }
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <BaseType>(reader);
            Console.WriteLine(result.GetType());
            // Amazon.IonObjectMapper.Test.DerivedType
        }
예제 #4
0
        public void Scratch()
        {
            AnnotateGuidsClass         annotateGuidsClass         = new AnnotateGuidsClass();
            AnnotateNullableGuidsClass annotateNullableGuidsClass = new AnnotateNullableGuidsClass();

            MemoryStream  stream;
            IonSerializer serializer;

            serializer = new IonSerializer(new IonSerializationOptions {
                AnnotateGuids = true
            });
            stream = (MemoryStream)serializer.Serialize(annotateGuidsClass);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     a: 1,
            //     b: guid128::{{ lkjNEjlW0kmrb2M2lA+jGg== }}
            // }

            stream = (MemoryStream)serializer.Serialize(annotateNullableGuidsClass);
            serializer.Deserialize <AnnotateNullableGuidsClass>(stream);

            serializer = new IonSerializer(new IonSerializationOptions {
                AnnotateGuids = false
            });
            stream = (MemoryStream)serializer.Serialize(annotateGuidsClass);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     a: 1,
            //     b: {{ lkjNEjlW0kmrb2M2lA+jGg== }}
            // }

            stream = (MemoryStream)serializer.Serialize(annotateNullableGuidsClass);
            serializer.Deserialize <AnnotateNullableGuidsClass>(stream);
        }
예제 #5
0
        public void Scratch()
        {
            IonSerializer ionSerializer;
            MemoryStream  stream;
            Country       country;


            stream = (MemoryStream) new IonSerializer().Serialize(TestObjects.UnitedStates);

            ionSerializer = new IonSerializer();
            country       = ionSerializer.Deserialize <Country>(stream);

            Console.WriteLine(country.States[0].Capital.Mayor);
            // Amazon.IonObjectMapper.Test.Politician
            Console.WriteLine(country.States[0].Capital.Mayor.FirstName);
            // Sarah


            stream = (MemoryStream) new IonSerializer().Serialize(TestObjects.UnitedStates);

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                MaxDepth = 4
            });
            country = ionSerializer.Deserialize <Country>(stream);

            Console.WriteLine(country.States[0].Capital.Mayor);
            // Amazon.IonObjectMapper.Test.Politician
            Console.WriteLine(country.States[0].Capital.Mayor.FirstName == null);
            // True
        }
예제 #6
0
        public void Scratch()
        {
            IonSerializer ionSerializer;
            MemoryStream  stream;

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                Format = IonSerializationFormat.BINARY
            });
            stream = (MemoryStream)ionSerializer.Serialize(TestObjects.a90);
            Console.WriteLine(BitConverter.ToString(stream.ToArray()));
            // E0-01-00-EA-EE-B0...

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                Format = IonSerializationFormat.TEXT
            });
            stream = (MemoryStream)ionSerializer.Serialize(TestObjects.a90);
            Console.WriteLine(System.Text.Encoding.UTF8.GetString(stream.ToArray()));
            // 'Amazon.IonObjectMapper.Test.Supra'::{brand:'OEM.Manufacturer'::"Toyota"}

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                Format = IonSerializationFormat.PRETTY_TEXT
            });
            stream = (MemoryStream)ionSerializer.Serialize(TestObjects.a90);
            Console.WriteLine(System.Text.Encoding.UTF8.GetString(stream.ToArray()));
            // 'Amazon.IonObjectMapper.Test.Supra'::{
            //     brand: 'OEM.Manufacturer'::"Toyota"
            // }
        }
예제 #7
0
        public void RespectPrettyTextSerializationFormat()
        {
            var serializer =
                new IonSerializer(new IonSerializationOptions {
                Format = IonSerializationFormat.PRETTY_TEXT
            });
            MemoryStream testStream = (MemoryStream)serializer.Serialize(TestObjects.John);
            string       testString = System.Text.Encoding.UTF8.GetString(testStream.ToArray());

            IIonValue    ionValue             = IonLoader.Default.Load(TestObjects.JohnIonText).GetElementAt(0);
            StringWriter expectedStringWriter = new StringWriter();

            using (var writer =
                       IonTextWriterBuilder.Build(expectedStringWriter, new IonTextOptions {
                PrettyPrint = true
            }))
            {
                ionValue.WriteTo(writer);
                writer.Finish();
            }

            string expectedString = expectedStringWriter.ToString();

            Assert.AreEqual(expectedString, testString);
        }
예제 #8
0
        public void ExceptionOnMultiParameterIonPropertySetterMethods()
        {
            var serializer = new IonSerializer();

            var stream = serializer.Serialize(TestObjects.Chalkboard);

            Assert.ThrowsException <InvalidOperationException>(() => serializer.Deserialize <Chalkboard>(stream));
        }
예제 #9
0
 public static T DeserializeWithCustomSerializer <T>(IonSerializer <T> customSerializer, T item)
 {
     return(DeserializeWithCustomSerializers(
                new Dictionary <Type, IIonSerializer>()
     {
         { typeof(T), customSerializer }
     },
                item));
 }
        public void CanUseTheNamingConventionOnAnObject()
        {
            var stream = new IonSerializer(new IonSerializationOptions {
                NamingConvention = new SnakeCaseNamingConvention()
            }).Serialize(TestObjects.honda);
            var serialized = Utils.StreamToIonValue(stream);

            Assert.AreEqual(2010, serialized.GetField("year_of_manufacture").IntValue);
        }
예제 #11
0
 public static IIonValue SerializeToIonWithCustomSerializer <T>(IonSerializer <T> customSerializer, T item)
 {
     return(SerializeToIonWithCustomSerializers(
                new Dictionary <Type, IIonSerializer>()
     {
         { typeof(T), customSerializer }
     },
                item));
 }
예제 #12
0
        public static T DeserializeWithCustomSerializers <T>(Dictionary <Type, IIonSerializer> ionSerializers, T item)
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                IonSerializers = ionSerializers
            });

            var stream = new IonSerializer().Serialize(item);

            return(serializer.Deserialize <T>(stream));
        }
예제 #13
0
        public void SerializesObjectsWithIgnoreReadOnlyProperties()
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                IgnoreReadOnlyProperties = true
            });
            IIonStruct serialized = StreamToIonValue(serializer.Serialize(TestObjects.JohnGreenwood));

            Assert.IsTrue(serialized.ContainsField("firstName"));
            Assert.IsTrue(serialized.ContainsField("lastName"));
            Assert.IsFalse(serialized.ContainsField("major"));
        }
예제 #14
0
        public static IIonValue SerializeToIonWithCustomSerializers <T>(
            Dictionary <Type, IIonSerializer> ionSerializers, T item)
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                IonSerializers = ionSerializers
            });

            var stream = serializer.Serialize(item);

            return(StreamToIonValue(stream));
        }
예제 #15
0
        public void RespectTypeAnnotationForProperty()
        {
            var serializer = new IonSerializer();
            var testStream = serializer.Serialize(TestObjects.a90);
            var ionValue   = StreamToIonValue(Copy(testStream));

            Assert.IsTrue(ionValue.GetField("brand").HasAnnotation("OEM.Manufacturer"));

            var deserialized = serializer.Deserialize <Supra>(testStream);

            Assert.AreEqual(TestObjects.a90.ToString(), deserialized.ToString());
        }
        public void AnnotatedIonSerializerDeserialization()
        {
            var annotatedIonDeserializer = new Dictionary <string, IIonSerializer>();

            annotatedIonDeserializer.Add("OEM.Manufacturer", new SupraManufacturerDeserializer());

            var customizedDeserializer = new IonSerializer(new IonSerializationOptions {
                AnnotatedIonSerializers = annotatedIonDeserializer
            });

            Assert.AreEqual("BMW", Serde(customizedDeserializer, TestObjects.a90).Brand);
        }
예제 #17
0
        public void DeserializesObjectsThatExceedMaxDepth()
        {
            var stream = new IonSerializer().Serialize(TestObjects.UnitedStates);

            var serializer = new IonSerializer(new IonSerializationOptions {
                MaxDepth = 4
            });
            var deserialized = serializer.Deserialize <Country>(stream);

            Assert.IsNotNull(deserialized.States[0].Capital.Mayor);
            Assert.IsNull(deserialized.States[0].Capital.Mayor.FirstName);
        }
예제 #18
0
        public void SerializesAndDeserializesObjectsWithCaseInsensitiveProperties()
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = true
            });

            var stream       = serializer.Serialize(TestObjects.Titanic);
            var deserialized = serializer.Deserialize <ShipWithVariedCasing>(stream);

            Assert.AreEqual(TestObjects.Titanic.Name, deserialized.name);
            Assert.AreEqual(TestObjects.Titanic.Weight, deserialized.WEIGHT);
            Assert.AreEqual(TestObjects.Titanic.Capacity, deserialized.CaPaCiTy);
        }
예제 #19
0
        public void SerializesObjectsWithIgnoreDefaults()
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                IgnoreDefaults = true
            });
            IIonStruct serialized = StreamToIonValue(serializer.Serialize(new Motorcycle {
                canOffroad = true
            }));

            Assert.IsFalse(serialized.ContainsField("Brand"));
            Assert.IsFalse(serialized.ContainsField("color"));
            Assert.IsTrue(serialized.ContainsField("canOffroad"));
        }
예제 #20
0
        public void DeserializesObjectsWithIgnoreReadOnlyProperties()
        {
            var stream = new IonSerializer().Serialize(TestObjects.JohnGreenwood);

            var serializer = new IonSerializer(new IonSerializationOptions {
                IgnoreReadOnlyProperties = true
            });
            var deserialized = serializer.Deserialize <Student>(stream);

            Assert.AreEqual(TestObjects.JohnGreenwood.FirstName, deserialized.FirstName);
            Assert.AreEqual(TestObjects.JohnGreenwood.LastName, deserialized.LastName);
            Assert.AreNotEqual(TestObjects.JohnGreenwood.Major, deserialized.Major);
        }
예제 #21
0
        public void SerializesObjectsWithIgnoreReadOnlyFields()
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                IgnoreReadOnlyFields = true, IncludeFields = true
            });

            IIonStruct serialized = StreamToIonValue(serializer.Serialize(TestObjects.drKyler));

            Assert.IsFalse(serialized.ContainsField("firstName"));
            Assert.IsFalse(serialized.ContainsField("lastName"));
            Assert.IsTrue(serialized.ContainsField("department"));
            Assert.IsFalse(serialized.ContainsField("birthDate"));
        }
예제 #22
0
        public void DeserializeBasedOnTypeAnnotations()
        {
            var item = new List <Vehicle>()
            {
                new Plane {
                    MaxCapacity = 64
                }, new Boat(), new Helicopter()
            };
            var serializer   = new IonSerializer();
            var deserialized = serializer.Deserialize <List <Vehicle> >(serializer.Serialize(item));

            Assert.AreEqual(string.Join(",", item), string.Join(",", deserialized));
        }
예제 #23
0
        public void DeserializesObjectsWithIgnoreDefaults()
        {
            var stream = new IonSerializer().Serialize(new Motorcycle {
                canOffroad = true
            });

            var serializer = new IonSerializer(new IonSerializationOptions {
                IgnoreDefaults = true
            });
            var deserialized = serializer.Deserialize <Motorcycle>(stream);

            Assert.IsNull(deserialized.Brand);
            Assert.IsNull(deserialized.color);
            Assert.IsNotNull(deserialized.canOffroad);
        }
        public void Scratch()
        {
            IonSerializer ionSerializer;
            Unicorn       result;
            MemoryStream  stream;

            Unicorn unicorn = new Unicorn();

            // ionSerializer = new IonSerializer();
            // stream = (MemoryStream)ionSerializer.Serialize(unicorn);
            // uncommenting the previous two lines will throw a System.ArgumentNullException error

            ionSerializer = new IonSerializer(new IonSerializationOptions
            {
                CustomContext = new Dictionary <string, object>()
                {
                    { "drop", "Dentine" }
                }
            });
            stream = (MemoryStream)ionSerializer.Serialize(unicorn);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     Status: "Mythical",
            //     Drop: "Dentine",
            //     IsHorned: true
            // }
            result = ionSerializer.Deserialize <Unicorn>(stream);
            Console.WriteLine(result);
            //  <Unicorn>{ Mythical, Dentine, True }

            ionSerializer = new IonSerializer(new IonSerializationOptions
            {
                CustomContext = new Dictionary <string, object>()
                {
                    { "father", "Poseidon" }
                }
            });
            stream = (MemoryStream)ionSerializer.Serialize(unicorn);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     Status: "Mythical",
            //     Drop: "Alicorn",
            //     IsHorned: true
            // }
            result = ionSerializer.Deserialize <Unicorn>(stream);
            Console.WriteLine(result);
            //  <Unicorn>{ Mythical, Alicorn, True }
        }
예제 #25
0
        public void DeserializesObjectsWithIgnoreReadOnlyFields()
        {
            var stream = new IonSerializer(new IonSerializationOptions {
                IncludeFields = true
            }).Serialize(TestObjects.drKyler);

            var serializer = new IonSerializer(new IonSerializationOptions {
                IgnoreReadOnlyFields = true, IncludeFields = true
            });
            var deserialized = serializer.Deserialize <Teacher>(stream);

            Assert.IsNull(deserialized.firstName);
            Assert.IsNull(deserialized.lastName);
            Assert.IsNotNull(deserialized.department);
            Assert.IsNull(deserialized.birthDate);
        }
예제 #26
0
        public void SerializesAndDeserializesSubtypesBasedOnTypeAnnotations()
        {
            var item = new List <Vehicle>()
            {
                new Plane(), new Boat(), new Helicopter()
            };
            var serializer = new IonSerializer(new IonSerializationOptions
            {
                AnnotatedTypeAssemblies = new string[]
                {
                    typeof(Vehicle).Assembly.GetName().Name
                }
            });
            var deserialized = serializer.Deserialize <List <Vehicle> >(serializer.Serialize(item));

            Assert.AreEqual(string.Join(",", item), string.Join(",", deserialized));
        }
예제 #27
0
        public void RespectBinarySerializationFormat()
        {
            var serializer = new IonSerializer(new IonSerializationOptions {
                Format = IonSerializationFormat.BINARY
            });
            MemoryStream testStream = (MemoryStream)serializer.Serialize(TestObjects.John);

            IIonValue    ionValue       = IonLoader.Default.Load(TestObjects.JohnIonText).GetElementAt(0);
            MemoryStream expectedStream = new MemoryStream();

            using (var writer = IonBinaryWriterBuilder.Build(expectedStream))
            {
                ionValue.WriteTo(writer);
                writer.Finish();
            }

            Assert.IsTrue(expectedStream.ToArray().SequenceEqual(testStream.ToArray()));
        }
예제 #28
0
        public void RespectTypeAnnotationForClassAndProperty()
        {
            var serializer = new IonSerializer();
            var testStream = serializer.Serialize(TestObjects.honda);
            var ionValue   = StreamToIonValue(Copy(testStream));

            string[] actualAnnotations = ionValue.GetField("engine").GetTypeAnnotationSymbols()
                                         .Select(symbolToken => symbolToken.Text).ToArray();
            string[] expectedAnnotations =
            {
                "Amazon.IonObjectMapper.Test.my.custom.engine.type",
                "Amazon.IonObjectMapper.Test.Engine"
            };
            Assert.AreEqual(string.Join(",", expectedAnnotations), string.Join(",", actualAnnotations));

            var deserialized = serializer.Deserialize <Car>(testStream);

            Assert.AreEqual(TestObjects.honda.ToString(), deserialized.ToString());
        }
예제 #29
0
        private static void RecordMetrics(IDictionary <string, PerformanceTestObject> dictionary)
        {
            IonSerializer serializer = new IonSerializer();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            long baseMemoryUsed = GC.GetTotalMemory(true);

            Stopwatch timer = new Stopwatch();

            timer.Start();
            Stream serialized = serializer.Serialize(dictionary);

            dictionary = serializer.Deserialize <Dictionary <string, PerformanceTestObject> >(serialized);
            timer.Stop();

            long memoryUsed = GC.GetTotalMemory(true) - baseMemoryUsed;

            suite.AddMetric(dictionary.Count, timer.ElapsedTicks, memoryUsed);
        }
예제 #30
0
        public void SerializeObjectsWithCustomContextFactoryAttribute()
        {
            var customContext = new Dictionary <string, object>()
            {
                { "customCourseSerializer", new UpdateCourseSections() }
            };
            var customSerializer = new IonSerializer(new IonSerializationOptions {
                CustomContext = customContext
            });
            var stream     = customSerializer.Serialize(TestObjects.bob);
            var serialized = StreamToIonValue(stream);

            Assert.IsTrue(serialized.ContainsField("name"));
            Assert.IsTrue(serialized.ContainsField("course"));
            Assert.IsTrue(serialized.ContainsField("id"));
            var course = serialized.GetField("course");

            Assert.AreEqual(9, course.GetField("Sections").IntValue);
            Assert.AreEqual("2021-10-10T13:15:21Z", course.GetField("MeetingTime").StringValue);
        }