Exemple #1
0
    public override void Write(Utf8JsonWriter writer, QueryPropertyInfo value, JsonSerializerOptions options)
    {
        if (value == null)
        {
            writer.WriteNullValue();
            return;
        }

        if (value is BooleanQueryPropertyInfo bp)
        {
            writer.WriteStartObject();
            BooleanQueryPropertyInfo.WriteProperties(writer, bp, options);
            writer.WriteEndObject();
        }
        else if (value is DateTimeQueryPropertyInfo dp)
        {
            writer.WriteStartObject();
            DateTimeQueryPropertyInfo.WriteProperties(writer, dp, options);
            writer.WriteEndObject();
        }
        else if (value is EnumQueryPropertyInfo ep)
        {
            writer.WriteStartObject();
            EnumQueryPropertyInfo.WriteProperties(writer, ep, options);
            writer.WriteEndObject();
        }
        else
        {
            writer.WriteStartObject();

            QueryPropertyInfo.WriteProperties(writer, value, options);

            writer.WriteEndObject();
        }
    }
Exemple #2
0
    protected internal static bool TryReadProperty(ref Utf8JsonReader reader, BooleanQueryPropertyInfo obj, JsonSerializerOptions options)
    {
        if (reader.ValueTextEquals(nameof(obj.TrueString)))
        {
            obj.TrueString = reader.ReadString();
            return(true);
        }
        if (reader.ValueTextEquals(nameof(obj.FalseString)))
        {
            obj.FalseString = reader.ReadString();
            return(true);
        }

        return(QueryPropertyInfo.TryReadProperty(ref reader, obj, options));
    }
Exemple #3
0
 protected internal static void WriteProperties(Utf8JsonWriter writer, BooleanQueryPropertyInfo value, JsonSerializerOptions options)
 {
     writer.WriteString(nameof(value.TrueString), value.TrueString);
     writer.WriteString(nameof(value.FalseString), value.FalseString);
     QueryPropertyInfo.WriteProperties(writer, value, options);
 }
Exemple #4
0
    public override QueryPropertyInfo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.Null)
        {
            return(null);
        }

        var jo = JsonDocument.ParseValue(ref reader).RootElement;

        QueryPropertyInfo      ret     = null;
        TryReadPropertyHandler tryRead = null;

        if (jo.TryGetProperty(nameof(QueryPropertyInfo.TypeName), out var tne))
        {
            switch (tne.GetString())
            {
            case nameof(DateTime):
            case nameof(DateTimeOffset):
                var dr = new DateTimeQueryPropertyInfo();
                ret     = dr;
                tryRead = (ref Utf8JsonReader r, JsonSerializerOptions op) => DateTimeQueryPropertyInfo.TryReadProperty(ref r, dr, options);
                break;

            case nameof(Boolean):
                var br = new BooleanQueryPropertyInfo();
                ret     = br;
                tryRead = (ref Utf8JsonReader r, JsonSerializerOptions op) => BooleanQueryPropertyInfo.TryReadProperty(ref r, br, options);
                break;

            case nameof(Enum):
                var er = new EnumQueryPropertyInfo();
                ret     = er;
                tryRead = (ref Utf8JsonReader r, JsonSerializerOptions op) => EnumQueryPropertyInfo.TryReadProperty(ref r, er, options);
                break;
            }
        }

        if (jo.TryGetProperty(nameof(EnumQueryPropertyInfo.Fields), out var fe))
        {
            var er = new EnumQueryPropertyInfo();
            ret     = er;
            tryRead = (ref Utf8JsonReader r, JsonSerializerOptions op) => EnumQueryPropertyInfo.TryReadProperty(ref r, er, options);
        }

        if (ret == null)
        {
            ret     = new QueryPropertyInfo();
            tryRead = (ref Utf8JsonReader r, JsonSerializerOptions op) => QueryPropertyInfo.TryReadProperty(ref r, ret, options);
        }

        var nr = new Utf8JsonReader(Encoding.UTF8.GetBytes(jo.GetRawText()).AsSpan());

        if (nr.Read())
        {
            while (nr.Read())
            {
                if (nr.TokenType == JsonTokenType.EndObject)
                {
                    return(ret);
                }
                else if (nr.TokenType == JsonTokenType.PropertyName)
                {
                    if (!tryRead(ref nr, options))
                    {
                        nr.TrySkip();
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Unexpected TokenType={nr.TokenType}");
                }
            }
        }

        throw new InvalidOperationException("Unexpected EOL");
    }
Exemple #5
0
    public override QueryPropertyInfo ReadJson(JsonReader reader, Type objectType, QueryPropertyInfo existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
        {
            return(null);
        }
        else if (reader.TokenType != JsonToken.StartObject)
        {
            throw new InvalidOperationException();
        }

        var r = new QueryPropertyInfo();

        BooleanQueryPropertyInfo getOrCreateBoolean()
        => (r as BooleanQueryPropertyInfo) ?? (BooleanQueryPropertyInfo)(r = new BooleanQueryPropertyInfo
        {
            Name = r.Name,
            DisplayName = r.DisplayName,
            TypeName = r.TypeName,
            DefaultOperator = r.DefaultOperator,
        });

        DateTimeQueryPropertyInfo getOrCreateDateTime()
        => (r as DateTimeQueryPropertyInfo) ?? (DateTimeQueryPropertyInfo)(r = new DateTimeQueryPropertyInfo
        {
            Name = r.Name,
            DisplayName = r.DisplayName,
            TypeName = r.TypeName,
            DefaultOperator = r.DefaultOperator,
        });

        EnumQueryPropertyInfo getOrCreateEnum()
        => (r as EnumQueryPropertyInfo) ?? (EnumQueryPropertyInfo)(r = new EnumQueryPropertyInfo
        {
            Name = r.Name,
            DisplayName = r.DisplayName,
            TypeName = r.TypeName,
            DefaultOperator = r.DefaultOperator,
        });

        while (reader.Read())
        {
            switch (reader.TokenType)
            {
            case JsonToken.EndObject:
                return(r);

            case JsonToken.PropertyName:
                var propertyName = reader.Value?.ToString();

                if (!reader.Read())
                {
                    throw new InvalidOperationException();
                }

                switch (propertyName)
                {
                case nameof(r.TypeName):
                    var tn = reader.Value?.ToString();

                    switch (tn)
                    {
                    case nameof(DateTime):
                    case nameof(DateTimeOffset):
                        getOrCreateDateTime();
                        break;

                    case nameof(Boolean):
                        getOrCreateBoolean();
                        break;

                    case nameof(Enum):
                        getOrCreateEnum();
                        break;
                    }

                    r.TypeName = tn;
                    break;

                case nameof(r.Name):
                    r.Name = reader.Value?.ToString();
                    break;

                case nameof(r.DisplayName):
                    r.DisplayName = reader.Value?.ToString();
                    break;

                case nameof(r.DefaultOperator):
                    r.DefaultOperator = reader.Value?.ToString();
                    break;

                case nameof(BooleanQueryPropertyInfo.TrueString):
                    getOrCreateBoolean().TrueString = reader.Value?.ToString();
                    break;

                case nameof(BooleanQueryPropertyInfo.FalseString):
                    getOrCreateBoolean().FalseString = reader.Value?.ToString();
                    break;

                case nameof(DateTimeQueryPropertyInfo.IsDate):
                    getOrCreateDateTime().IsDate = reader.Value is bool bds ||
                                                   reader.Value is string ds && bool.TryParse(ds, out bds)
                                ? bds : false;
                    break;

                case nameof(EnumQueryPropertyInfo.Fields):
                    var er = getOrCreateEnum();
                    if (reader.TokenType == JsonToken.Null)
                    {
                        er.Fields = null;
                    }
                    else if (reader.TokenType != JsonToken.StartArray)
                    {
                        throw new InvalidOperationException();
                    }
                    else
                    {
                        EnumFieldInfo f = null;
                        while (reader.Read() && reader.TokenType != JsonToken.EndArray)
                        {
                            switch (reader.TokenType)
                            {
                            case JsonToken.EndObject:
                                if (f != null)
                                {
                                    er.Fields.Add(f);
                                    f = null;
                                }
                                break;

                            case JsonToken.StartObject:
                                f = new EnumFieldInfo();
                                break;

                            case JsonToken.PropertyName:

                                switch (reader.Value?.ToString())
                                {
                                case nameof(f.Name):
                                    f.Name = reader.ReadAsString();
                                    break;

                                case nameof(f.DisplayName):
                                    f.DisplayName = reader.ReadAsString();
                                    break;

                                case nameof(f.Value):
                                    f.Value = (long)reader.ReadAsDouble();
                                    break;

                                default:
                                    if (!reader.Read())
                                    {
                                        throw new InvalidOperationException();
                                    }
                                    break;
                                }

                                break;
                            }
                        }
                    }

                    break;
                }

                break;
            }
        }

        throw new InvalidOperationException();
    }