Esempio n. 1
0
        internal static BinaryModelInfo GetBinaryModel(Type type, BaseOptionInfo option, Dictionary <Type, BinaryModelInfo> generatedModels)
        {
            var getBinaryModel = typeof(BinaryModelInfo).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public)
                                 .FirstOrDefault(x => x.Name == nameof(BinaryModelInfo.GetBinaryModel)).MakeGenericMethod(type);

            return((BinaryModelInfo)getBinaryModel.Invoke(null, new object[] { GetTypeGo(type, option), option, generatedModels }));
        }
        internal static BaseTypeGoInfo GetTypeGo(Type type, BaseOptionInfo option)
        {
            System.Reflection.MethodInfo getBinaryModel = typeof(BaseTypeGoInfo).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public)
                                                          .FirstOrDefault(x => x.Name == nameof(BaseTypeGoInfo.Generate)).MakeGenericMethod(type);
            object typeGo = getBinaryModel.Invoke(null, new object[] { option });

            return((BaseTypeGoInfo)typeGo);
        }
        /// <summary>
        /// Get binary member
        /// </summary>
        /// <returns></returns>
        internal override MemberBinaryModelInfo GetBinaryMember(BaseOptionInfo option, Dictionary<Type, BinaryModelInfo> generatedModels)
        {
            MemberBinaryModelInfo memberBinaryModelInfo = new MemberBinaryModelInfo()
            {
                Name = Name,
                ResultType = BinaryModelInfo.GetBinaryModel(TypeGoInfo, option, generatedModels),
                CanRead = true,
                CanWrite = true,
                Type = MemberBinaryModelType.Property
            };

            return memberBinaryModelInfo;
        }
Esempio n. 4
0
        /// <summary>
        /// Initialize seralizer with options
        /// </summary>
        public BinarySerializer(BaseOptionInfo jsonOptionInfo)
        {
            Options = jsonOptionInfo;

            AddTypes            = Options.Types.Add;
            TryGetValueOfTypeGo = Options.Types.TryGetValue;
            //SerializeHandler.Serializer = this;

            HasGenerateRefrencedTypes         = jsonOptionInfo.HasGenerateRefrencedTypes;
            Setting.HasGenerateRefrencedTypes = jsonOptionInfo.HasGenerateRefrencedTypes;

            //SerializeFunction = async (TypeGoInfo typeGoInfo, Stream stream, object dataRef) =>
            //{
            //   await  SerializeObject(dataRef, typeGoInfo);
            //};
        }
Esempio n. 5
0
        /// <summary>
        /// Serializes object to json
        /// </summary>
        /// <param name="jsonOptionInfo">your json serializer option</param>
        public Serializer(BaseOptionInfo jsonOptionInfo)
        {
            Options = jsonOptionInfo;

            AddTypes            = Options.Types.Add;
            TryGetValueOfTypeGo = Options.Types.TryGetValue;

            HasGenerateRefrencedTypes         = jsonOptionInfo.HasGenerateRefrencedTypes;
            Setting.HasGenerateRefrencedTypes = jsonOptionInfo.HasGenerateRefrencedTypes;
            Encoding = Options.Encoding;

            //SerializeFunction = (TypeGoInfo typeGoInfo, JsonSerializeHandler handler, ref object dataRef) =>
            //{
            //    SerializeObject(ref dataRef, typeGoInfo);
            //};
        }
Esempio n. 6
0
        /// <summary>
        /// Get structure of models of typeGo
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public static List <BinaryModelInfo> GetStructureModels(BaseOptionInfo option = default)
        {
            if (option == null)
            {
                option = DefaultOptions;
            }
            List <BinaryModelInfo>             result          = new List <BinaryModelInfo>();
            Dictionary <Type, BinaryModelInfo> generatedModels = new Dictionary <Type, BinaryModelInfo>();

            foreach (KeyValuePair <Type, object> type in option.Types)
            {
                Type typeGoType = ((BaseTypeGoInfo)type.Value).Type;
                if (typeGoType.Assembly.FullName.StartsWith("System.Private.CoreLib"))
                {
                    continue;
                }
                result.Add(BinaryModelInfo.GetBinaryModel(typeGoType, option, generatedModels));
            }
            return(result);
        }
Esempio n. 7
0
        public void ComplexUserTestDeserializeBase(byte[] Result, ComplexUser Value, BaseOptionInfo SerializerOptions)
        {
            //in this example server side has SimpleUserInfo
            //server side has Id, Name, Family
            //and the client side has SimpleUserOldStructureInfo
            //client side has Id, Age, BirthDate ,Name

            //new structure of models
            var newStructureModels = BinarySerializer.GetStructureModels(SerializerOptions);

            //my old deserializer
            var myDeserializer = new BinaryDeserializer();

            myDeserializer.Options = new BinaryGo.Helpers.BaseOptionInfo();

            #region VersionChangedControl
            //generate type
            myDeserializer.Options.GenerateType <ComplexUserOldStructureInfo>();
            //add model renamed
            myDeserializer.AddMovedType(myDeserializer.GetStrcutureModelName(typeof(ComplexUser)), typeof(ComplexUserOldStructureInfo));
            myDeserializer.AddMovedType(myDeserializer.GetStrcutureModelName(typeof(ComplexCompanyInfo)), typeof(ComplexCompanyOldStructureInfo));
            myDeserializer.AddMovedType(myDeserializer.GetStrcutureModelName(typeof(ComplexCompanyInfo[])), typeof(ComplexCompanyOldStructureInfo[]));
            myDeserializer.AddMovedType(myDeserializer.GetStrcutureModelName(typeof(ComplexCarInfo)), typeof(ComplexCarOldStructureInfo));
            myDeserializer.AddMovedType(myDeserializer.GetStrcutureModelName(typeof(ComplexCarInfo[])), typeof(ComplexCarOldStructureInfo[]));
            myDeserializer.AddMovedType(myDeserializer.GetStrcutureModelName(typeof(CompanyType)), typeof(int));
            //build new structure to old structure
            myDeserializer.BuildStructure(newStructureModels);
            #endregion

            var result = myDeserializer.Deserialize <ComplexUserOldStructureInfo>(Result);
            Assert.True(result.IsEquals(Value));

            //now serialize from client side and deserialize from server side happen
            result.Phone = "my phoe number :)";
            BinarySerializer binarySerializer = new BinarySerializer(myDeserializer.Options);
            var resultSerialized   = binarySerializer.Serialize(result);
            var resultDeserialized = myDeserializer.Deserialize <ComplexUserOldStructureInfo>(resultSerialized);
            Assert.True(resultDeserialized.IsEquals(Value));
        }
Esempio n. 8
0
 /// <summary>
 /// Get Binary member by property
 /// </summary>
 /// <returns></returns>
 internal abstract MemberBinaryModelInfo GetBinaryMember(BaseOptionInfo option, Dictionary <Type, BinaryModelInfo> generatedModels);
        /// <summary>
        /// Get binary model of type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="typeGoInfo"></param>
        /// <param name="option"></param>
        /// <param name="generatedModels"></param>
        /// <returns></returns>
        internal static BinaryModelInfo GetBinaryModel <T>(TypeGoInfo <T> typeGoInfo, BaseOptionInfo option, Dictionary <Type, BinaryModelInfo> generatedModels)
        {
            Type type = typeGoInfo.Type;

            if (generatedModels.TryGetValue(type, out BinaryModelInfo binaryModel))
            {
                return(binaryModel);
            }
            binaryModel = new BinaryModelInfo
            {
                Name         = type.Name,
                Namespace    = type.Namespace,
                AssemblyName = System.IO.Path.GetFileName(type.Assembly.Location),
                Properties   = new List <MemberBinaryModelInfo>(),
                Generics     = new List <BinaryModelInfo>()
            };

            generatedModels.Add(type, binaryModel);
            foreach (Type genericType in type.GetGenericArguments())
            {
                binaryModel.Generics.Add(GetBinaryModel(genericType, option, generatedModels));
            }

            if (typeGoInfo.Variable is ObjectVariable <T> objectVariable)
            {
                foreach (BasePropertyGoInfo <T> property in objectVariable.Properties)
                {
                    MemberBinaryModelInfo propertyResult = property.GetBinaryMember(option, generatedModels);
                    propertyResult.Index = property.Index;
                    binaryModel.Properties.Add(propertyResult);
                }
            }


            return(binaryModel);
        }
Esempio n. 10
0
 /// <summary>
 /// Initialize seralizer
 /// </summary>
 public BinarySerializer()
 {
     Options = DefaultOptions;
 }