Пример #1
0
        public void CanDeserializeToDifferentTypeUsingIonConstructor()
        {
            var cr = new CircleRadius(5);

            var stream = defaultSerializer.Serialize(cr);
            var cc     = defaultSerializer.Deserialize <CircleCircumference>(stream);

            Assert.AreEqual(2 * Math.PI * cr.Radius, cc.Circumference);
        }
Пример #2
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);
        }
Пример #3
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 }
        }
Пример #4
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());
        }
Пример #5
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
        }
Пример #6
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
        }
Пример #7
0
        public void ExceptionOnMultiParameterIonPropertySetterMethods()
        {
            var serializer = new IonSerializer();

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

            Assert.ThrowsException <InvalidOperationException>(() => serializer.Deserialize <Chalkboard>(stream));
        }
Пример #8
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));
        }
Пример #9
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);
        }
Пример #10
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());
        }
Пример #11
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);
        }
Пример #12
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));
        }
Пример #13
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);
        }
Пример #14
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 }
        }
Пример #16
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);
        }
Пример #17
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));
        }
Пример #18
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());
        }
Пример #19
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);
        }
Пример #20
0
        public void Scratch()
        {
            IonSerializer ionSerializer;
            List <object> result;
            MemoryStream  stream;

            List <object> testList = new List <object>(new object[] { "test", 5 });

            ionSerializer = new IonSerializer();
            stream        = (MemoryStream)ionSerializer.Serialize(testList);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // [
            //     "test",
            //     5
            // ]
            result = ionSerializer.Deserialize <List <object> >(stream);
            Console.WriteLine(result[0]);
            // test
            Console.WriteLine(result[1]);
            // 5

            ionSerializer = new IonSerializer(new IonSerializationOptions
            {
                IonSerializers = new Dictionary <Type, IIonSerializer>()
                {
                    { typeof(string), new BracketStringIonSerializer() }
                }
            });
            stream = (MemoryStream)ionSerializer.Serialize(testList);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // [
            //     "<test>",
            //     5
            // ]
            result = ionSerializer.Deserialize <List <object> >(stream);
            Console.WriteLine(result[0]);
            // test
            Console.WriteLine(result[1]);
            // 5
        }
Пример #21
0
        public void Scratch()
        {
            IgnoreDefaultsClass ignoreDefaultsObject = new IgnoreDefaultsClass {
                A = 1
            };

            MemoryStream  stream;
            IonSerializer ionSerializer;

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                IgnoreDefaults = false
            });
            stream = (MemoryStream)ionSerializer.Serialize(ignoreDefaultsObject);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     a: 1,
            //     b: null
            // }

            ignoreDefaultsObject = ionSerializer.Deserialize <IgnoreDefaultsClass>(stream);
            Console.WriteLine(ignoreDefaultsObject.A);
            // 1
            Console.WriteLine(ignoreDefaultsObject.B == null);
            // True

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                IgnoreDefaults = true
            });
            stream = (MemoryStream)ionSerializer.Serialize(ignoreDefaultsObject);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     a: 1
            // }

            ignoreDefaultsObject = ionSerializer.Deserialize <IgnoreDefaultsClass>(stream);
            Console.WriteLine(ignoreDefaultsObject.A);
            // 1
            Console.WriteLine(ignoreDefaultsObject.B == null);
            // True
        }
        public void Scratch()
        {
            IonSerializer ionSerializer;
            Trilobyte     result;
            MemoryStream  stream;

            Trilobyte trilobyte = new Trilobyte();

            ionSerializer = new IonSerializer();
            stream        = (MemoryStream)ionSerializer.Serialize(trilobyte);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     group: 'Paleozoic.Cambrian'::"Arthropods",
            //     isVertebrate: false
            // },

            result = ionSerializer.Deserialize <Trilobyte>(stream);
            Console.WriteLine(result);
            //  <Trilobyte>{ Arthropods, False }

            ionSerializer = new IonSerializer(new IonSerializationOptions
            {
                AnnotatedIonSerializers = new Dictionary <string, IIonSerializer>()
                {
                    { "Paleozoic.Cambrian", new CambrianIonSerializer() }
                }
            });
            stream = (MemoryStream)ionSerializer.Serialize(trilobyte);
            Console.WriteLine(Utils.PrettyPrint(stream));
            // {
            //     group: 'Paleozoic.Cambrian'::"Springgina",
            //     isVertebrate: false
            // },
            result = ionSerializer.Deserialize <Trilobyte>(stream);
            Console.WriteLine(result);
            //  <Trilobyte>{ Springgina, False }
        }
        public void Scratch()
        {
            string ionText = "{iRoNiCcAsE: 3}";

            IonSerializer   ionSerializer;
            IIonReader      reader;
            NormalCaseClass result;

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = false
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <NormalCaseClass>(reader);
            Console.WriteLine(result.ironicCase == null); // could not deserialize differently-cased text
            // True

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = true
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <NormalCaseClass>(reader);
            Console.WriteLine(result.ironicCase); // successfully deserializes differently-cased text
            // 3
        }
Пример #24
0
        public static T Serde <T>(IonSerializer ionSerializer, T item)
        {
            var stream = ionSerializer.Serialize(item);

            return(ionSerializer.Deserialize <T>(stream));
        }
 /// <summary>
 /// Deserialize a ValueHolder object containing the Ion binary into an object of type T.
 /// </summary>
 ///
 /// <param name="v">The ValueHolder object to be deserialized into an object of type T.</param>
 /// <typeparam name="T">The return type.</typeparam>
 ///
 /// <returns>The object of type T.</returns>
 public T Deserialize <T>(ValueHolder v)
 {
     return(serializer.Deserialize <T>(v.IonBinary));
 }