/// <summary>
        /// Create a JSON example for this type.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string JSONExample(this Type type)
        {
            string result = string.Empty;

            using (JSONSerialiser js = new JSONSerialiser())
            {
                try
                {
                    // see if a default can be created
                    if (type.IsGenericType == true)
                    {
                        // handle List<T> output
                        if (type.GenericTypeArguments.Length > 0)
                        {
                            result = js.Serialize(Activator.CreateInstance(type.GenericTypeArguments[0], true));
                            result = "[" + result + "]";
                        }
                    }
                    else
                    {
                        result = js.Serialize(Activator.CreateInstance(type, true));
                    }
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
            }

            return(result);
        }
 public static string ToJson(this object me)
 {
     using (JSONSerialiser serializer = new JSONSerialiser())
     {
         return(serializer.Serialize(me));
     }
 }
        public void TestMethod1()
        {
            string peerAName = Guid.NewGuid().ToString();
            string peerBName = Guid.NewGuid().ToString();

            string peerAData = Guid.NewGuid().ToString();
            string peerBData = Guid.NewGuid().ToString();

            StringMessage message = new StringMessage()
            {
                To      = new StringPeerIdentification(peerAName),
                From    = new StringPeerIdentification(peerBName),
                Version = new IntMessageVersionIdentification(10),
                Data    = peerAData
            };

            ISerializer serializer = new JSONSerialiser();

            byte[] messageBytes = serializer.Serialize(message);

            StringMessage newMessage = (StringMessage)serializer.Deserialize(messageBytes);



            //BasicUDPNetworking peerA = new BasicUDPNetworking(
            //    StringPeerIdentification.Generate(),
            //    new PeerManager(),
            //    new Serializer(),
            //    new IPEndPoint(IPAddress.Any, 4000)
            //);
        }
示例#4
0
        public void JSONSerialiser_Serialisation_DoesntThrowOnValidData()
        {
            IBaseMessage message        = ISerializerTestsData.GenerateMessage();
            ISerializer  jsonSerialiser = new JSONSerialiser();

            jsonSerialiser.Serialize(message);
        }
 /// <summary>
 /// Save an object in the session in Json Format.
 /// </summary>
 /// <param name="session"></param>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <remarks>Use GetJSon to retrieve the object.></remarks>
 public static void SetJSon(this ISession session, string key, object value)
 {
     using (JSONSerialiser js = new JSONSerialiser())
     {
         string sessionData = js.Serialize(value).ToBase64();
         session.SetString(key, sessionData);
     }
 }
示例#6
0
        public void JSONSerialiser_Deserialisation_DoesntThrowOnValidData()
        {
            IBaseMessage message        = ISerializerTestsData.GenerateMessage();
            ISerializer  jsonSerialiser = new JSONSerialiser();

            byte[] messageBytes = jsonSerialiser.Serialize(message);

            IBaseMessage messageDeserialized = jsonSerialiser.Deserialize(messageBytes);

            Assert.AreEqual(message.To, messageDeserialized.To);
            Assert.AreEqual(message.From, messageDeserialized.From);
            Assert.AreEqual(message.Type, messageDeserialized.Type);
            Assert.AreEqual(message.Version, messageDeserialized.Version);
        }
示例#7
0
        /// <summary>
        /// Create a JSON example for this type.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string JSONExample(this Type type)
        {
            string result = string.Empty;

            using (JSONSerialiser js = new JSONSerialiser())
            {
                try
                {
                    // see if a default can be created
                    result = js.Serialize(Activator.CreateInstance(type, true));
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
            }

            return(result);
        }
示例#8
0
        public void JSONSerialiser_Serialisation_ThrowsOnNull()
        {
            ISerializer jsonSerialiser = new JSONSerialiser();

            Assert.ThrowsException <ArgumentNullException>(() => jsonSerialiser.Serialize(null));
        }