private void TestEnumForByUnderlyingValue <T>(SerializationContext context, T value, string property)
        {
            var serializer = context.GetSerializer <T>();

            using (var stream = new MemoryStream())
            {
                serializer.Pack(stream, value);
                stream.Position = 0;
                var deserialized = serializer.Unpack(stream);

                if (property == null)
                {
                    Assert.That(deserialized, Is.EqualTo(value));
                    stream.Position = 0;
                    var result = Unpacking.UnpackObject(stream);
                    Assert.That(
                        result.ToString().Equals((( IFormattable )value).ToString("D", null)),
                        result + " == " + (( IFormattable )value).ToString("D", null)
                        );
                }
                else
                {
                    var propertyInfo = typeof(T).GetProperty(property);
                    Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
                    stream.Position = 0;
                    var result = Unpacking.UnpackDictionary(stream);
                    Assert.That(
                        result[property].ToString().Equals((( IFormattable )propertyInfo.GetValue(value, null)).ToString("D", null)),
                        result[property] + " == " + (( IFormattable )propertyInfo.GetValue(value, null)).ToString("D", null)
                        );
                }
            }
        }
        private static void TestEnumForByUnderlyingValueCore <T>(Stream stream, T value, T deserialized, string property)
        {
            if (property == null)
            {
                Assert.That(deserialized, Is.EqualTo(value));
                stream.Position = 0;
                var result = Unpacking.UnpackObject(stream);
                Assert.That(
                    result.ToString().Equals((( IFormattable )value).ToString("D", null)),
                    result + " == " + (( IFormattable )value).ToString("D", null)
                    );
            }
            else
            {
                var propertyInfo = typeof(T).GetProperty(property);
#if !UNITY
                Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
#else
                Assert.That(propertyInfo.GetGetMethod().Invoke(deserialized, null), Is.EqualTo(propertyInfo.GetGetMethod().Invoke(value, null)));
#endif // !UNITY
                stream.Position = 0;
                var result = Unpacking.UnpackDictionary(stream);
                Assert.That(
#if !UNITY
                    result[property].ToString().Equals((( IFormattable )propertyInfo.GetValue(value, null)).ToString("D", null)),
                    result[property] + " == " + (( IFormattable )propertyInfo.GetValue(value, null)).ToString("D", null)
#else
                    result[property].ToString().Equals((( IFormattable )propertyInfo.GetGetMethod().Invoke(value, null)).ToString("D", null)),
                    result[property] + " == " + (( IFormattable )propertyInfo.GetGetMethod().Invoke(value, null)).ToString("D", null)
#endif // !UNITY
                    );
            }
        }
        public void TestDataMemberAttributeNamedProperties()
        {
            var context = GetSerializationContext();

            if (context.SerializationMethod == SerializationMethod.Array)
            {
                // Nothing to test.
                return;
            }

            var value = new DataMemberAttributeNamedPropertyTestTarget()
            {
                Member = "A Member"
            };
            var target = this.CreateTarget <DataMemberAttributeNamedPropertyTestTarget>(context);

            using (var buffer = new MemoryStream())
            {
                target.Pack(buffer, value);
                buffer.Position = 0;
                var asDictionary = Unpacking.UnpackDictionary(buffer);

                Assert.That(asDictionary["Alias"] == value.Member);

                buffer.Position = 0;

                var unpacked = target.Unpack(buffer);
                Assert.That(unpacked.Member, Is.EqualTo(value.Member));
            }
        }
Exemplo n.º 4
0
        public void TestIssue111()
        {
            var context = new SerializationContext
            {
                SerializationMethod      = SerializationMethod.Map,
                EnumSerializationOptions =
                {
                    SerializationMethod = EnumSerializationMethod.ByName
                },
                CompatibilityOptions =
                {
                    PackerCompatibilityOptions = PackerCompatibilityOptions.None
                }
            };

            var serializer = context.GetSerializer <Dictionary <string, object> >();

            var dict = new Dictionary <string, object>();

            dict["a"]       = "x";
            dict["b"]       = true;
            dict["c"]       = 5;
            dict["myclass"] = new Issue111Class()
            {
                x = 8, y = "ola"
            };

            byte[] body = serializer.PackSingleObject(dict);

            using (var stream = new MemoryStream(body))
            {
                var unpackedDictionary = Unpacking.UnpackDictionary(stream);
                Assert.That(unpackedDictionary.Count, Is.EqualTo(4));
                Assert.That(unpackedDictionary["a"] == "x");
                Assert.That(unpackedDictionary["b"] == true);
                Assert.That(unpackedDictionary["c"] == 5);
                Assert.That(unpackedDictionary["myclass"].IsDictionary);
                var myClass = unpackedDictionary["myclass"].AsDictionary();
                Assert.That(myClass["x"] == 8);
                Assert.That(myClass["y"] == "ola");
            }
        }
Exemplo n.º 5
0
        private void TestEnumForByName <T>(SerializationContext context, T value, string property)
        {
            var serializer = context.GetSerializer <T>();

            using (var stream = new MemoryStream())
            {
                serializer.Pack(stream, value);
                stream.Position = 0;
                var deserialized = serializer.Unpack(stream);

                if (property == null)
                {
                    Assert.That(deserialized, Is.EqualTo(value));
                    stream.Position = 0;
                    Assert.That(Unpacking.UnpackString(stream), Is.EqualTo(value.ToString()));
                }
                else
                {
                    var propertyInfo = typeof(T).GetProperty(property);
#if !UNITY
                    Assert.That(propertyInfo.GetValue(deserialized, null), Is.EqualTo(propertyInfo.GetValue(value, null)));
#else
                    Assert.That(propertyInfo.GetGetMethod().Invoke(deserialized, null), Is.EqualTo(propertyInfo.GetGetMethod().Invoke(value, null)));
#endif // !UNITY
                    stream.Position = 0;
                    var result = Unpacking.UnpackDictionary(stream);
                    Assert.That(
#if !UNITY
                        result[property].Equals(propertyInfo.GetValue(value, null).ToString()),
                        result[property] + " == " + propertyInfo.GetValue(value, null)
#else
                        result[property].Equals(propertyInfo.GetGetMethod().Invoke(value, null).ToString()),
                        result[property] + " == " + propertyInfo.GetGetMethod().Invoke(value, null)
#endif // !UNITY
                        );
                }
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            string  ip         = "192.168.204.128";
            int     port       = 50020;
            ZSocket requester  = new ZSocket(ZSocketType.REQ);
            string  reqAddress = string.Format("tcp://{0}:{1}", ip, port);

            requester.Connect("tcp://192.168.204.128:50020");
            requester.Send(new ZFrame("SUB_PORT"));

            ZFrame reply    = requester.ReceiveFrame();
            string sub_port = reply.ReadString();

            ZSocket subscriber = new ZSocket(ZSocketType.SUB);
            string  subAddress = string.Format("tcp://{0}:{1}", ip, sub_port);

            subscriber.Connect(subAddress);
            subscriber.SetOption(ZSocketOption.SUBSCRIBE, "pupil.0");
            subscriber.SetOption(ZSocketOption.SUBSCRIBE, "pupil.1");

            while (true)
            {
                IEnumerable <ZFrame> response      = subscriber.ReceiveFrames(2);
                ZFrame[]             responseArray = response.ToArray();
                string topic   = responseArray[0].ReadString();
                byte[] payload = responseArray[1].Read();

                //MessagePackObjectDictionary mpoDict = MessagePackSerializer.Create<MessagePackObjectDictionary>().Unpack(new MemoryStream(rawMessage));

                var mpoDict = Unpacking.UnpackDictionary(payload).Value;


                string message = mpoDict["norm_pos"].ToString();
                Console.WriteLine("{0}: {1}", topic, message);
            }
        }
Exemplo n.º 7
0
        public Dictionary <string, object> Execute(string method, params object[] args)
        {
            if (string.IsNullOrEmpty(_host))
            {
                throw new Exception("Host null or empty");
            }

            if (method != "auth.login" && string.IsNullOrEmpty(_token))
            {
                throw new Exception("Not authenticated.");
            }

            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { return(true); };          //dis be bad, no ssl check

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_host);

            request.ContentType = "binary/message-pack";
            request.Method      = "POST";
            request.KeepAlive   = true;

            Stream requestStream = request.GetRequestStream();
            Packer msgpackWriter = Packer.Create(requestStream);

            msgpackWriter.PackArrayHeader(args.Length + 1 + (string.IsNullOrEmpty(_token) ? 0 : 1));

            msgpackWriter.Pack(method);

            if (!string.IsNullOrEmpty(_token))
            {
                msgpackWriter.Pack(_token);
            }

            foreach (object arg in args)
            {
                Pack(msgpackWriter, arg);
            }

            requestStream.Close();

            byte[]       results;
            byte[]       buffer  = new byte[4096];
            MemoryStream mstream = new MemoryStream();

            using (WebResponse response = request.GetResponse())
            {
                using (Stream rstream = response.GetResponseStream())
                {
                    int count = 0;

                    do
                    {
                        count = rstream.Read(buffer, 0, buffer.Length);
                        mstream.Write(buffer, 0, count);
                    } while (count != 0);
                }
            }

            mstream.Position = 0;
            var unpacker = MessagePackSerializer.Create <Dictionary <string, object> >();

            //everything is a bunch of bytes, needs to be typed
            IDictionary <MessagePackObject, MessagePackObject> resp = Unpacking.UnpackDictionary(mstream);

            //This is me trying to type the response for the user....
            Dictionary <string, object> returnDictionary = TypifyDictionary(resp);

            return(returnDictionary);
        }