/// <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); }
/// <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)); }
//#region CompileTimeSerialization ///// <summary> ///// The "compile time" serialization is a faster way to serialize data ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="data"></param> ///// <returns></returns> //public string SerializeCompile<T>(T data) //{ // Writer = new StringBuilder(256); // //ClearSerializedObjects(); // ReferencedIndex = 0; // if (GetSerializer(out Action<Serializer, StringBuilder, T> serializer, ref data)) // serializer(this, Writer, data); // return Writer.ToString(); //} ///// <summary> ///// Continue serialization in compile time ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="data"></param> //public void ContinueSerializeCompile<T>(T data) //{ // if (GetSerializer(out Action<Serializer, StringBuilder, T> serializer, ref data)) // serializer(this, Writer, data); //} ///// <summary> ///// Gets "compile time" serializer to serialize data faster ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="serializer"></param> ///// <param name="data"></param> ///// <returns></returns> //internal bool GetSerializer<T>(out Action<Serializer, StringBuilder, T> serializer, ref T data) //{ // serializer = TypeInfo<T>.Serialize; // if (serializer == null) // { // var genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); // if (genericTypeDefinition == typeof(IEnumerable<>)) // { // var type = typeof(IEnumerable<>).MakeGenericType(typeof(T).GetGenericArguments()[0]); // if (TypeManager.CompiledTypes.TryGetValue(type, out CompileTime.TypeInfo typeInfo)) // { // typeInfo.DynamicSerialize(this, Writer, data); // return false; // } // } // throw new Exception($"Type {typeof(T)} not initialized in compile time!"); // } // return true; //} //#endregion /// <summary> /// Serializes an object to a json string /// </summary> /// <param name="typeGoInfo"></param> /// <param name="serializeHandler"></param> /// <param name="data">Object to serialize</param> /// <returns>Json string from serialized object</returns> internal void SerializeObject <T>(TypeGoInfo <T> typeGoInfo, ref JsonSerializeHandler serializeHandler, ref T data) { typeGoInfo.JsonSerialize(ref serializeHandler, ref data); }