Пример #1
0
        public static string ToJsonString <T>(this IJsonFormatter <T> formatter, T value, IJsonFormatterResolver formatterResolver)
        {
            var writer = new JsonWriterUtf8();

            formatter.Serialize(writer, value, formatterResolver);
            return(writer.ToString());
        }
Пример #2
0
        public static byte[] GetEncodedPropertyName(string propertyName)
        {
            var writer = new JsonWriterUtf8();

            writer.WritePropertyName(propertyName);
            return(writer.ToUtf8ByteArray());
        }
Пример #3
0
        public static byte[] PrettyPrintByteArray(string json)
        {
            var reader = new JsonReaderUtf8(Encoding.UTF8.GetBytes(json));
            var writer = new JsonWriterUtf8(MemoryPool.GetBuffer());

            WritePrittyPrint(reader, writer, 0);
            return(writer.ToUtf8ByteArray());
        }
Пример #4
0
        public static byte[] PrettyPrintByteArray(byte[] json, int offset)
        {
            var reader = new JsonReaderUtf8(json, offset);
            var writer = new JsonWriterUtf8(MemoryPool.GetBuffer());

            WritePrittyPrint(reader, writer, 0);
            return(writer.ToUtf8ByteArray());
        }
Пример #5
0
        public static byte[] GetEncodedPropertyNameWithPrefixValueSeparator(string propertyName)
        {
            var writer = new JsonWriterUtf8();

            writer.WriteValueSeparator();
            writer.WritePropertyName(propertyName);
            return(writer.ToUtf8ByteArray());
        }
Пример #6
0
        public static byte[] GetEncodedPropertyNameWithoutQuotation(string propertyName)
        {
            var writer = new JsonWriterUtf8();

            writer.WriteString(propertyName); // "propname"
            var buf    = writer.GetBuffer();
            var result = new byte[buf.Count - 2];

            Buffer.BlockCopy(buf.Array, buf.Offset + 1, result, 0, result.Length); // without quotation
            return(result);
        }
Пример #7
0
        public static byte[] Serialize <T>(T value, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = JsonSerializer.DefaultResolver;
            }

            var writer    = new JsonWriterUtf8(MemoryPool.GetBuffer());
            var formatter = resolver.GetFormatterWithVerify <T>();

            formatter.Serialize(writer, value, resolver);
            return(writer.ToUtf8ByteArray());
        }
Пример #8
0
        public static async System.Threading.Tasks.Task SerializeAsync <T>(Stream stream, T value, IJsonFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = JsonSerializer.DefaultResolver;
            }

            var buf = BufferPool.Default.Rent();

            try
            {
                var writer    = new JsonWriterUtf8(buf);
                var formatter = resolver.GetFormatterWithVerify <T>();
                formatter.Serialize(writer, value, resolver);
                var buffer = writer.GetBuffer();
                await stream.WriteAsync(buffer.Array, buffer.Offset, buffer.Count).ConfigureAwait(false);
            }
            finally
            {
                BufferPool.Default.Return(buf);
            }
        }