internal static Utf8CsvWriter SerializeToBuffer(object graph)
        {
            Type            type = graph.GetType();
            CsvBytesContext info = GetLastContext(type);
            var             size = new CsvBytesSize();

            info.EstimateSize(size, graph);
            int           totalSize = info.MinSize + size.Size;
            Utf8CsvWriter result    = null;

            byte[] buffer = null;
            if (totalSize > 400)
            {
                buffer = XPoolSave <byte> .Rent(totalSize);

                result = new Utf8CsvWriter(buffer);
                Serialize(result, graph, info);
                XPoolSave <byte> .Return(result.GetBuffer());
            }
            else
            {
                buffer = new byte[totalSize];
                result = new Utf8CsvWriter(buffer);
                Serialize(result, graph, info);
            }
            return(result);
        }
        internal static CsvStringWriter SerializeToBuffer(object graph)
        {
            Type             type = graph.GetType();
            CsvStringContext info = GetLastContext(type);
            var size = new CsvStringSize();

            info.EstimateSize(size, graph);
            int             totalSize = info.MinSize + size.Size;
            CsvStringWriter result    = null;

            char[] buffer = null;
            if (totalSize > 400)
            {
                buffer = XPoolSave <char> .Rent(totalSize);

                result = new CsvStringWriter(buffer);
                Serialize(result, graph, info);
                XPoolSave <char> .Return(result.GetBuffer());
            }
            else
            {
                buffer = new char[totalSize];
                result = new CsvStringWriter(buffer);
                Serialize(result, graph, info);
            }
            return(result);
        }
        internal static JsonString SerializeToBuffer(object graph)
        {
            Type type = graph.GetType();
            JsonStringContext info = GetInfo(type);
            JsonStringSize    size = new JsonStringSize();

            size.SetInfo(info);
            info.EstimateSize(size, graph);
            int        totalSize = info.MinSize + size.Size;
            JsonString result    = null;

            char[] buffer = null;
            if (totalSize > 400)
            {
                buffer = XPoolSave <char> .Rent(totalSize);

                result = new JsonString(buffer);
                Serialize(result, graph, info);
                XPoolSave <char> .Return(result.GetBuffer());
            }
            else
            {
                buffer = new char[totalSize];
                result = new JsonString(buffer);
                Serialize(result, graph, info);
            }
            return(result);
        }
Exemple #4
0
 internal static void Serialize(ObjectWriter stream, object graph, ObjectBufferContext info)
 {
     stream.WriteType(graph.GetType());
     stream.SetInfo(info);
     info.Serializer(stream, graph);
     XPoolSave <byte> .Return(stream._buffer);
 }
Exemple #5
0
        public void MyPool()
        {
            for (int i = 0; i < N; i++)
            {
                var buf = XPoolSave <char> .Rent(Size);

                XPoolSave <char> .Return(buf);
            }
        }
        internal unsafe static Span <byte> Serialize(object graph)
        {
            if (graph.GetType().IsArray)
            {
                GCHandle handle = default;
                Array    ar     = (Array)graph;
                if (ar.Length == 0)
                {
                    return(null);
                }
                bool            isFirst = true;
                CsvBytesContext info    = null;
                Utf8CsvWriter   writer  = null;
                foreach (object item in ar)
                {
                    if (isFirst)
                    {
                        info = GetLastContext(item.GetType());
                        //if(info.IsAllBaseType)
                        int size = info.MinSize * ar.Length + info.GetHeaderSize();
                        //char[] buffer = CharsBufferManager.GetBuffer(size);
                        byte[] buffer = ArrayPool <byte> .Shared.Rent(size);

                        handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                        writer = new Utf8CsvWriter(buffer);
                        //fixed (byte* pointer = buffer)
                        //{
                        //    stream.bp = pointer;
                        //}
                        //stream.WriteHeader(info.Names);
                        writer.WriteHeader(info.NamesCommaBytes);
                        isFirst = false;
                    }
                    info.Serializer(writer, item);
                    writer.WriteNewLine();
                }
                //CharsBufferManager.SetBuffer(stream.GetBuffer());
                ArrayPool <byte> .Shared.Return(writer.GetBuffer());

                //var ret = stream.ToString();
                handle.Free();
                return(new Span <byte>(writer.GetBuffer(), 0, writer.Position));
            }
            else
            {
                Type[] gtypes = graph.GetType().GenericTypeArguments;
                if (gtypes.Length == 1)
                {
                    Type            type   = gtypes[0];
                    CsvBytesContext info   = GetLastContext(type);
                    Utf8CsvWriter   stream = null;
                    IList           list   = graph as IList;
                    if (list != null)
                    {
                        int    size   = info.MinSize * list.Count + info.GetHeaderSize();
                        byte[] buffer = XPoolSave <byte> .Rent(size);

                        stream = new Utf8CsvWriter(buffer);
                        stream.WriteHeader(info.NamesCommaBytes);
                        for (int i = 0; i < list.Count; i++)
                        {
                            info.Serializer(stream, list[i]);
                            stream.WriteNewLine();
                        }
                        XPoolSave <byte> .Return(stream.GetBuffer());
                    }
                    return(new Span <byte>(stream.GetBuffer(), 0, stream.Position));
                }
                //IEnumerable enumerable =  graph as IEnumerable;
                //foreach (object item in enumerable)
                //{
                //    //stream.Write(item);
                //    //Serialize(stream, graph, info);
                //}
            }

            return(null);
        }