public override CsfLabel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.PropertyName)
            {
                throw new JsonException();
            }

            var result = new CsfLabel
            {
                Label = reader.GetString() !
            };

            var value      = new CsfValue();
            var converter  = options.GetConverter <List <CsfValue> >();
            var converter2 = options.GetConverter <string>();

            while (reader.Read())
            {
OUTER:
                if (reader.TokenType is JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType is JsonTokenType.StartArray or JsonTokenType.String)
                {
                    value.Value = converter2 !.Read(ref reader, options) !;
                    break;
                }
        public static async Task Serialize(this CsfValue @this, Stream stream, byte[] buffer)
        {
            buffer.SizeCheck(8);
            var flag = string.IsNullOrEmpty(@this.Extra);
            var str  = Encoding.Unicode.GetBytes(@this.Value.Replace("\r\n", "\n"));

            (flag ? STR_RAW : WSTR_RAW).CopyToLittleEndianByteArray(buffer);
            @this.Value.Length.CopyToLittleEndianByteArray(buffer, 4);

            await stream.WriteAsync(buffer.AsMemory(0, 8)).ConfigureAwait(false);

            await stream.WriteAsync(Decoding(str).AsMemory()).ConfigureAwait(false);

            if (!flag)
            {
                @this.Extra.Length.CopyToLittleEndianByteArray(buffer);
                await stream.WriteAsync(buffer.AsMemory(0, 4)).ConfigureAwait(false);

                await stream.WriteAsync(Encoding.ASCII.GetBytes(@this.Extra).AsMemory()).ConfigureAwait(false);
            }
        }
示例#3
0
        public override CsfLabel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            var result     = new CsfLabel();
            var value      = new CsfValue();
            var converter  = options.GetConverter <List <CsfValue> >();
            var converter2 = options.GetConverter <string>();

            while (reader.Read())
            {
                if (reader.TokenType is JsonTokenType.EndObject)
                {
                    break;
                }
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }
                switch (reader.GetString()?.ToLower())
                {
                case "label":
                    reader.Read();
                    result.Label = reader.GetString() !;
                    break;

                case "values":
                    reader.Read();
                    result.Values = converter !.Read(ref reader, options) !;
                    break;

                case "value":
                    if (result.Values.Count > 0)
                    {
                        throw new JsonException();
                    }
                    reader.Read();
                    value.Value = converter2 !.Read(ref reader, options) !;
                    break;

                case "extra":
                    if (result.Values.Count > 0)
                    {
                        throw new JsonException();
                    }
                    reader.Read();
                    value.Extra = reader.TokenType is JsonTokenType.String ? reader.GetString() ! : throw new JsonException();
                    break;

                default:
                    throw new JsonException();
                }
            }
            if (result.Values.Count < 1)
            {
                result.Values.Add(value);
            }
            return(result);
        }