<commit_before>﻿//-----------------------------------------------------------------------
// <copyright file="JsonSchema4.cs" company="NJsonSchema">
//     Copyright (c) <NAME>. All rights reserved.
// </copyright>
// <license>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author><NAME>, <EMAIL></author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema.Collections;

namespace NJsonSchema
{
    public partial class JsonSchema4
    {
        [OnDeserialized]
        internal void OnDeserialized(StreamingContext ctx)
        {
            Initialize();
        }

        /// <summary>Gets or sets the type name (class name of the object). </summary>
        [JsonProperty("typeName", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -100 + 4)]
        public string TypeName { get; set; }

        /// <summary>Gets or sets the enumeration names (optional, draft v5). </summary>
        [JsonIgnore]
        public Collection<string> EnumerationNames { get; set; }

        [JsonProperty("additionalItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal object AdditionalItemsRaw
        {
            get
            {
                if (AdditionalItemsSchema != null)
                    return AdditionalItemsSchema;
                if (!AllowAdditionalItems)
                    return false;
                return null;
            }
            set
            {
                if (value is bool)
                    AllowAdditionalItems = (bool)value;
                else if (value != null)
                    AdditionalItemsSchema = FromJsonWithoutReferenceHandling(value.ToString());
            }
        }

        [JsonProperty("additionalProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal object AdditionalPropertiesRaw
        {
            get
            {
                if (AdditionalPropertiesSchema != null)
                    return AdditionalPropertiesSchema;
                if (!AllowAdditionalProperties)
                    return false;
                return null;
            }
            set
            {
                if (value is bool)
                    AllowAdditionalProperties = (bool)value;
                else if (value != null)
                    AdditionalPropertiesSchema = FromJsonWithoutReferenceHandling(value.ToString());
            }
        }

        [JsonProperty("items", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal object ItemsRaw
        {
            get
            {
                if (Item != null)
                    return Item;
                if (Items.Count > 0)
                    return Items;
                return null;
            }
            set
            {
                if (value is JArray)
                    Items = new ObservableCollection<JsonSchema4>(((JArray)value).Select(t => FromJsonWithoutReferenceHandling(t.ToString())));
                else if (value != null)
                    Item = FromJsonWithoutReferenceHandling(value.ToString());
            }
        }

        [JsonProperty("type", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -100 + 3)]
        internal object TypeRaw
        {
            get
            {
                var flags = Enum.GetValues(Type.GetType())
                    .Cast<Enum>().Where(v => Type.HasFlag(v))
                    .OfType<JsonObjectType>()
                    .Where(v => v != JsonObjectType.None)
                    .ToArray();

                if (flags.Length > 1)
                    return new JArray(flags.Select(f => new JValue(f.ToString().ToLower())));

                if (flags.Length == 1)
                    return new JValue(flags[0].ToString().ToLower());

                return null;
            }
            set
            {
                if (value is JArray)
                    Type = ((JArray)value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToObjectType(token.ToString()));
                else
                {
                    // Section 5.5.2:
                    // http://json-schema.org/latest/json-schema-validation.html#anchor79
                    // "type" must be either string or array.
                    // At this point we expect a valid string
                    // with one of the 7 primitive names, anything else is invalid.
                    Type = ConvertStringToObjectType(value as string);
                }
            }
        }

        [JsonProperty("required", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal ICollection<string> RequiredPropertiesRaw
        {
            get { return RequiredProperties != null && RequiredProperties.Count > 0 ? RequiredProperties : null; }
            set { RequiredProperties = value; }
        }

        [JsonProperty("properties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal IDictionary<string, JsonSchema4> PropertiesRaw
        {
            get
            {
                return Properties != null && Properties.Count > 0 ?
                    Properties.ToDictionary(p => p.Key, p => (JsonSchema4)p.Value) : null;
            }
            set
            {
                Properties = value != null ?
                    new ObservableDictionary<string, JsonProperty>(value.ToDictionary(p => p.Key, p => JsonProperty.FromJsonSchema(p.Key, p.Value))) :
                    new ObservableDictionary<string, JsonProperty>();
            }
        }

        [JsonProperty("patternProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal IDictionary<string, JsonSchema4> PatternPropertiesRaw
        {
            get
            {<commit_msg>Caching TypeRaw property value.<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="JsonSchema4.cs" company="NJsonSchema">
//     Copyright (c) <NAME>. All rights reserved.
// </copyright>
// <license>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author><NAME>, <EMAIL></author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema.Collections;

namespace NJsonSchema
{
    public partial class JsonSchema4
    {
        [OnDeserialized]
        internal void OnDeserialized(StreamingContext ctx)
        {
            Initialize();
        }

        /// <summary>Gets or sets the type name (class name of the object). </summary>
        [JsonProperty("typeName", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -100 + 4)]
        public string TypeName { get; set; }

        /// <summary>Gets or sets the enumeration names (optional, draft v5). </summary>
        [JsonIgnore]
        public Collection<string> EnumerationNames { get; set; }

        [JsonProperty("additionalItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal object AdditionalItemsRaw
        {
            get
            {
                if (AdditionalItemsSchema != null)
                    return AdditionalItemsSchema;
                if (!AllowAdditionalItems)
                    return false;
                return null;
            }
            set
            {
                if (value is bool)
                    AllowAdditionalItems = (bool)value;
                else if (value != null)
                    AdditionalItemsSchema = FromJsonWithoutReferenceHandling(value.ToString());
            }
        }

        [JsonProperty("additionalProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal object AdditionalPropertiesRaw
        {
            get
            {
                if (AdditionalPropertiesSchema != null)
                    return AdditionalPropertiesSchema;
                if (!AllowAdditionalProperties)
                    return false;
                return null;
            }
            set
            {
                if (value is bool)
                    AllowAdditionalProperties = (bool)value;
                else if (value != null)
                    AdditionalPropertiesSchema = FromJsonWithoutReferenceHandling(value.ToString());
            }
        }

        [JsonProperty("items", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal object ItemsRaw
        {
            get
            {
                if (Item != null)
                    return Item;
                if (Items.Count > 0)
                    return Items;
                return null;
            }
            set
            {
                if (value is JArray)
                    Items = new ObservableCollection<JsonSchema4>(((JArray)value).Select(t => FromJsonWithoutReferenceHandling(t.ToString())));
                else if (value != null)
                    Item = FromJsonWithoutReferenceHandling(value.ToString());
            }
        }

        private Lazy<object> typeRaw;

        private void ResetTypeRaw()
        {
            this.typeRaw = new Lazy<object>(() => 
            {
                var flags = Enum.GetValues(Type.GetType())
                    .Cast<Enum>().Where(v => Type.HasFlag(v))
                    .OfType<JsonObjectType>()
                    .Where(v => v != JsonObjectType.None)
                    .ToArray();

                if (flags.Length > 1)
                    return new JArray(flags.Select(f => new JValue(f.ToString().ToLower())));

                if (flags.Length == 1)
                    return new JValue(flags[0].ToString().ToLower());

                return null;
            });
        }

        [JsonProperty("type", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -100 + 3)]
        internal object TypeRaw
        {
            get
            {
                if(this.typeRaw == null)
                    this.ResetTypeRaw();

                return this.typeRaw.Value;
            }
            set
            {
                if (value is JArray)
                    Type = ((JArray)value).Aggregate(JsonObjectType.None, (type, token) => type | ConvertStringToObjectType(token.ToString()));
                else
                {
                    // Section 5.5.2:
                    // http://json-schema.org/latest/json-schema-validation.html#anchor79
                    // "type" must be either string or array.
                    // At this point we expect a valid string
                    // with one of the 7 primitive names, anything else is invalid.
                    Type = ConvertStringToObjectType(value as string);
                }

                this.ResetTypeRaw();
            }
        }

        [JsonProperty("required", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal ICollection<string> RequiredPropertiesRaw
        {
            get { return RequiredProperties != null && RequiredProperties.Count > 0 ? RequiredProperties : null; }
            set { RequiredProperties = value; }
        }

        [JsonProperty("properties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal IDictionary<string, JsonSchema4> PropertiesRaw
        {
            get
            {
                return Properties != null && Properties.Count > 0 ?
                    Properties.ToDictionary(p => p.Key, p => (JsonSchema4)p.Value) : null;
            }
            set
            {
                Properties = value != null ?
                    new ObservableDictionary<string, JsonProperty>(value.ToDictionary(p => p.Key, p => JsonProperty.FromJsonSchema(p.Key, p.Value))) :
                    new ObservableDictionary<string, JsonProperty>();
            }
        }

        [JsonProperty("patternProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
        internal IDictionary<string, JsonSchema4> PatternPropertiesRaw
        {
            get
            {
{'fraction_non_alphanumeric': 0.11278912646053572, 'fraction_numerical': 0.004292186630633495, 'mean_word_length': 1.5326086956521738, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 76, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1469', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2818, 'n_words': 652}