Ejemplo n.º 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");
        }
Ejemplo n.º 2
0
 public override void Get <T>(string key, Action <T, Exception> cb)
 {
     GetRaw(key, (bytes, exc) =>
     {
         T obj = default(T);
         if (exc == null)
         {
             obj = (T)cacheItemSerializer.Deserialize(bytes);
         }
         cb(obj, exc);
     });
 }
        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.Deserialize(value.GetString()));
        }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public TField ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <TField, TInput>(ref reader, field));
            }

            var placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            var fieldType = field.FieldType;

            if (fieldType is null || fieldType == CodecFieldType)
            {
                TSurrogate surrogate = default;
                _surrogateSerializer.Deserialize(ref reader, ref surrogate);
                var result = ConvertFromSurrogate(ref surrogate);
                ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
                return(result);
            }

            // The type is a descendant, not an exact match, so get the specific serializer for it.
            var specificSerializer = reader.Session.CodecProvider.GetCodec(fieldType);

            if (specificSerializer != null)
            {
                return((TField)specificSerializer.ReadValue(ref reader, field));
            }

            ThrowSerializerNotFoundException(fieldType);
            return(null);
        }
Ejemplo n.º 5
0
        public async Task <TValue?> GetAsync(CancellationToken token = default)
        {
            try
            {
                var bytes = await _cache.GetAsync(_key, token);

                var cached = (TValue?)_valueSerializer.Deserialize(bytes, typeof(TValue));

                if (cached != null)
                {
                    return(cached);
                }
            }
            catch
            {
                if (_valueFactory == null || !_fallbackToFactoryOnException)
                {
                    throw;
                }
            }

            if (_valueFactory == null)
            {
                return(null);
            }

            var value = await _valueFactory(_serviceProvider, token);

            if (value != null)
            {
                await SetAsync(value, token);
            }

            return(value);
        }
    public void Deserialize <TInput>(ref Reader <TInput> reader, ref TField value)
    {
        TSurrogate surrogate = default;

        _surrogateSerializer.Deserialize(ref reader, ref surrogate);
        value = _converter.ConvertFromSurrogate(in surrogate);
    }
Ejemplo n.º 7
0
        public TField ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            ReferenceCodec.MarkValueField(reader.Session);
            TSurrogate surrogate = default;

            _surrogateSerializer.Deserialize(ref reader, ref surrogate);
            var result = ConvertFromSurrogate(ref surrogate);

            return(result);
        }
Ejemplo n.º 8
0
        private void AddFieldToBaseFromRaw(string name)
        {
            AddKeysToBase();
            lock (deserializeLock) {
                if (!GetRawItems().ContainsKey(prefix + name))
                {
                    return;
                }

                //var typeName = Encoding.ASCII.GetString(GetRawItems()[TYPE_PREFIX + name]);
                //var fieldClrType = Type.GetType(typeName);

                var bytes = GetRawItems()[prefix + name];

                var valueToAdd     = serializer.Deserialize(bytes);
                var persistentCopy = serializer.Deserialize(bytes);

                BaseSet(name, valueToAdd);
                persistentValues.Add(name, persistentCopy);
            }
        }
Ejemplo n.º 9
0
        private double?DeserializeNullableFloat(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

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

            return((double?)_floatSerializer.Deserialize(value.GetDouble()) !);
        }
Ejemplo n.º 10
0
        private int?DeserializeNullableInt(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

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

            return((int?)_intSerializer.Deserialize(value.GetInt32()));
        }
Ejemplo n.º 11
0
        private System.DateTimeOffset?DeserializeNullableDateTime(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

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

            return((System.DateTimeOffset?)_dateTimeSerializer.Deserialize(value.GetString()));
        }
        private System.Uri?DeserializeNullableUrl(JsonElement obj, string fieldName)
        {
            if (!obj.TryGetProperty(fieldName, out JsonElement value))
            {
                return(null);
            }

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

            return((System.Uri?)_urlSerializer.Deserialize(value.GetString()) !);
        }
Ejemplo n.º 13
0
        public TField ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <TField, TInput>(ref reader, field));
            }

            var        placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            TSurrogate surrogate = default;

            _surrogateSerializer.Deserialize(ref reader, ref surrogate);
            var result = ConvertFromSurrogate(ref surrogate);

            ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
            return(result);
        }
Ejemplo n.º 14
0
        public TV Deserialize <TV>(ReadOnlyMemory <byte>?data)
        {
            if (IsPossiblyGZipped(data))
            {
                try
                {
                    using var stream = new MemoryStream(data !.Value.ToArray());

                    using var dms = new MemoryStream();
                    using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
                        gzip.CopyTo(dms);

                    data = dms.ToArray();
                }
                catch (InvalidDataException)
                {
                }
            }

            return(_serializer.Deserialize <TV>(data));
        }
Ejemplo n.º 15
0
        private System.Guid DeserializeUuid(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((System.Guid)_uuidSerializer.Deserialize(value.GetString()));
        }
        private int DeserializeInt(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((int)_intSerializer.Deserialize(value.GetInt32()));
        }
Ejemplo n.º 17
0
        private ThumbnailSizeName DeserializeThumbnailSizeName(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((ThumbnailSizeName)_thumbnailSizeNameSerializer.Deserialize(value.GetString()));
        }
Ejemplo n.º 18
0
        private bool DeserializeBoolean(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((bool)_booleanSerializer.Deserialize(value.GetBoolean()) !);
        }
Ejemplo n.º 19
0
        private ResolutionType DeserializeResolutionType(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((ResolutionType)_resolutionTypeSerializer.Deserialize(value.GetString()) !);
        }
Ejemplo n.º 20
0
        private string DeserializeString(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((string)_stringSerializer.Deserialize(value.GetString()) !);
        }
Ejemplo n.º 21
0
        private IssueType DeserializeIssueType(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((IssueType)_issueTypeSerializer.Deserialize(value.GetString()) !);
        }
        private System.DateTimeOffset DeserializeDateTime(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((System.DateTimeOffset)_dateTimeSerializer.Deserialize(value.GetString()) !);
        }
Ejemplo n.º 23
0
        private double DeserializeFloat(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((double)_floatSerializer.Deserialize(value.GetDouble()));
        }
Ejemplo n.º 24
0
 public TV Get <TK, TV>(TK key, CancellationToken token) =>
 _serializer.Deserialize <TV>(_provider.Get(ParseKey(key), token));
        private Direction DeserializeDirection(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((Direction)_directionSerializer.Deserialize(value.GetString()) !);
        }
Ejemplo n.º 26
0
        private long DeserializeLong(JsonElement obj, string fieldName)
        {
            JsonElement value = obj.GetProperty(fieldName);

            return((long)_longSerializer.Deserialize(value.GetInt64()));
        }