Пример #1
0
        // wellknown serialized objects
        static Context Wellknown()
        {
            var result = new Context();
            // 0 is null
            ulong index = 1;

            result.Register(index++, RuntimeType.GetType(typeof(object)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(string)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(Type)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(Nullable <>)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(IList)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(IDictionary)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(ICollection <>)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(IDictionary <,>)).TypeData());
            // other well known values, to speed up read-write and reduce stream size
            result.Register(index++, "");
            result.Register(index++, RuntimeType.GetType(typeof(byte[])).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(Guid)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(bool)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(char)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(byte)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(sbyte)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(short)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(ushort)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(int)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(uint)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(long)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(ulong)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(float)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(double)).TypeData());
            result.Register(index++, RuntimeType.GetType(typeof(decimal)).TypeData());
            return(result);
        }
Пример #2
0
        public object Read(Type type, object suggested = null)
        {
            if (suggested != null && !type.IsInstanceOfType(suggested))
                suggested = null;

            readRaw = false;
            var args = new ReadArgs(RObject.TypeData(), RuntimeType.GetType(type), suggested);
            return Read(args);
        }
Пример #3
0
 protected object AsMetaData(object obj)
 {
     if (obj is Type)
     {
         return(RuntimeType.GetType((Type)obj).TypeData());
     }
     if (obj is RuntimeType)
     {
         return(((RuntimeType)obj).TypeData());
     }
     return(obj);
 }
Пример #4
0
        /// <summary>
        /// Generates the C# class that can be used to deserialize all given types.
        /// </summary>
        /// <param name="namespace">The namespace of the generated class.</param>
        /// <param name="types">The types that will be rewritten with only the serialization information.</param>
        /// <returns>A generated C# code file as string.</returns>
        public string GenerateCSharpCode(string @namespace, params Type[] types)
        {
            foreach (var t in types)
            {
                var rt = RuntimeType.GetType(t);
                RecursiveAdd(rt);
            }
            var sb = new StringBuilder(256);

            GenerateCSharpCode(new StringWriter(sb), @namespace);
            return(sb.ToString());
        }
Пример #5
0
            internal SurrogateInfo(Type target, Type surrogate)
            {
                Target        = RuntimeType.GetType(target);
                SurrogateType = RuntimeType.GetType(surrogate);

                var tInterface = typeof(ISurrogate <>).MakeGenericType(target);

                Initialize  = tInterface.TryGetMethods(nameof(ISurrogate <int> .Convert), null, target).First();
                Instantiate = tInterface.GetRuntimeMethod(nameof(ISurrogate <int> .Revert), Array.Empty <Type>());
                Populate    = tInterface.TryGetMethods(nameof(ISurrogate <int> .Populate), null, target).First();
                CanPopulate = tInterface.GetProperty(nameof(ISurrogate <int> .CanPopulate));
            }
Пример #6
0
        void WriteCollection <T>(ICollection <T> value)
        {
            output.Write(value.IsReadOnly);
            if (value.IsReadOnly)
            {
                return;
            }
            var count = value.Count;

            output.WriteVInt(count);
            var surt = RuntimeType.GetType(typeof(T));

            foreach (var item in value)
            {
                count--;
                Write(surt, item);
            }
            if (count != 0)
            {
                throw new ArgumentException($"({value}.Count reported an incorrect value ({value.Count})");
            }
        }
Пример #7
0
        void WriteDictionary <K, V>(IDictionary <K, V> value)
        {
            output.Write(value.IsReadOnly);
            if (value.IsReadOnly)
            {
                return;
            }
            var count = value.Count;

            output.WriteVInt(count);
            var surk = RuntimeType.GetType(typeof(K));
            var surv = RuntimeType.GetType(typeof(V));

            foreach (var kv in value)
            {
                count--;
                Write(surk, kv.Key);
                Write(surv, kv.Value);
            }
            if (count != 0)
            {
                throw new ArgumentException($"({value}.Count reported an incorrect value ({value.Count})");
            }
        }
Пример #8
0
 public T Read<T>(T suggested = default(T))
 {
     readRaw = false;
     var args = new ReadArgs(RObject.TypeData(), RuntimeType.GetType(typeof(T)), suggested);
     return (T)Read(args);
 }
Пример #9
0
        internal void Write(RuntimeType expected, object value)
        {
            value = AsMetaData(value);

            // write id, continue if first time
            if (expected.IsReference)
            {
                if (TryGetId(value, out var id))
                {
                    output.WriteVInt(id);
                    return;
                }
                id = NewId();
                Register(id, value);
                output.WriteVInt(id);
            }

            // write class info if needed
            var actual = expected;

            if (expected.IsReference && !expected.IsSealed)
            {
                actual = RuntimeType.GetType(value);
                Write(RType, actual);
            }

            // only proceed further if value is supported
            if (!expected.IsSupported || !actual.IsSupported)
            {
                return;
            }

            // dispatch to the appropriate write method
            if (actual.Surrogate != null)
            {
                Write(RObject, actual.Surrogate.Convert(value));
            }
            else if (actual.Converter != null && !settings.IgnoreTypeConverter)
            {
                WriteConverter(actual, value);
            }
            else if (actual.IsISerializable && !Settings.IgnoreISerializable)
            {
                WriteISerializable(value);
            }
            else
            {
                switch (actual.Kind)
                {
                default:
                case PrimitiveType.None:
                    throw new InvalidOperationException("shouldn't be there");

                case PrimitiveType.Object:
                    if (actual.IsArray)
                    {
                        WriteArray(actual, value);
                    }
                    else if (actual.IsNullable)
                    {
                        if (value == null)
                        {
                            output.Write(false);
                        }
                        else
                        {
                            output.Write(true);
                            Write(actual.GenericParameters[0], value);
                        }
                    }
                    else if (actual.IsEnum)
                    {
                        Write(actual.Element, value);
                    }
                    else
                    {
                        WriteObject(actual, value);
                    }
                    break;

                case PrimitiveType.Type:
                    ((TypeData)value).Write(this, output);
                    break;

                case PrimitiveType.String:
                    output.Write((string)value);
                    break;

                case PrimitiveType.Bytes:
                    output.Write((byte[])value);
                    break;

                case PrimitiveType.Guid:
                    output.Write((Guid)value);
                    break;

                case PrimitiveType.Bool:
                    output.Write((bool)value);
                    break;

                case PrimitiveType.Char:
                    output.Write((char)value);
                    break;

                case PrimitiveType.Byte:
                    output.Write((byte)value);
                    break;

                case PrimitiveType.SByte:
                    output.Write((sbyte)value);
                    break;

                case PrimitiveType.Int16:
                    output.Write((short)value);
                    break;

                case PrimitiveType.UInt16:
                    output.Write((ushort)value);
                    break;

                case PrimitiveType.Int32:
                    output.Write((int)value);
                    break;

                case PrimitiveType.UInt32:
                    output.Write((uint)value);
                    break;

                case PrimitiveType.Int64:
                    output.Write((long)value);
                    break;

                case PrimitiveType.UInt64:
                    output.Write((ulong)value);
                    break;

                case PrimitiveType.Single:
                    output.Write((float)value);
                    break;

                case PrimitiveType.Double:
                    output.Write((double)value);
                    break;

                case PrimitiveType.Decimal:
                    output.Write((decimal)value);
                    break;
                }
            }
        }