Ejemplo n.º 1
0
        public override IValueValidator Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var rootElement = JsonDocument.ParseValue(ref reader).RootElement;

            var nameJsonProperty = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(IValueValidator.Name), StringComparison.OrdinalIgnoreCase));

            if (nameJsonProperty.Value.ValueKind == JsonValueKind.String)
            {
                var valueValidator = CreateValueValidatorByName(nameJsonProperty.Value.GetString());

                var propertiesJsonProperty = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(IValueValidator.Properties), StringComparison.OrdinalIgnoreCase));
                if (propertiesJsonProperty.Value.ValueKind == JsonValueKind.Object)
                {
                    var newOptions = JsonSerializerOptionsHelper.Create(options, this, new ObjectToInferredTypesConverter());
                    var properties = JsonSerializer.Deserialize <IDictionary <string, object> >(propertiesJsonProperty.Value.GetRawText(), newOptions);
                    if (properties != null && properties.Any())
                    {
                        foreach (var property in properties)
                        {
                            valueValidator.Properties[property.Key] = property.Value;
                        }
                    }
                }

                return(valueValidator);
            }

            throw new JsonException($"Can't to get the {nameof(IValueValidator.Name)} property of {nameof(IValueValidator)}!");
        }
Ejemplo n.º 2
0
        public override ISelectionStringValueItemSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var newOptions = JsonSerializerOptionsHelper.Create(options, this);

            var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
            var items       = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(ISelectionStringValueItemSource.Items), StringComparison.OrdinalIgnoreCase)).Value.GetRawText();

            var selectionStringValueItem =
                JsonSerializer.Deserialize <List <LocalizableSelectionStringValueItem> >(items, newOptions) ??
                new List <LocalizableSelectionStringValueItem>();

            return(new StaticSelectionStringValueItemSource(selectionStringValueItem.ToArray().As <ISelectionStringValueItem[]>()));
        }
        public override IStringValueType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
            var name        = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(IStringValueType.Name), StringComparison.OrdinalIgnoreCase)).Value.GetString();
            var newOptions  = JsonSerializerOptionsHelper.Create(options, this, new ValueValidatorJsonConverter(), new SelectionStringValueItemSourceJsonConverter());

            return(name switch
            {
                "SelectionStringValueType" => JsonSerializer.Deserialize <SelectionStringValueType>(rootElement.GetRawText(), newOptions),
                "FreeTextStringValueType" => JsonSerializer.Deserialize <FreeTextStringValueType>(rootElement.GetRawText(), newOptions),
                "ToggleStringValueType" => JsonSerializer.Deserialize <ToggleStringValueType>(rootElement.GetRawText(), newOptions),
                _ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!")
            });
        public override ISelectionStringValueItemSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var rootElement       = JsonDocument.ParseValue(ref reader).RootElement;
            var itemsJsonProperty = rootElement.EnumerateObject().FirstOrDefault(x =>
                                                                                 x.Name.Equals(nameof(ISelectionStringValueItemSource.Items), StringComparison.OrdinalIgnoreCase));

            if (itemsJsonProperty.Value.ValueKind == JsonValueKind.Array)
            {
                var newOptions = JsonSerializerOptionsHelper.Create(options, this);

                var selectionStringValueItem =
                    JsonSerializer.Deserialize <LocalizableSelectionStringValueItem[]>(itemsJsonProperty.Value.GetRawText(), newOptions) ??
                    Array.Empty <LocalizableSelectionStringValueItem>();

                return(new StaticSelectionStringValueItemSource(selectionStringValueItem.As <ISelectionStringValueItem[]>()));
            }

            throw new JsonException($"Can't to get the {nameof(ISelectionStringValueItemSource.Items)} property of {nameof(ISelectionStringValueItemSource)}!");
        }
Ejemplo n.º 5
0
        public override IValueValidator Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var rootElement    = JsonDocument.ParseValue(ref reader).RootElement;
            var name           = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("Name", StringComparison.InvariantCultureIgnoreCase)).Value.GetString();
            var valueValidator = CreateValueValidatorByName(name);

            var propertiesRawText = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("Properties", StringComparison.InvariantCultureIgnoreCase)).Value.GetRawText();
            var newOptions        = JsonSerializerOptionsHelper.Create(options, this, new ObjectToInferredTypesConverter());
            var properties        = JsonSerializer.Deserialize <IDictionary <string, object> >(propertiesRawText, newOptions);

            if (properties != null && properties.Any())
            {
                foreach (var property in properties)
                {
                    valueValidator.Properties[property.Key] = property.Value;
                }
            }

            return(valueValidator);
        }
        public override void Write(Utf8JsonWriter writer, ISelectionStringValueItemSource value, JsonSerializerOptions options)
        {
            var newOptions = JsonSerializerOptionsHelper.Create(options, this);

            JsonSerializer.Serialize(writer, value, value.GetType(), newOptions);
        }
Ejemplo n.º 7
0
        public override void Write(Utf8JsonWriter writer, IValueValidator value, JsonSerializerOptions options)
        {
            var newOptions = JsonSerializerOptionsHelper.Create(options, this);

            JsonSerializer.Serialize(writer, value, value.GetType(), newOptions);
        }