Exemple #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)
            {
                _readJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this, new ObjectToInferredTypesConverter());
                var properties = propertiesJsonProperty.Value.Deserialize <IDictionary <string, object> >(_readJsonSerializerOptions);
                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)}!");
    }
Exemple #2
0
    public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
    {
        var newOptions      = JsonSerializerOptionsHelper.Create(options, this);
        var entityConverter = (JsonConverter <bool>)newOptions.GetConverter(typeof(bool));

        entityConverter.Write(writer, value, newOptions);
    }
    public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var newOptions = JsonSerializerOptionsHelper.Create(options, x => x == this);

        var converterFactory    = newOptions.Converters.FirstOrDefault(x => x is AbpHasExtraPropertiesJsonConverterFactory).As <AbpHasExtraPropertiesJsonConverterFactory>();
        var newConverterFactory = new AbpHasExtraPropertiesJsonConverterFactory();

        if (converterFactory != null)
        {
            newOptions.Converters.Remove(converterFactory);
            newConverterFactory.AddExcludeTypes(converterFactory.GetExcludeTypes().ToArray());
        }

        newConverterFactory.AddExcludeTypes(typeToConvert);
        newOptions.Converters.Add(newConverterFactory);

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

        if (rootElement.ValueKind == JsonValueKind.Object)
        {
            var extensibleObject = JsonSerializer.Deserialize <T>(rootElement.GetRawText(), newOptions);

            var extraPropertiesJsonProperty = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(IHasExtraProperties.ExtraProperties), StringComparison.OrdinalIgnoreCase));
            if (extraPropertiesJsonProperty.Value.ValueKind == JsonValueKind.Object)
            {
                var extraPropertyDictionary = JsonSerializer.Deserialize(extraPropertiesJsonProperty.Value.GetRawText(), typeof(ExtraPropertyDictionary), newOptions);
                ObjectHelper.TrySetProperty(extensibleObject, x => x.ExtraProperties, () => extraPropertyDictionary);
            }

            return(extensibleObject);
        }

        throw new JsonException("RootElement's ValueKind is not Object!");
    }
Exemple #4
0
    public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
    {
        var newOptions = JsonSerializerOptionsHelper.Create(options, x =>
                                                            x == this ||
                                                            x.GetType() == typeof(AbpStringToEnumFactory));

        JsonSerializer.Serialize(writer, value, newOptions);
    }
Exemple #5
0
        public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
        {
            var newOptions = JsonSerializerOptionsHelper.Create(options, x =>
                                                                x == this ||
                                                                x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory));

            JsonSerializer.Serialize(writer, value, newOptions);
        }
Exemple #6
0
    public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var newOptions = JsonSerializerOptionsHelper.Create(options, x =>
                                                            x == this ||
                                                            x.GetType() == typeof(AbpStringToEnumFactory));

        newOptions.Converters.Add(_innerJsonStringEnumConverter.CreateConverter(typeToConvert, newOptions));
        return(JsonSerializer.Deserialize <T>(ref reader, newOptions));
    }
Exemple #7
0
        public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var newOptions = JsonSerializerOptionsHelper.Create(options, x =>
                                                                x == this ||
                                                                x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory));

            var rootElement      = JsonDocument.ParseValue(ref reader).RootElement;
            var extensibleObject = JsonSerializer.Deserialize <T>(rootElement.GetRawText(), newOptions);

            var extraProperties = rootElement.EnumerateObject().FirstOrDefault(x =>
                                                                               x.Name.Equals(nameof(IHasExtraProperties.ExtraProperties), StringComparison.OrdinalIgnoreCase))
                                  .Value.GetRawText();
            var extraPropertyDictionary = JsonSerializer.Deserialize(extraProperties, typeof(ExtraPropertyDictionary), newOptions);

            ObjectHelper.TrySetProperty(extensibleObject, x => x.ExtraProperties, () => extraPropertyDictionary);

            return(extensibleObject);
        }
Exemple #8
0
    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)
        {
            _readJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this);

            var selectionStringValueItem =
                itemsJsonProperty.Value.Deserialize <LocalizableSelectionStringValueItem[]>(_readJsonSerializerOptions) ??
                Array.Empty <LocalizableSelectionStringValueItem>();

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

        throw new JsonException($"Can't to get the {nameof(ISelectionStringValueItemSource.Items)} property of {nameof(ISelectionStringValueItemSource)}!");
    }
Exemple #9
0
    public override IStringValueType 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(IStringValueType.Name), StringComparison.OrdinalIgnoreCase));

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

            _readJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this, new ValueValidatorJsonConverter(), new SelectionStringValueItemSourceJsonConverter());

            return(name switch
            {
                "SelectionStringValueType" => rootElement.Deserialize <SelectionStringValueType>(_readJsonSerializerOptions),
                "FreeTextStringValueType" => rootElement.Deserialize <FreeTextStringValueType>(_readJsonSerializerOptions),
                "ToggleStringValueType" => rootElement.Deserialize <ToggleStringValueType>(_readJsonSerializerOptions),
                _ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!")
            });
        public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options)
        {
            var newOptions = JsonSerializerOptionsHelper.Create(options, this);

            JsonSerializer.Serialize(writer, objectToWrite, newOptions);
        }
Exemple #11
0
 public override void Write(Utf8JsonWriter writer, IValueValidator value, JsonSerializerOptions options)
 {
     _writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this);
     JsonSerializer.Serialize(writer, value, value.GetType(), _writeJsonSerializerOptions);
 }
Exemple #12
0
 public override void Write(Utf8JsonWriter writer, TEntity value, JsonSerializerOptions options)
 {
     _writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this);
     JsonSerializer.Serialize(writer, value, _writeJsonSerializerOptions);
 }
Exemple #13
0
 public override void Write(Utf8JsonWriter writer, ISelectionStringValueItemSource value, JsonSerializerOptions options)
 {
     _writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, this);
     JsonSerializer.Serialize(writer, value, value.GetType(), _writeJsonSerializerOptions);
 }