예제 #1
0
        private void TestSerializer(IValueSerializer serializer, string name)
        {
            //First check we can set and get strings
            byte[] bytes;

            bytes = serializer.Serialize("TestString");
            var str = serializer.Deserialize(bytes) as string;

            Assert.AreEqual("TestString", str);

            //Then check performance of type serialization

            var userObject = JsonConvert.DeserializeObject <User>("{\"$id\":\"1\",\"ApplicationId\":\"00000000-0000-0000-0000-000000000000\",\"Id\":\"b2f29128-24c0-4a8a-b3cd-8bfc0378bd72\",\"UserName\":\"[email protected]\",\"LoweredUserName\":\"[email protected]\",\"MobileAlias\":null,\"IsAnonymous\":false,\"LastActivityDate\":\"0001-01-01T00:00:00\",\"FirstName\":\"Vinsight\",\"LastName\":\"Support\",\"isApiUser\":false,\"Application\":null,\"ApplicationReference\":{\"EntityKey\":null},\"UsersInRoles\":[{\"$id\":\"2\",\"EntryNum\":9,\"UserId\":\"b2f29128-24c0-4a8a-b3cd-8bfc0378bd72\",\"RoleId\":\"881ea4f6-d7db-4890-8418-15e84fc910b9\",\"OrganisationId\":\"c663e81d-57e2-4ba6-a40c-6c587101ae95\",\"Organisation\":{\"$id\":\"3\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Name\":\"Vinsight Support\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[{\"$ref\":\"2\"}],\"Subscriptions\":[],\"Applications\":[],\"Users\":[],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},\"Org anisationReference\":{\"EntityKey\":null},\"Role\":{\"$id\":\"4\",\"ApplicationId\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"Id\":\"881ea4f6-d7db-4890-8418-15e84fc910b9\",\"Name\":\"Admins\",\"LoweredName\":\"admins\",\"Description\":null,\"IsReadOnly\":false,\"Application\":{\"$id\":\"5\",\"Name\":\"vinsight\",\"LoweredName\":null,\"Id\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"Description\":null,\"EntityKey\":null},\"ApplicationReference\":{\"EntityKey\":null},\"UsersInRoles\":[{\"$ref\":\"2\"}],\"ResourcePermissions\":[],\"EntityKey\":null},\"RoleReference\":{\"EntityKey\":null},\"User\":{\"$ref\":\"1\"},\"UserReference\":{\"EntityKey\":null},\"EntityKey\":null},{\"$id\":\"6\",\"EntryNum\":20,\"UserId\":\"b2f29128-24c0-4a8a-b3cd-8bfc0378bd72\",\"RoleId\":\"3e744d7f-d43d-41f4-ac4a-4bc1ac6fb1de\",\"OrganisationId\":\"c663e81d-57e2-4ba6-a40c-6c587101ae95\",\"Organisation\":{\"$id\":\"7\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Name\":\"Vinsight Support\",\"LoweredName\":null,\"Connections\":[],\" UsersInRoles\":[{\"$ref\":\"6\"}],\"Subscriptions\":[],\"Applications\":[],\"Users\":[],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},\"OrganisationReference\":{\"EntityKey\":null},\"Role\":{\"$id\":\"8\",\"ApplicationId\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"Id\":\"3e744d7f-d43d-41f4-ac4a-4bc1ac6fb1de\",\"Name\":\"ApplicationAdmins\",\"LoweredName\":\"applicationadmins\",\"Description\":null,\"IsReadOnly\":false,\"Application\":{\"$id\":\"9\",\"Name\":\"vinsight\",\"LoweredName\":null,\"Id\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"Description\":null,\"EntityKey\":null},\"ApplicationReference\":{\"EntityKey\":null},\"UsersInRoles\":[{\"$ref\":\"6\"}],\"ResourcePermissions\":[],\"EntityKey\":null},\"RoleReference\":{\"EntityKey\":null},\"User\":{\"$ref\":\"1\"},\"UserReference\":{\"EntityKey\":null},\"EntityKey\":null},{\"$id\":\"10\",\"EntryNum\":28,\"UserId\":\"b2f29128-24c0-4a8a-b3cd-8bfc0378bd72\",\"RoleId\":\" d17a5b3f-5394-4ebd-af11-56468974fce5\",\"OrganisationId\":\"c663e81d-57e2-4ba6-a40c-6c587101ae95\",\"Organisation\":{\"$id\":\"11\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Name\":\"Vinsight Support\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[{\"$ref\":\"10\"}],\"Subscriptions\":[],\"Applications\":[],\"Users\":[],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},\"OrganisationReference\":{\"EntityKey\":null},\"Role\":{\"$id\":\"12\",\"ApplicationId\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"Id\":\"d17a5b3f-5394-4ebd-af11-56468974fce5\",\"Name\":\"Users\",\"LoweredName\":\"users\",\"Description\":null,\"IsReadOnly\":false,\"Application\":{\"$id\":\"13\",\"Name\":\"vinsight\",\"LoweredName\":null,\"Id\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"Description\":null,\"EntityKey\":null},\"ApplicationReference\":{\"EntityKey\":null},\"UsersInRoles\":[{\"$ref\":\"10\"}],\"ResourcePermissions\":[],\"EntityK ey\":null},\"RoleReference\":{\"EntityKey\":null},\"User\":{\"$ref\":\"1\"},\"UserReference\":{\"EntityKey\":null},\"EntityKey\":null}],\"Subscriptions\":[],\"Organisations\":[{\"$id\":\"14\",\"Id\":\"3e8b7d0a-8155-405d-8729-3811dd14a5fe\",\"Name\":\"Empty Test\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"15\",\"Id\":\"6ab925a8-07e1-444d-aa9c-3af1f6c5a670\",\"Name\":\"My Test Company9\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"16\",\"Id\":\"a40be8bb-e71f-4363-a042-437e401ab1ca\",\"Name\":\"My Test Company11\",\"LoweredName\":null,\"Connections\":[],\" UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"17\",\"Id\":\"284440de-0a41-4c57-9984-4d19af8f20fb\",\"Name\":\"My Test Company6\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"18\",\"Id\":\"c663e81d-57e2-4ba6-a40c-6c587101ae95\",\"Name\":\"Vinsight Support\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"19\",\"Id\":\"c3c3f80c-2b34-4ba2-9e7f-9582a2c44ca9\",\"Name\":\"My Test C ompany5\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"20\",\"Id\":\"f345c188-d288-4fc2-a269-bb9229eb0475\",\"Name\":\"My Test Company7\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"21\",\"Id\":\"429db3c7-d022-4ee6-afcb-ec99d6236a1b\",\"Name\":\"My Test Company8\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null},{\"$id\":\"22\",\"Id\":\"aeaae08 2-0fe4-4b98-b4da-ffba8b430e11\",\"Name\":\"My Test Company4\",\"LoweredName\":null,\"Connections\":[],\"UsersInRoles\":[],\"Subscriptions\":[],\"Applications\":[],\"Users\":[{\"$ref\":\"1\"}],\"RequestLogs\":[],\"ResourcePermissions\":[],\"ApplicationsSettings\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"EntityKey\":null}],\"ApplicationsSettings\":[{\"$id\":\"23\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"InheritSettingsId\":null,\"UserId\":null,\"ApplicationId\":\"700d72d5-946e-4fb0-aff2-75c4f61fd2c9\",\"DefaultUrl\":\"/Dashboards/Sales\",\"DockItems\":null,\"LastLoginContext\":null,\"CompanyRole\":null,\"Language\":\"en-NZ\",\"LanguageVariant\":null,\"OrganisationId\":null,\"Application\":null,\"ApplicationReference\":{\"EntityKey\":null},\"DerivedSettings\":[],\"BaseSettings\":null,\"BaseSettingsReference\":{\"EntityKey\":null},\"User\":{\"$ref\":\"1\"},\"UserReference\":{\"EntityKey\":null},\"Organisation\":null,\"OrganisationReference\":{\"EntityKey\":null},\"EntityKey\":null},{\"$id\":\"24\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"InheritSettingsId\":null,\"UserId\":null,\"ApplicationId\":\"06503493-67d9-4ccb-aa48-9c06649c5085\",\"DefaultUrl\":\"/\",\"DockItems\":null,\"LastLoginContext\":null,\"CompanyRole\":null,\"Language\":null,\"LanguageVariant\":null,\"OrganisationId\":null,\"Application\":null,\"ApplicationReference\":{\"EntityKey\":null},\"DerivedSettings\":[],\"BaseSettings\":null,\"BaseSettingsReference\":{\"EntityKey\":null},\"User\":{\"$ref\":\"1\"},\"UserReference\":{\"EntityKey\":null},\"Organisation\":null,\"OrganisationReference\":{\"EntityKey\":null},\"EntityKey\":null}],\"UsersSettings\":[],\"RequestLogs\":[],\"ResourcePermissions\":[],\"AuditLogEntries\":[],\"UserFacets\":[],\"ApiKey\":null,\"ApiUserCreatedByUser\":null,\"EntityKey\":null}");

            User user = null;

            var startedAt = DateTime.Now;

            for (var i = 0; i < 100; i++)
            {
                bytes = serializer.Serialize(userObject);
                user  = serializer.Deserialize(bytes, typeof(User)) as User;
            }

            var endedAt = DateTime.Now;

            var timeTaken = (endedAt - startedAt).Milliseconds / 100.0;

            Assert.IsNotNull(user);
            Assert.AreEqual("*****@*****.**", user.UserName);
            Assert.AreEqual("Vinsight", user.FirstName);

            Trace.WriteLine(name + ": " + timeTaken + " ms, " + bytes.Length + " bytes");
        }
예제 #2
0
        private ReadOnlyMemory <byte> Serialize <TV>(TV value)
        {
            Stream stream = _manager.GetStream();

            try
            {
                _serializer.Serialize(value, ref stream);

                if (stream.Length > _options.MaxValueLength)
                {
                    throw new InvalidOperationException(string.Format(null, Res.Value_is_Too_Large, stream.Length, _options.MaxKeyLength));
                }

                if (stream is MemoryStream ms)
                {
                    return(ms.ToArray());
                }
                if (stream is ReadOnlyMemoryStream roms)
                {
                    return(roms.Memory);
                }

                using var s = new MemoryStream();
                stream.CopyTo(s);
                return(s.ToArray());
            }
            finally
            {
                stream.Dispose();
            }
        }
예제 #3
0
        public static ReadOnlyMemory <byte> Serialize <TV>(this IValueSerializer serializer, [AllowNull] TV value)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            Stream stream = new MemoryStream();

            try
            {
                serializer.Serialize(value, ref stream);

                if (stream is MemoryStream ms)
                {
                    return(ms.ToArray());
                }
                if (stream is ReadOnlyMemoryStream roms)
                {
                    return(roms.Memory);
                }

                using var s = new MemoryStream();
                stream.CopyTo(s);
                return(s.ToArray());
            }
            finally
            {
                stream.Dispose();
            }
        }
예제 #4
0
        private string DeserializeString(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

            return((string)_stringSerializer.Serialize(value.GetString()));
        }
예제 #5
0
        private double?DeserializeFloat(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

            return((double?)_floatSerializer.Serialize(value.GetDouble()));
        }
        private object SerializeNullableFloat(object value)
        {
            if (value is null)
            {
                return(null);
            }


            return(_floatSerializer.Serialize(value));
        }
        private object SerializeNullableInt(object value)
        {
            if (value is null)
            {
                return(null);
            }


            return(_intSerializer.Serialize(value));
        }
        private object SerializeNullableBoolean(object value)
        {
            if (value is null)
            {
                return(null);
            }


            return(_booleanSerializer.Serialize(value));
        }
예제 #9
0
        private object SerializeNullableString(object value)
        {
            if (value is null)
            {
                return(null);
            }


            return(_stringSerializer.Serialize(value));
        }
예제 #10
0
        private object SerializeNullableUrl(object value)
        {
            if (value is null)
            {
                return(null);
            }


            return(_urlSerializer.Serialize(value));
        }
예제 #11
0
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, TField value) where TBufferWriter : IBufferWriter <byte>
        {
            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteStartObject(fieldIdDelta, expectedType, CodecFieldType);
            TSurrogate surrogate = default;

            ConvertToSurrogate(value, ref surrogate);
            _surrogateSerializer.Serialize(ref writer, ref surrogate);
            writer.WriteEndObject();
        }
예제 #12
0
        } // todo: highlight elements of free array with color (chessboard-ish order)    with   ((index.X + index.Y) & 1) ? color1 : color2

        void WriteValue(object value, V2Int pos)
        {
            for (int i = Values.Count; i <= pos.X; i++)
            {
                Values.Add(new List <object>());
            }
            for (int i = Values[pos.X].Count; i < pos.Y; i++)
            {
                Values[pos.X].Add(null);
            }
            Values[pos.X].Add(serializer.Serialize(value));
        }
예제 #13
0
        private string?DeserializeNullableString(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

            if (value.ValueKind == JsonValueKind.Null)
            {
                return(null);
            }

            return((string?)_stringSerializer.Serialize(value.GetString()) !);
        }
예제 #14
0
        private object?SerializeVariable(
            object?obj,
            IValueSerializer serializer)
        {
            if (obj is IList list)
            {
                var serialized = new List <object?>();

                foreach (object?element in list)
                {
                    serialized.Add(SerializeVariable(element, serializer));
                }

                return(serializer);
            }

            return(serializer.Serialize(obj));
        }
예제 #15
0
        public override void Set(string key, object value, int?expireSeconds = null)
        {
            key = KeyForBucket(key);
            var connection = GetConnection();

            if (value is String)
            {
                if (expireSeconds.HasValue && expireSeconds.Value > 0)
                {
                    connection.SetWithExpiry(0, key, expireSeconds.Value, (String)value);
                }
                else
                {
                    connection.Set(0, key, (String)value);
                }
            }
            else if (value is Byte[])
            {
                if (expireSeconds.HasValue && expireSeconds.Value > 0)
                {
                    connection.SetWithExpiry(0, key, expireSeconds.Value, (Byte[])value);
                }
                else
                {
                    connection.Set(0, key, (Byte[])value);
                }
            }
            else
            {
                var bytes = cacheItemSerializer.Serialize(value);
                if (expireSeconds.HasValue && expireSeconds.Value > 0)
                {
                    connection.SetWithExpiry(0, key, expireSeconds.Value, bytes);
                }
                else
                {
                    connection.Set(0, key, bytes);
                }
            }
        }
예제 #16
0
        /// <inheritdoc/>
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, TField value) where TBufferWriter : IBufferWriter <byte>
        {
            if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value))
            {
                return;
            }

            var fieldType = value.GetType();

            if (fieldType == CodecFieldType)
            {
                writer.WriteStartObject(fieldIdDelta, expectedType, fieldType);
                TSurrogate surrogate = default;
                ConvertToSurrogate(value, ref surrogate);
                _surrogateSerializer.Serialize(ref writer, ref surrogate);
                writer.WriteEndObject();
            }
            else
            {
                SerializeUnexpectedType(ref writer, fieldIdDelta, expectedType, value, fieldType);
            }
        }
예제 #17
0
        public void Serialize <TV>([AllowNull] TV value, ref Stream stream)
        {
            _serializer.Serialize(value, ref stream);
            if (stream.Length <= 10)
            {
                return;
            }

            var cms = _manager.GetStream();

            using (var gzip = new GZipStream(cms, CompressionMode.Compress))
                stream.CopyTo(gzip);

            if (cms.Length < stream.Length)
            {
                stream.Dispose();

                stream = cms;
            }
            else
            {
                cms.Dispose();
            }
        }
 public void Export(string filePath, IModel model) =>
 _DataWriter.Write(
     _fileStreamer.GetStream(filePath),
     _valueSerializer.Serialize(
         _modelReader.Read(model)));
    public void Serialize <TBufferWriter>(ref Writer <TBufferWriter> writer, ref TField value) where TBufferWriter : IBufferWriter <byte>
    {
        var surrogate = _converter.ConvertToSurrogate(in value);

        _surrogateSerializer.Serialize(ref writer, ref surrogate);
    }
 private object SerializeNullableInt(object value)
 {
     return(_intSerializer.Serialize(value));
 }
 private object SerializeNullableUuid(object value)
 {
     return(_uuidSerializer.Serialize(value));
 }
예제 #22
0
        private void Set(string name, object value)
        {
            if (value != null && namesAdded.Contains(name))
            {
                if (value.Equals(persistentValues.ContainsKey(name) ? persistentValues[name] : null))
                {
                    return;
                }
            }
            var bytes = serializer.Serialize(value);

            byte[] storedBytes;
            if (GetRawItems().TryGetValue(prefix + name, out storedBytes) &&
                bytes.SequenceEqual(storedBytes))
            {
                return;
            }

            var itemsToSet = new Dictionary <string, byte[]>(1);

            itemsToSet.Add(prefix + name, bytes);
            //itemsToSet.Add(TYPE_PREFIX+name, Encoding.ASCII.GetBytes(value.GetType().AssemblyQualifiedName));
            var setTask = redis.Hashes.Set(0, GetKeyForSession(), itemsToSet);

            SetTasks.Add(setTask);

            OneTimeResetTimeout();

            if (rawItems.ContainsKey(prefix + name))
            {
                rawItems[prefix + name] = bytes;
            }
            else
            {
                rawItems.Add(prefix + name, bytes);
            }

            if (persistentValues.ContainsKey(name))
            {
                persistentValues.Remove(name);
            }
            var persistentCopy = serializer.Deserialize(bytes);

            persistentValues.Add(name, persistentCopy);

            if (!namesAdded.Contains(name))
            {
                namesAdded.Add(name);
                if (keysAdded && BaseGetAllKeys().Contains(name))
                {
                    BaseSet(name, value);
                }
                else
                {
                    BaseAdd(name, value);
                }
            }
            else
            {
                BaseSet(name, value);
            }
        }
예제 #23
0
        private System.DateTimeOffset DeserializeDateTime(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((System.DateTimeOffset)_dateTimeSerializer.Serialize(value.GetString()) !);
        }
 private object SerializeNullableCatalogProductDtoFilter(object value)
 {
     return(_catalogProductDtoFilterSerializer.Serialize(value));
 }
예제 #25
0
        public Task SetAsync(TValue value, DistributedCacheEntryOptions options, CancellationToken token = default)
        {
            var bytes = _valueSerializer.Serialize(value);

            return(_cache.SetAsync(_key, bytes, options, token));
        }
예제 #26
0
        private int DeserializeInt(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((int)_intSerializer.Serialize(value.GetInt32()) !);
        }