Esempio n. 1
0
        public SocketClientSettings(string server, int port,
                                    Encoding encoding, int packetSize, int maxAttempts,
                                    int receiveTimeOut, JsonSerializerSettings settings)
        {
            Server      = server;
            Port        = port;
            Encoding    = encoding;
            BufferSize  = packetSize;
            MaxAttempts = maxAttempts;
            Timeout     = receiveTimeOut;

            SerializerSettings = new JsonSerializerSettings();
            foreach (PropertyInfo prop in SerializerSettings.GetType().GetProperties())
            {
                try
                {
                    var value = settings.GetType().GetProperty(prop.Name).GetValue(settings);
                    if (value == null)
                    {
                        continue;
                    }
                    prop.SetValue(SerializerSettings, value);
                }
                catch { }
            }
        }
Esempio n. 2
0
        public SocketClientSettings(string server, int port,
                                    Encoding encoding, int maxAttempts = 3,
                                    int timeout = 5000,
                                    JsonSerializerSettings jsonSettings = null)
        {
            Server      = server;
            Port        = port;
            Encoding    = encoding;
            MaxAttempts = maxAttempts;
            Timeout     = timeout;

            if (jsonSettings != null)
            {
                SerializerSettings = new JsonSerializerSettings();
                foreach (PropertyInfo prop in SerializerSettings.GetType().GetProperties())
                {
                    try
                    {
                        var value = jsonSettings.GetType().GetProperty(prop.Name).GetValue(jsonSettings);
                        if (value == null)
                        {
                            continue;
                        }
                        prop.SetValue(SerializerSettings, value);
                    }
                    catch { }
                }
            }
        }
Esempio n. 3
0
        private void ConnectToServer(string server,
                                     int port, Encoding encoding, int byteBuffer = 4098 * 50, int maxAttempts = 10,
                                     JsonSerializerSettings serializerSettings   = null)
        {
            int attempts = 0;

            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            while (!clientSocket.Connected)
            {
                if (attempts >= maxAttempts)
                {
                    throw new Exception("Maximum number of attempts exceeded");
                }

                attempts++;
                clientSocket.Connect(server, port);
            }

            Server     = server;
            Port       = port;
            Encoding   = encoding;
            BufferSize = byteBuffer;

            if (serializerSettings != null)
            {
                SerializerSettings = new JsonSerializerSettings();
                foreach (PropertyInfo prop in SerializerSettings.GetType().GetProperties())
                {
                    try
                    {
                        var value = serializerSettings.GetType().GetProperty(prop.Name).GetValue(serializerSettings);
                        if (value == null)
                        {
                            continue;
                        }
                        prop.SetValue(SerializerSettings, value);
                    }
                    catch { }
                }
            }

            thisStr = $"SocketClient for .NET Standard. Connected on {server}:{port}";
        }
Esempio n. 4
0
        public static void ApplyCustomSettings(this JsonSerializer serializer,
                                               JsonSerializerSettings settings)
        {
            if (settings == null)
            {
                return;
            }

            foreach (PropertyInfo prop in serializer.GetType().GetProperties())
            {
                try
                {
                    var value = settings.GetType().GetProperty(prop.Name).GetValue(settings);
                    if (value == null)
                    {
                        continue;
                    }
                    prop.SetValue(serializer, value);
                }
                catch { }
            }
        }
        /// <summary>
        /// Changes the default serializer settings for Newtonsoft.Json
        /// </summary>
        /// <param name="settings">JsonSerializerSettings object</param>
        protected void SetJsonSerializerSettings(JsonSerializerSettings settings)
        {
            if (settings == null)
            {
                throw new InvalidOperationException("Cannot be null.");
            }

            SerializerSettings = new JsonSerializerSettings();
            foreach (PropertyInfo prop in SerializerSettings.GetType().GetProperties())
            {
                try
                {
                    var value = settings.GetType().GetProperty(prop.Name).GetValue(settings);
                    if (value == null)
                    {
                        continue;
                    }
                    prop.SetValue(SerializerSettings, value);
                }
                catch { }
            }
        }
        public string Serialize <T>(T objectToSerialize, string idPropertyName)
        {
            Validate.NotNull(objectToSerialize, nameof(objectToSerialize));

            var settings = _settings;

            if (idPropertyName != null)
            {
                if (!_settingsCache.TryGetValue(typeof(T), out settings))
                {
                    settings = new JsonSerializerSettings();
                    foreach (var p in settings.GetType().GetProperties())
                    {
                        p.SetValue(settings, p.GetValue(_settings));
                    }

                    settings.ContractResolver = new ShouldSerializeContractResolver <T>(idPropertyName);

                    _settingsCache.TryAdd(typeof(T), settings);
                }
            }

            return(JsonConvert.SerializeObject(objectToSerialize, settings));
        }