Exemple #1
0
        void ISerializerInternal.SerializeInternal(object obj, SerializationContext context, StringBuilder sb)
        {
            if (obj == null)
            {
                sb.Append(EmptySymbol);
                return;
            }

            if (obj.Equals(EmptyValue))
            {
                sb.Append(EmptySymbol);
                return;
            }

            // if the object is already cached
            // then only put the id and data necessary to get it back from cache on deserialization
            if (context.TryGet(obj, out int reference))
            {
                sb.Append(NodeParser.WrappingStart);
                sb.Append('\n');
                context.TabPadding++;
                SerializerUtils.GetTabSpaces(context.TabPadding, sb);

                WriteHeader(sb, reference);

                sb.Append('\n');
                context.TabPadding--;
                SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                sb.Append(NodeParser.WrappingEnd);
                return;
            }

            // else write the object
            WriteObjectData(obj, context, sb);
        }
Exemple #2
0
        internal override void WriteObjectData(object obj, SerializationContext context, StringBuilder sb)
        {
            // else then we deserialize the data inside
            sb.Append(NodeParser.WrappingStart);
            sb.Append('\n');
            context.TabPadding++;
            SerializerUtils.GetTabSpaces(context.TabPadding, sb);

            context.Register(obj, out int newRef);

            WriteHeader(sb, newRef);
            sb.Append('\n');

            if (PropertiesCount != 0)
            {
                for (int i = 0; i < PropertiesCount - 1; i++)
                {
                    object val = PropertieGetter[i].Invoke(obj);



                    if (context.WithPropertiesComments)
                    {
                        SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                        sb.Append($"# { PropertiesName[i] } #");
                        sb.Append('\n');
                    }

                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);

                    Serializers[i].SerializeInternal(val, context, sb);

                    sb.Append(SerializerConsts.DATA_SEPARATOR);
                    sb.Append('\n');
                }

                if (context.WithPropertiesComments)
                {
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    sb.Append($"# { PropertiesName[PropertiesCount - 1] } #");
                    sb.Append('\n');
                }

                if (PropertiesCount - 1 >= 0)
                {
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    object lastVal = PropertieGetter[PropertiesCount - 1].Invoke(obj);
                    Serializers[PropertiesCount - 1].SerializeInternal(lastVal, context, sb);
                }
            }

            sb.Append('\n');
            context.TabPadding--;
            SerializerUtils.GetTabSpaces(context.TabPadding, sb);
            sb.Append(NodeParser.WrappingEnd);
        }
        internal override void WriteObjectData(object obj, SerializationContext context, StringBuilder sb)
        {
            // else then we deserialize the data inside
            sb.Append(NodeParser.WrappingStart);
            sb.Append('\n');
            context.TabPadding++;
            SerializerUtils.GetTabSpaces(context.TabPadding, sb);

            context.Register(obj, out int newRef);

            WriteHeader(sb, newRef);
            sb.Append('\n');

            IEnumerable cast = (IEnumerable)obj;

            int index = 0;

            foreach (object element in cast)
            {
                Type elementType = element.GetType();


                if (context.WithPropertiesComments)
                {
                    sb.Append('\n');
                    sb.Append('\n');
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    sb.Append($"# [{ index }] #");
                }

                ISerializerInternal elementSerialiazer = SerializerDependencies.SerializerCollection.GetOrAdd(elementType);
                sb.Append('\n');
                SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                elementSerialiazer.SerializeInternal(element, context, sb);
                sb.Append(SerializerConsts.DATA_SEPARATOR);
                index++;
            }

            sb.Remove(sb.Length - 1, 1);
            sb.Append('\n');
            context.TabPadding--;
            SerializerUtils.GetTabSpaces(context.TabPadding, sb);
            sb.Append(NodeParser.WrappingEnd);
        }
Exemple #4
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);
            }
        }
        internal override void WriteObjectData(object obj, SerializationContext context, StringBuilder sb)
        {
            // else then we deserialize the data inside
            sb.Append(NodeParser.WrappingStart);
            sb.Append('\n');
            context.TabPadding++;
            SerializerUtils.GetTabSpaces(context.TabPadding, sb);

            context.Register(obj, out int newRef);

            WriteHeader(sb, newRef);
            sb.Append('\n');

            IDictionary cast = (IDictionary)obj;

            ICollection keys = cast.Keys;

            int index = 0;

            if (keys.Count != 0)
            {
                foreach (object key in keys)
                {
                    Type keyType = key.GetType();

                    var value = cast[key];

                    Type valueType = value.GetType();

                    if (context.WithPropertiesComments)
                    {
                        sb.Append('\n');
                        sb.Append('\n');
                        SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                        sb.Append($"# [{ index }] #");
                    }

                    ISerializerInternal keySerialiazer   = SerializerDependencies.SerializerCollection.GetOrAdd(keyType);
                    ISerializerInternal valueSerialiazer = SerializerDependencies.SerializerCollection.GetOrAdd(valueType);
                    sb.Append('\n');
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);

                    // kv pair
                    sb.Append('{');
                    sb.Append('\n');
                    context.TabPadding++;

                    // key
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    sb.Append("# key #");
                    sb.Append('\n');
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    keySerialiazer.SerializeInternal(key, context, sb);
                    sb.Append(SerializerConsts.DATA_SEPARATOR);
                    sb.Append('\n');

                    // value
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    sb.Append("# value #");
                    sb.Append('\n');
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    valueSerialiazer.SerializeInternal(value, context, sb);
                    context.TabPadding--;
                    sb.Append('\n');
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);
                    sb.Append('}');
                    sb.Append(SerializerConsts.DATA_SEPARATOR);
                    SerializerUtils.GetTabSpaces(context.TabPadding, sb);

                    index++;
                }

                sb.Remove(sb.Length - 2, 2);
            }
            sb.Append('\n');
            context.TabPadding--;
            SerializerUtils.GetTabSpaces(context.TabPadding, sb);
            sb.Append(NodeParser.WrappingEnd);
        }