Ejemplo n.º 1
0
        /// <summary>
        /// Initalizes TypeGo variable
        /// </summary>
        /// <param name="arrayTypeGoInfo">TypeGo variable to initialize</param>
        /// <param name="options">Serializer or deserializer options</param>
        public void Initialize(TypeGoInfo <List <T> > arrayTypeGoInfo, ITypeOptions options)
        {
            arrayTypeGoInfo.IsNoQuotesValueType = false;
            //set the default value of variable
            arrayTypeGoInfo.DefaultValue = default;

            if (TryGetValueOfTypeGo(typeof(T), out object result))
            {
                typeGoInfo = (TypeGoInfo <T>)result;
            }
            else
            {
                typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
            }

            arrayTypeGoInfo.JsonSerialize = JsonSerialize;

            //set delegates to access faster and make it pointer directly usage for json deserializer
            arrayTypeGoInfo.JsonDeserialize = JsonDeserialize;

            //set delegates to access faster and make it pointer directly usage for binary serializer
            arrayTypeGoInfo.BinarySerialize = BinarySerialize;

            //set delegates to access faster and make it pointer directly usage for binary deserializer
            arrayTypeGoInfo.BinaryDeserialize = BinaryDeserialize;

            //create instance of object
            //arrayTypeGoInfo.CreateInstance = ReflectionHelper.GetActivator<TObject>(baseType);

            CastToArray = arrayTypeGoInfo.Cast;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Serializes an object into a json string
        /// </summary>
        /// <param name="data">Any object to serialize into json</param>
        /// <returns>The json string returned after serialization</returns>
        public Span <byte> Serialize <T>(T data)
        {
            TypeGoInfo <T> typeGoInfo;
            Type           dataType = typeof(T);

            if (!TryGetValueOfTypeGo(dataType, out object typeGo))
            {
                typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
            }
            else
            {
                typeGoInfo = (TypeGoInfo <T>)typeGo;
            }

            // The serialize handler lets the serializer access faster to the pointers
            //JsonSerializeHandler serializeHandler = new JsonSerializeHandler
            //{
            //    BinaryWriter = new BufferBuilder(typeGoInfo.Capacity),
            //    //AddSerializedObjects = serializedObjects.Add,
            //    //TryGetValueOfSerializedObjects = serializedObjects.TryGetValue
            //};
            BufferBuilder binaryWriter = new BufferBuilder(typeGoInfo.Capacity);

            //ReferencedIndex = 0;
            typeGoInfo.BinarySerialize(ref binaryWriter, ref data);
            typeGoInfo.Capacity = Math.Max(typeGoInfo.Capacity, binaryWriter.Length);
            return(binaryWriter.ToSpan().Slice(0, binaryWriter.Length));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// serialize to json as byte array
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public ReadOnlySpan <byte> SerializeToBytes <T>(T data)
        {
            TypeGoInfo <T> typeGoInfo;
            Type           dataType = typeof(T);

            if (!TryGetValueOfTypeGo(dataType, out object typeGo))
            {
                typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
            }
            else
            {
                typeGoInfo = (TypeGoInfo <T>)typeGo;
            }

            // The serialize handler lets the serializer access faster to the pointers
            JsonSerializeHandler serializeHandler = new JsonSerializeHandler
            {
                TextWriter = new BufferCharBuilder(typeGoInfo.Capacity),
                //AddSerializedObjects = serializedObjects.Add,
                //TryGetValueOfSerializedObjects = serializedObjects.TryGetValue
            };

            ReferencedIndex = 0;
            SerializeObject(typeGoInfo, ref serializeHandler, ref data);
            typeGoInfo.Capacity = Math.Max(typeGoInfo.Capacity, serializeHandler.TextWriter.Length);
            Span <byte> bytes = new Span <byte>(new byte[serializeHandler.TextWriter.Length]);

            Encoding.GetBytes(serializeHandler.TextWriter.ToSpan().Slice(0, serializeHandler.TextWriter.Length), bytes);
            return(bytes);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Serializes an object to a json string
        /// </summary>
        /// <param name="data">Object to serialize</param>
        /// <returns>Json string returned from your serialized object</returns>
        public string Serialize <T>(T data)
        {
            TypeGoInfo <T> typeGoInfo;
            Type           dataType = typeof(T);

            if (!TryGetValueOfTypeGo(dataType, out object typeGo))
            {
                typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
            }
            else
            {
                typeGoInfo = (TypeGoInfo <T>)typeGo;
            }

            // The serialize handler lets the serializer access faster to the pointers
            JsonSerializeHandler serializeHandler = new JsonSerializeHandler
            {
                TextWriter = new BufferCharBuilder(typeGoInfo.Capacity),
                //AddSerializedObjects = serializedObjects.Add,
                //TryGetValueOfSerializedObjects = serializedObjects.TryGetValue
            };

            ReferencedIndex = 0;
            SerializeObject(typeGoInfo, ref serializeHandler, ref data);
            typeGoInfo.Capacity = Math.Max(typeGoInfo.Capacity, serializeHandler.TextWriter.Length);
            return(new string(serializeHandler.TextWriter.ToArray(), 0, serializeHandler.TextWriter.Length));
        }
Ejemplo n.º 5
0
        public (string Result, TestEnum Value) EnumTestSerialize2()
        {
            BaseTypeGoInfo.Generate <TestEnum>(BinaryGo.Json.Serializer.DefaultOptions);
            var value  = TestEnum.Value10;
            var result = BinaryGo.Json.Serializer.NormalInstance.Serialize(value);

            Assert.True(result == $"{(int)value}", $"Your Value: {value} Serialize Value: {result}");
            return(result, value);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Deserializes a stream to a type
 /// </summary>
 /// <typeparam name="T">Type to deserialize into</typeparam>
 /// <param name="reader">SpanReader binary to deserialize</param>
 /// <returns>deserialized type</returns>
 public T Deserialize <T>(ReadOnlySpan <byte> reader)
 {
     try
     {
         Type dataType = typeof(T);
         if (!TryGetValueOfTypeGo(dataType, out object typeGoInfo))
         {
             typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
         }
         BinarySpanReader binaryReader = new BinarySpanReader(reader);
         return(((TypeGoInfo <T>)typeGoInfo).BinaryDeserialize(ref binaryReader));
     }
     finally
     {
         DeSerializedObjects.Clear();
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Deserializes a json to a type
        /// </summary>
        /// <typeparam name="T">Type to deserialize into</typeparam>
        /// <param name="json">Json string to deserialize</param>
        /// <returns>deserialized type</returns>
        public T Deserialize <T>(string json)
        {
            try
            {
                var dataType = typeof(T);
                if (!TryGetValueOfTypeGo(dataType, out object typeGoInfo))
                {
                    typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
                }
                var reader = new JsonSpanReader(json.AsSpan());
                JsonDeserializer deserializer = this;
                var result = FastDeserializerExtractor <T> .Extract(ref deserializer, (TypeGoInfo <T>) typeGoInfo, ref reader);

                return(result);
            }
            finally
            {
                //DeSerializedObjects.Clear();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initialize variable to typeGo
        /// </summary>
        /// <param name="typeGoInfo">TypeGo</param>
        /// <param name="options">Serializer's options or settings</param>
        public void InitializeBase(BaseTypeGoInfo typeGoInfo, ITypeOptions options)
        {
            typeGoInfo.Variable = this;
            CurrentCulture      = options.CurrentCulture;
            Options             = options;

            AddTypes                  = options.AddTypes;
            TryGetValueOfTypeGo       = options.TryGetValueOfTypeGo;
            HasGenerateRefrencedTypes = options.HasGenerateRefrencedTypes;

            var thisType   = this.GetType();
            var findMethod = thisType.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                             .Where(x => x.Name == "Initialize").FirstOrDefault();

            if (findMethod == null)
            {
                throw new Exception($"I tried to find method Initialize in type of {thisType.FullName} but not found this method pls add it");
            }
            findMethod.Invoke(this, new object[] { typeGoInfo, options });
        }
Ejemplo n.º 9
0
 static BinarySerializer()
 {
     BaseTypeGoInfo.GenerateDefaultVariables(DefaultOptions);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// generate type to typeGo
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public void GenerateType <T>()
 {
     BaseTypeGoInfo.Generate <T>(this);
 }