public void RoundTrip()
    {
        var factory    = new EncryptionFactory();
        var serializer = new JsonSerializer
        {
            ContractResolver = factory.GetContractResolver()
        };

        using (var algorithm = CryptoBuilder.Build())
        {
            string serialized;
            using (factory.GetEncryptSession(algorithm))
            {
                var instance = new ClassToSerialize
                {
                    Property1 = "Property1Value",
                    Property2 = "Property2Value"
                };
                serialized = serializer.Serialize(instance);
            }
            using (factory.GetDecryptSession(algorithm))
            {
                var result = serializer.Deserialize <ClassToSerialize>(serialized);
                ObjectApprover.VerifyWithJson(result);
            }
        }
    }
예제 #2
0
        void ButtonSerializeDeserialize_Click(object sender, RoutedEventArgs e)
        {
            // Serialize:
            var dataContractSerializer = new DataContractSerializer(typeof(ClassToSerialize));

#if OPENSILVER
            string       xml     = null;
            MemoryStream stream1 = new MemoryStream();
            dataContractSerializer.WriteObject(stream1, _classToSerialize);

            stream1.Seek(0, SeekOrigin.Begin);
            using (var streamReader = new StreamReader(stream1))
            {
                xml = streamReader.ReadToEnd();
            }
#else
            var xml = dataContractSerializer.SerializeToString(_classToSerialize);
#endif

            // Display the result of the serialization:
            MessageBox.Show("Result of the serialization:" + Environment.NewLine + Environment.NewLine + xml);

            // Deserialize:
            dataContractSerializer = new DataContractSerializer(typeof(ClassToSerialize));
#if OPENSILVER
            ClassToSerialize deserializedObject = (ClassToSerialize)dataContractSerializer.ReadObject(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml)));
#else
            ClassToSerialize deserializedObject = (ClassToSerialize)dataContractSerializer.DeserializeFromString(xml);
#endif

            // Display the result of the deserialization:
            SerializationDestinationPanel.DataContext = deserializedObject;
        }
예제 #3
0
        /// <summary>
        /// Helper method for Gem.Network.Tests.MessageEncodingTests.SuccessfulDecodeTest()
        /// Set's up a server and sends a message for decoding
        /// This is done in a seperate project because a socket exception is thrown
        /// because client / server use the same port
        /// </summary>
        public static void MessageDecodingTestHelper()
        {
            var server = new NetworkServer();
            //server.Connect(new ServerConfig { Name = "local", Port = 14241 });

            NetIncomingMessage inc;

            while (true)
            {
                if ((inc = server.ReadMessage()) != null)
                {
                    switch (inc.MessageType)
                    {
                    case NetIncomingMessageType.ConnectionApproval:
                        Console.WriteLine("Incoming LOGIN");
                        inc.SenderConnection.Approve();
                        break;

                    case NetIncomingMessageType.Data:
                        var outgoingmessage = server.CreateMessage();
                        var obj             = new ClassToSerialize();
                        obj.StringProperty = "SomeString";
                        MessageSerializer.Encode(obj, ref outgoingmessage);
                        server.SendToAll(outgoingmessage);

                        Console.WriteLine("Sended a package with some string");
                        break;
                    }
                }
            }
        }
    static void Main()
    {
        // per system (periodically rotated)
        var key = Encoding.UTF8.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");

        // per app domain
        using (var factory = new EncryptionFactory())
        {
            var serializer = new JsonSerializer
            {
                ContractResolver = factory.GetContractResolver()
            };

            // transferred as meta data with the serialized payload
            byte[] initVector;

            string serialized;

            // per serialize session
            using (var algorithm = new RijndaelManaged
            {
                Key = key
            })
            {
                initVector = algorithm.IV;
                using (factory.GetEncryptSession(algorithm))
                {
                    var instance = new ClassToSerialize
                    {
                        Property = "PropertyValue",
                    };
                    var builder = new StringBuilder();
                    using (var writer = new StringWriter(builder))
                    {
                        serializer.Serialize(writer, instance);
                    }
                    serialized = builder.ToString();
                }
            }

            // per deserialize session
            using (var algorithm = new RijndaelManaged
            {
                IV = initVector,
                Key = key
            })
            {
                using (factory.GetDecryptSession(algorithm))
                {
                    using var stringReader = new StringReader(serialized);
                    using var jsonReader   = new JsonTextReader(stringReader);
                    var deserialized = serializer.Deserialize <ClassToSerialize>(jsonReader);
                    Console.WriteLine(deserialized !.Property);
                }
            }
        }
        Console.ReadKey();
    }
예제 #5
0
        public void Test_ConvertToJsonWithAnnotation()
        {
            var tempClass = new ClassToSerialize <WeekdayWithAnnotation>
            {
                Item = WeekdayWithAnnotation.Monday
            };

            var json = JsonSerializer.Serialize(tempClass);

            json.Should().Be("{\"Item\":1}");
        }
예제 #6
0
        public void Test_ConvertNullToJsonWithAnnotation()
        {
            var tempClass = new ClassToSerialize <Weekday>
            {
                Item = null
            };

            var json = JsonSerializer.Serialize(tempClass);

            json.Should().Be("{\"Item\":null}");
        }
예제 #7
0
    static void Main()
    {
        var obj = new ClassToSerialize
        {
            PrimaryExc = 'a',
            IsMf       = false
        };
        var serializer = new DataContractJsonSerializer(obj.GetType());

        serializer.WriteObject(Console.OpenStandardOutput(), obj);
    }
    public void TestMethod1()
    {
        XmlSerializer sr   = new XmlSerializer(typeof(ClassToSerialize));
        var           demo = new DemoChild();
        var           ser  = new ClassToSerialize {
            anotherOne = demo
        };
        var stream = new MemoryStream();

        sr.Serialize(stream, ser);
    }
예제 #9
0
    static void Main(string[] args)
    {
        var obj  = new ClassToSerialize();
        var json = JsonConvert.SerializeObject(obj);

        Console.WriteLine(json);
        obj = JsonConvert.DeserializeObject <ClassToSerialize>(json);

        // note that obj.IgnoredMember == null at this point
        Console.ReadKey();
    }
예제 #10
0
    public void DeSerializeNow()
    {
        ClassToSerialize c = new ClassToSerialize();
        FileStream       s = new FileStream("Demo.bin", FileMode.Open, FileAccess.Read);
        BinaryFormatter  b = new BinaryFormatter();

        c = (ClassToSerialize)b.Deserialize(s);
        Console.WriteLine("Name :" + c.Name);
        Console.WriteLine("Age :" + c.Age);
        Console.WriteLine("Company Name :" + ClassToSerialize.CompanyName);
        Console.WriteLine("Company Name :" + c.GetSupportClassString());
        s.Close();
    }
        public DataContractSerializer_Demo()
        {
            this.InitializeComponent();

            _classToSerialize = new ClassToSerialize()
            {
                TextField    = "Some Text",
                DateField    = DateTime.Now,
                GuidField    = Guid.NewGuid(),
                BooleanField = true
            };
            SerializationSourcePanel.DataContext = _classToSerialize;
        }
예제 #12
0
        public void Serialize_ConvertsToYesNo(bool?value, string expectedResult)
        {
            // Arrange
            var model = new ClassToSerialize {
                BooleanValue = value
            };

            // Act
            var result = JsonConvert.SerializeObject(model);

            // Assert
            result.Should().Be(@$ "{{" "BooleanValue" ":{expectedResult}}}");
        }
예제 #13
0
        public void Serialize_ConvertsToYesNo(float?value, string expectedResult)
        {
            // Arrange
            var model = new ClassToSerialize {
                DecimalValue = (decimal?)value
            };

            // Act
            var result = JsonConvert.SerializeObject(model);

            // Assert
            result.Should().Be(@$ "{{" "DecimalValue" ":{expectedResult}}}");
        }
예제 #14
0
    public void Test()
    {
        var expected = new ClassToSerialize {
            Field1 = 1, Field2 = 2, Field3 = 3, Field4 = 4, Field5 = 5
        };
        var actual = Roundtrip(expected);

        Assert.AreEqual(expected.Field1, actual.Field1);
        Assert.AreEqual(expected.Field2, actual.Field2);
        Assert.AreEqual(expected.Field3, actual.Field3);
        Assert.AreEqual(expected.Field4, actual.Field4);
        Assert.AreEqual(expected.Field5, actual.Field5);
    }
예제 #15
0
    public void SerializeNow()
    {
        ClassToSerialize c = new ClassToSerialize();

        c.Name = "bipin";
        c.Age  = 26;
        ClassToSerialize.CompanyName = "xyz";
        FileStream      s = new FileStream("Demo.bin", FileMode.OpenOrCreate, FileAccess.Write);
        BinaryFormatter b = new BinaryFormatter();

        b.Serialize(s, c);
        s.Close();
    }
    public void ExampleUsage()
    {
        // per system (periodically rotated)
        var key = Encoding.UTF8.GetBytes("gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6");

        // per app domain
        using (var factory = new EncryptionFactory())
        {
            var serializer = new JsonSerializer
            {
                ContractResolver = factory.GetContractResolver()
            };

            // transferred as meta data with the serialized payload
            byte[] initVector;

            string serialized;

            // per serialize session
            using (var algorithm = new RijndaelManaged
            {
                Key = key
            })
            {
                initVector = algorithm.IV;
                using (factory.GetEncryptSession(algorithm))
                {
                    var instance = new ClassToSerialize
                    {
                        Property1 = "Property1Value",
                        Property2 = "Property2Value"
                    };
                    serialized = serializer.Serialize(instance);
                }
            }

            // per deserialize session
            using (var algorithm = new RijndaelManaged
            {
                IV = initVector,
                Key = key
            })
            {
                using (factory.GetDecryptSession(algorithm))
                {
                    var deserialized = serializer.Deserialize <ClassToSerialize>(serialized);
                    ObjectApprover.VerifyWithJson(deserialized);
                }
            }
        }
    }
예제 #17
0
        void ButtonSerializeDeserialize_Click(object sender, RoutedEventArgs e)
        {
            // Serialize:
            var dataContractSerializer = new DataContractSerializer(typeof(ClassToSerialize));
            var serializedXml          = dataContractSerializer.SerializeToString(_classToSerialize);

            // Display the result of the serialization:
            MessageBox.Show("Result of the serialization:" + Environment.NewLine + Environment.NewLine + serializedXml);

            // Deserialize:
            dataContractSerializer = new DataContractSerializer(typeof(ClassToSerialize));
            ClassToSerialize deserializedObject = (ClassToSerialize)dataContractSerializer.DeserializeFromString(serializedXml);

            // Display the result of the deserialization:
            SerializationDestinationPanel.DataContext = deserializedObject;
        }
예제 #18
0
        public void Test_ConvertToJsonWithSerializerOptions()
        {
            var tempClass = new ClassToSerialize <Weekday>
            {
                Item = Weekday.Monday
            };

            var serializerOptions = new JsonSerializerOptions
            {
                Converters = { new JsonTypeSafeEnumConverterFactory() }
            };

            var json = JsonSerializer.Serialize(tempClass, serializerOptions);

            json.Should().Be("{\"Item\":1}");
        }
예제 #19
0
    static void Main()
    {
        var obj = new ClassToSerialize
        {
            PrimaryExc = 'a',
            NoResults  = true
        };

        var serializer
            = new DataContractJsonSerializer(typeof(ClassToSerialize));

        var ms = new MemoryStream();

        serializer.WriteObject(ms, obj);

        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    }
        public void ProxyIsXmlSerializable()
        {
            ClassToSerialize proxy = (ClassToSerialize)
                                     generator.CreateClassProxy(typeof(ClassToSerialize), new StandardInterceptor());

            XmlSerializer serializer = new XmlSerializer(proxy.GetType());

            StringWriter writer = new StringWriter();

            serializer.Serialize(writer, proxy);

            StringReader reader = new StringReader(writer.GetStringBuilder().ToString());

            object newObj = serializer.Deserialize(reader);

            Assert.IsNotNull(newObj);
            Assert.IsInstanceOf(typeof(ClassToSerialize), newObj);
        }
    public void Simple()
    {
        var factory    = new EncryptionFactory();
        var serializer = new JsonSerializer
        {
            ContractResolver = factory.GetContractResolver()
        };

        using (var algorithm = CryptoBuilder.Build())
            using (factory.GetEncryptSession(algorithm))
            {
                var instance = new ClassToSerialize
                {
                    Property1 = "Property1Value",
                    Property2 = "Property2Value"
                };
                var result = serializer.Serialize(instance);
                Approvals.Verify(result);
            }
    }
예제 #22
0
        public Page5_Other()
        {
            this.InitializeComponent();

            // Initialize the DispatcherTimer
            _dispatcherTimer = new DispatcherTimer()
            {
                Interval = new TimeSpan(0, 0, 0, 0, 100)
            };
            _dispatcherTimer.Tick += DispatcherTimer_Tick;

            // Initialize the XmlSerializer demo:
            _classToSerialize = new ClassToSerialize()
            {
                TextField    = "Some text",
                DateField    = DateTime.Now,
                GuidField    = Guid.NewGuid(),
                BooleanField = true
            };
            SerializationSourcePanel.DataContext = _classToSerialize;
        }