示例#1
0
        public static T GetJson <T>(this IMemory memory, JsonSerializerOptions jsonSerializerOptions = null)
        {
            if (jsonSerializerOptions == null)
            {
                jsonSerializerOptions = GetDefaultJsonSerializerOptions();
            }
            var reader = new Utf8JsonReader(memory.AsSpan());

            return(JsonSerializer.Deserialize <T>(ref reader, jsonSerializerOptions));
        }
示例#2
0
        public static string GetString(this IMemory memory, Encoding encoding)
        {
#if !NETFRAMEWORK
            return(encoding.GetString(memory.AsSpan()));
#else
            unsafe
            {
                return(encoding.GetString((byte *)memory.Ptr, memory.Length));
            }
#endif
        }
示例#3
0
        public static void SetString(this IMemory memory, string value, Encoding encoding)
        {
            var length = encoding.GetByteCount(value);

            memory.Allocate(length);
#if !NETFRAMEWORK
            encoding.GetBytes(value, memory.AsSpan());
#else
            unsafe
            {
                fixed(char *chars = value)
                {
                    encoding.GetBytes(chars, value.Length, (byte *)memory.Ptr, length);
                }
            }
#endif
        }