/// <summary> /// json serialize /// </summary> /// <param name="handler"></param> /// <param name="value"></param> public void JsonSerialize(ref JsonSerializeHandler handler, ref List <T> value) { if (value != null) { handler.TextWriter.Write(JsonConstantsString.OpenSquareBrackets); int count = value.Count; for (int i = 0; i < count; i++) { var obj = value[i]; typeGoInfo.JsonSerialize(ref handler, ref obj); handler.TextWriter.Write(JsonConstantsString.Comma); } handler.TextWriter.RemoveLast(JsonConstantsString.Comma); handler.TextWriter.Write(JsonConstantsString.CloseSquareBrackets); } else { handler.TextWriter.Write(JsonConstantsString.Null); } }
//#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); }