public void LoadDefaultSerializers()
        {
            var serializers = new SanityHtmlSerializers();

            Serializers.Add("block", serializers.SerializeDefaultBlockAsync);
            Serializers.Add("image", serializers.SerializeImageAsync);
        }
Example #2
0
        public void StatesSerialization()
        {
            var comps = new Components();

            comps.Initialize(100);
            comps.Add(1, new ME.ECS.Views.ViewComponent()
            {
                seed = 123u, viewInfo = new ME.ECS.Views.ViewInfo(Entity.Empty, 12, 23)
            });

            var test = new TestState()
            {
                components = comps
            };

            var ser = new Serializers();

            ser.Add(new BufferArraySerializer());

            var bytes = Serializer.Pack(test, ser);

            var testRes = Serializer.Unpack <TestState>(bytes, ser);

            NUnit.Framework.Assert.AreEqual(test.components.GetData(1).Count, testRes.components.GetData(1).Count);
        }
        public void BufferArraySerialization()
        {
            var test = new TestDataBufferArray {
                viewInfo         = new ME.ECS.Views.ViewInfo(Entity.Empty, 12, 23),
                bufferComponents = new ME.ECS.Collections.BufferArray <ME.ECS.Views.ViewComponent>(new [] {
                    new ME.ECS.Views.ViewComponent {
                        seed = 123u, viewInfo = new ME.ECS.Views.ViewInfo(Entity.Empty, 12, 23)
                    },
                    null,
                    null,
                    null,
                    null,
                    new ME.ECS.Views.ViewComponent {
                        seed = 123u, viewInfo = new ME.ECS.Views.ViewInfo(Entity.Empty, 12, 23)
                    },
                    null,
                    null,
                }, 6),
                buffer = new ME.ECS.Collections.BufferArray <MyStruct>(new[] {
                    new MyStruct {
                        bar = 1, foo = 2
                    },
                    new MyStruct {
                        bar = 2, foo = 3
                    },
                    new MyStruct {
                        bar = 4, foo = 5
                    },
                    new MyStruct {
                        bar = 6, foo = 7
                    },
                    new MyStruct {
                        bar = 8, foo = 9
                    }
                }, 5)
            };

            byte[] bytes;
            {
                var ser = new Serializers();
                ser.Add(new BufferArraySerializer());

                bytes = Serializer.Pack(test, ser);
                ser.Dispose();
            }

            {
                var ser = new Serializers();
                ser.Add(new BufferArraySerializer());

                var testRes = Serializer.Unpack <TestDataBufferArray>(bytes, ser);
                ser.Dispose();
                for (int i = 0; i < test.buffer.Length; ++i)
                {
                    NUnit.Framework.Assert.AreEqual(test.buffer.arr[i], testRes.buffer.arr[i]);
                }
            }
        }
Example #4
0
        public IHttpConsumer AddSerializer(params ISerializer[] serializers)
        {
            foreach (ISerializer serializer in serializers)
            {
                Serializers.Add(serializer);
            }

            return(this);
        }
Example #5
0
        public virtual UiState DefaultSerializers()
        {
            Serializers.Clear();
            Serializers.Add(typeof(TextBox), control => control.Text);
            Serializers.Add(typeof(KeywordTextBox), control => ((KeywordTextBox)control).Text);
            Serializers.Add(typeof(RadioButton), control => ((RadioButton)control).Checked.ToString());
            Serializers.Add(typeof(CheckBox), control => ((CheckBox)control).Checked.ToString());
            Serializers.Add(typeof(NumericUpDown), control => ((NumericUpDown)control).Value.ToString());
            Serializers.Add(typeof(DateTimePicker), control => ((DateTimePicker)control).Value.ToString());
            Serializers.Add(typeof(ComboBox), control => {
                ComboBox cbx = (ComboBox)control;

                //   if (cbx.Items.Count == 0) // No items in combobox, dont serialize selected item, as we don't know the item type.
                //        return null;

                return(cbx.SelectedIndex.ToString());
            });
            Serializers.Add(typeof(DataGridView), control => {
                StringBuilder sb = new StringBuilder();

                DataGridView dgv = (DataGridView)control;
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    if (row.IsNewRow)
                    {
                        continue;
                    }
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        string value = cell.Value?.ToString();
                        if (cell.OwningColumn.GetType() != typeof(DataGridViewTextBoxColumn))
                        {
                            value = null;
                        }

                        sb.Append(value).Append(',');
                    }

                    if (row.Cells.Count > 0)
                    {
                        sb.Remove(sb.Length - 1, 1);
                    }
                    sb.Append('|');
                }
                if (dgv.Rows.Count > 0 && sb.Length > 0)
                {
                    sb.Remove(sb.Length - 1, 1);
                }
                return(sb.ToString());
            });
            return(this);
        }
Example #6
0
 protected Serializer <TV> GetSerializer(ISerializable serializableObject)
 {
     try
     {
         var serializer = GetInternalSerializer(serializableObject);
         Serializers.Add(serializer);
         return(serializer);
     }
     catch (Exception e)
     {
         OnErrorEvent(e);
         throw;
     }
 }
Example #7
0
 private CollectionSerializer <TC> GetCollectionSerializer(ISerializable serializableObject)
 {
     try
     {
         var serializer = InternalGetCollectionSerializer(serializableObject);
         Serializers.Add(serializer);
         return(serializer);
     }
     catch (Exception e)
     {
         OnErrorEvent(e);
         throw;
     }
 }
Example #8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="serializers">The serializers.</param>
 public SerialBox(IEnumerable <ISerializer> serializers)
 {
     serializers ??= new List <ISerializer>();
     Serializers = serializers.Where(x => !x.GetType().Namespace.StartsWith("SERIALBOX", StringComparison.OrdinalIgnoreCase))
                   .ToDictionary(x => x.ContentType);
     foreach (var Serializer in serializers.Where(x => x.GetType()
                                                  .Namespace
                                                  .StartsWith("SERIALBOX", StringComparison.OrdinalIgnoreCase)))
     {
         if (!Serializers.ContainsKey(Serializer.ContentType))
         {
             Serializers.Add(Serializer.ContentType, Serializer);
         }
     }
 }
Example #9
0
        private void Initialize()
        {
            // get properties
            List <PropertyInfo> props = GetSerializableProperties(Type);

            PropertiesCount = props.Count;

            for (int i = 0; i < props.Count; i++)
            {
                PropertyInfo            prop   = props[i];
                Func <object, object>   getter = SerializerUtils.PropertyGetterToDelegate(prop.GetMethod);
                Action <object, object> setter = SerializerUtils.PropertySetterToDelegate(prop.SetMethod);


                ISerializerInternal serializer = SerializerDependencies.SerializerCollection.GetOrAdd(prop.PropertyType);

                PropertieGetter.Add(getter);
                PropertieSetter.Add(setter);
                Serializers.Add(serializer);
                PropertiesName.Add(prop.Name);
            }

            // get fields
            List <FieldInfo> fields = GetSerializableFields(Type);

            var FieldsCount = fields.Count;

            PropertiesCount += FieldsCount;

            for (int i = 0; i < fields.Count; i++)
            {
                FieldInfo               field  = fields[i];
                Func <object, object>   getter = SerializerUtils.FieldGetterToDelegate(field);
                Action <object, object> setter = SerializerUtils.FieldSetterToDelegate(field);

                ISerializerInternal serializer = SerializerDependencies.SerializerCollection.GetOrAdd(field.FieldType);

                PropertieGetter.Add(getter);
                PropertieSetter.Add(setter);
                Serializers.Add(serializer);
                PropertiesName.Add(field.Name);
            }
        }
Example #10
0
        public static XmlSerializer GetSerializer(Type type, string customKey, SerializerFactory factory)
        {
            string key = customKey + type.AssemblyQualifiedName;

            if (Serializers.ContainsKey(key))
            {
                return(Serializers[key]);
            }
            XmlSerializer serializer = factory();

            if (Serializers.ContainsKey(key))
            {
                return(Serializers[key]);
            }
            lock (_syncRoot)
            {
                Serializers.Add(key, serializer);
            }
            return(serializer);
        }
        public static State Load(IStorageEngine storageEngine, ISet <object> ephemeralInstances)
        {
            var valuesDictionaries = new DictionaryWithDefault <long, Dictionary <string, object> >(_ => new Dictionary <string, object>());

            //Load all entries from log file
            var storedState = storageEngine.Load();

            var serializers     = new Serializers(new SerializerFactory());
            var stateMaps       = new StateMaps(serializers);
            var entriesPerOwner = storedState.StorageEntries;
            //var entriesPerOwner = storedState.StorageEntries;

            var serializersTypeEntries = storedState.Serializers;

            var toDeserialize = new Queue <long>();
            var referenced    = new DictionaryWithDefault <long, List <Reference> >(_ => new List <Reference>());

            ISerializer Deserialize(long id)
            {
                if (serializers.ContainsKey(id))
                {
                    return(serializers[id]);
                }

                var ownerEntries = entriesPerOwner.ContainsKey(id)
                    ? entriesPerOwner[id]
                    : Enumerable.Empty <StorageEntry>();

                var resolvedValues = valuesDictionaries[id];

                //deserialize non-referencing values
                var resolvableEntries = ownerEntries
                                        .Where(e => !e.Reference.HasValue)
                                        .ToList();

                foreach (var resolvableEntry in resolvableEntries)
                {
                    resolvedValues[resolvableEntry.Key] = resolvableEntry.Value;
                }

                var referencedEntries = ownerEntries.Where(e => e.Reference.HasValue);

                foreach (var referencedEntry in referencedEntries)
                {
                    // ReSharper disable once PossibleInvalidOperationException
                    var wp = Deserialize(referencedEntry.Reference.Value);
                    resolvedValues[referencedEntry.Key] = wp.Instance;
                }

                var serializer = (ISerializer)DeserializationMethodInvoker
                                 .Invoke(
                    id,
                    serializersTypeEntries[id],
                    valuesDictionaries[id],
                    ephemeralInstances
                    );

                stateMaps[id] = new StateMap(serializers, resolvedValues);
                serializers.Add(serializer);

                if (serializer.Instance is Reference r && r.Id.HasValue)
                {
                    toDeserialize.Enqueue(r.Id.Value);
                    referenced[r.Id.Value].Add(r);
                }

                return(serializer);
            }

            var roots = (Roots)Deserialize(0).Instance;

            ephemeralInstances.Add(roots);

            while (toDeserialize.Any())
            {
                Deserialize(toDeserialize.Dequeue());
            }

            foreach (var serializer in serializers)
            {
                foreach (var reference in referenced[serializer.Id])
                {
                    reference.SetSerializer(serializer);
                }
            }

            return(new State(roots, stateMaps, serializers));
        }
Example #12
0
 public void Register(Typeserializer x)
 {
     Serializers.Add(x.Name, x);
 }