Exemplo n.º 1
0
        public void LargeModeDeserialize()
        {
            var sharedDictionary = new SharedDictionary(new SystemTextJsonSerializer(SerializeMode.LargeObject), null, null);

            sharedDictionary.SetProperty("key1", "string");
            sharedDictionary.SetProperty("key2", 1);
            byte[] bytes = sharedDictionary.RawExport();
            Assert.AreEqual(Encoding.UTF8.GetString(bytes), JsonConstant.LargeModeJson);
        }
Exemplo n.º 2
0
        public void ShortModeDeserialize()
        {
            var sharedDictionary = new SharedDictionary(SpanJsonSerializer.Create(SerializeMode.ShortObject), null, null);

            sharedDictionary.SetProperty("key1", "string");
            sharedDictionary.SetProperty("key2", 1);
            byte[] bytes = sharedDictionary.RawExport();
            Assert.AreEqual(Encoding.UTF8.GetString(bytes), JsonConstant.ShortModeJson);
        }
Exemplo n.º 3
0
        public void LargeModeNullValue()
        {
            var sharedDictionary = new SharedDictionary(new SystemTextJsonSerializer(SerializeMode.LargeObject), null, null);

            sharedDictionary.SetProperty <int?>("nullableInt", null);
            sharedDictionary.SetProperty <string?>("nullableString", null);

            byte[] binary = sharedDictionary.RawExport();
            sharedDictionary.ClearProperty();
            Assert.AreEqual(0, sharedDictionary.PropertyCount);
            sharedDictionary.RawImport(binary);

            Assert.AreEqual(null, sharedDictionary.GetProperty <int?>("nullableInt"));
            Assert.AreEqual(null, sharedDictionary.GetProperty <string?>("nullableString"));
        }
Exemplo n.º 4
0
        public void TestImplicitOperator2()
        {
            var sharedDictionary = new SharedDictionary(EmptySerializer.Default, null, null);

            sharedDictionary.SetProperty(key, new ImplicitOperatableSource2());
            sharedDictionary.GetProperty <ImplicitOperatableTarget2>(key);
        }
Exemplo n.º 5
0
        public void TestNullable()
        {
            var sharedDictionary = new SharedDictionary(EmptySerializer.Default, null, null);

            sharedDictionary.SetProperty <NullableData>(key, new NullableData());
            sharedDictionary.GetProperty <NullableData?>(key);
        }
Exemplo n.º 6
0
        public void TestContravariance()
        {
            var sharedDictionary = new SharedDictionary(EmptySerializer.Default, null, null);

            sharedDictionary.SetProperty(key, new Contravariance <object>());
            sharedDictionary.GetProperty <IContravariance <string> >(key);
        }
Exemplo n.º 7
0
        public void TestUpcast()
        {
            var sharedDictionary = new SharedDictionary(EmptySerializer.Default, null, null);

            sharedDictionary.SetProperty(key, "value");
            sharedDictionary.GetProperty <object>(key);
        }
Exemplo n.º 8
0
        public static async Task Main(string[] args)
        {
            var sharedDictionary = new SharedDictionary(Utf8JsonSerializer.Default, FileStorage.Default, null);

            sharedDictionary.SetProperty("text", "sssss");
            sharedDictionary.SetProperty("number", 1234);
            sharedDictionary.SetProperty("data", new Data());
            sharedDictionary.SetProperty("list", new List <int> {
                1, 2, 3, 4
            });

            await sharedDictionary.SaveToStorageAsync();

            sharedDictionary = new SharedDictionary(Utf8JsonSerializer.Default, FileStorage.Default, null);
            await sharedDictionary.LoadFromStorageAsync();

            foreach (var property in sharedDictionary)
            {
                WriteLine($"key: {property.Key}");
            }

            WriteLine(sharedDictionary.GetProperty <string>("text"));
            WriteLine(sharedDictionary.GetProperty <int>("number"));
            WriteLine(sharedDictionary.GetProperty <long>("number"));
            WriteLine(sharedDictionary.GetProperty <Data>("data"));
            WriteLine(sharedDictionary.GetProperty <List <int> >("list")?.Count);
            WriteLine(sharedDictionary.GetProperty <IEnumerable <int> >("list").Count());

            var serializer = new Utf8JsonSerializer();

            serializer.MigrationTypeDictionary[TypeCache <Data> .FullName] = TypeCache <MigratedData> .FullName;
            sharedDictionary = new SharedDictionary(serializer, FileStorage.Default, null);
            await sharedDictionary.LoadFromStorageAsync();

            WriteLine($"MigratedData: {sharedDictionary.GetProperty<MigratedData>("data")}");
        }
Exemplo n.º 9
0
        private async void showCount()
        {
            // if you generate code for Xamarin.iOS, you use under.

            /*Utf8Json.Resolvers.CompositeResolver.Register(
             *  // code generating resolver
             *  Utf8Json.Resolvers.GeneratedResolver.Instance,
             *  Utf8Json.Resolvers.BuiltinResolver.Instance,
             *  Utf8Json.Resolvers.AttributeFormatterResolver.Instance,
             *  Utf8Json.Resolvers.DynamicGenericResolver.Instance,
             *  Utf8Json.Resolvers.EnumResolver.Default
             * );*/
            // var formatterResolver = new Utf8JsonFormatterResolver(Utf8Json.Resolvers.CompositeResolver.Instance);

            var formatterResolver = new Utf8JsonFormatterResolver(AotStandardResolver.Default);
            var sharedDictionary  = new SharedDictionary(
                new Utf8JsonSerializer(formatterResolver),
                IsolatedFileStorage.Default,
                AesCryptoConverter.Default
                );
            await sharedDictionary.LoadFromStorageAsync();

            if (sharedDictionary.TryGetProperty(welcomeKey, out int count))
            {
                WelcomeCounter.Text = $"Welcome Count: {count}";
            }
            else
            {
                WelcomeCounter.Text = $"First Welcome!";
            }

            count++;

            sharedDictionary.SetProperty(welcomeKey, count);

            await sharedDictionary.SaveToStorageAsync();
        }