Exemplo n.º 1
0
        internal virtual void SerializeInternal([Nullable(1)] JsonWriter jsonWriter, object value, Type objectType)
        {
            ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");
            Formatting?formatting = null;

            if (this._formatting != null)
            {
                Formatting formatting2 = jsonWriter.Formatting;
                Formatting?formatting3 = this._formatting;
                if (!(formatting2 == formatting3.GetValueOrDefault() & formatting3 != null))
                {
                    formatting            = new Formatting?(jsonWriter.Formatting);
                    jsonWriter.Formatting = this._formatting.GetValueOrDefault();
                }
            }
            DateFormatHandling?dateFormatHandling = null;

            if (this._dateFormatHandling != null)
            {
                DateFormatHandling dateFormatHandling2 = jsonWriter.DateFormatHandling;
                DateFormatHandling?dateFormatHandling3 = this._dateFormatHandling;
                if (!(dateFormatHandling2 == dateFormatHandling3.GetValueOrDefault() & dateFormatHandling3 != null))
                {
                    dateFormatHandling            = new DateFormatHandling?(jsonWriter.DateFormatHandling);
                    jsonWriter.DateFormatHandling = this._dateFormatHandling.GetValueOrDefault();
                }
            }
            DateTimeZoneHandling?dateTimeZoneHandling = null;

            if (this._dateTimeZoneHandling != null)
            {
                DateTimeZoneHandling dateTimeZoneHandling2 = jsonWriter.DateTimeZoneHandling;
                DateTimeZoneHandling?dateTimeZoneHandling3 = this._dateTimeZoneHandling;
                if (!(dateTimeZoneHandling2 == dateTimeZoneHandling3.GetValueOrDefault() & dateTimeZoneHandling3 != null))
                {
                    dateTimeZoneHandling            = new DateTimeZoneHandling?(jsonWriter.DateTimeZoneHandling);
                    jsonWriter.DateTimeZoneHandling = this._dateTimeZoneHandling.GetValueOrDefault();
                }
            }
            FloatFormatHandling?floatFormatHandling = null;

            if (this._floatFormatHandling != null)
            {
                FloatFormatHandling floatFormatHandling2 = jsonWriter.FloatFormatHandling;
                FloatFormatHandling?floatFormatHandling3 = this._floatFormatHandling;
                if (!(floatFormatHandling2 == floatFormatHandling3.GetValueOrDefault() & floatFormatHandling3 != null))
                {
                    floatFormatHandling            = new FloatFormatHandling?(jsonWriter.FloatFormatHandling);
                    jsonWriter.FloatFormatHandling = this._floatFormatHandling.GetValueOrDefault();
                }
            }
            StringEscapeHandling?stringEscapeHandling = null;

            if (this._stringEscapeHandling != null)
            {
                StringEscapeHandling stringEscapeHandling2 = jsonWriter.StringEscapeHandling;
                StringEscapeHandling?stringEscapeHandling3 = this._stringEscapeHandling;
                if (!(stringEscapeHandling2 == stringEscapeHandling3.GetValueOrDefault() & stringEscapeHandling3 != null))
                {
                    stringEscapeHandling            = new StringEscapeHandling?(jsonWriter.StringEscapeHandling);
                    jsonWriter.StringEscapeHandling = this._stringEscapeHandling.GetValueOrDefault();
                }
            }
            CultureInfo cultureInfo = null;

            if (this._culture != null && !this._culture.Equals(jsonWriter.Culture))
            {
                cultureInfo        = jsonWriter.Culture;
                jsonWriter.Culture = this._culture;
            }
            string dateFormatString = null;

            if (this._dateFormatStringSet && jsonWriter.DateFormatString != this._dateFormatString)
            {
                dateFormatString            = jsonWriter.DateFormatString;
                jsonWriter.DateFormatString = this._dateFormatString;
            }
            TraceJsonWriter traceJsonWriter = (this.TraceWriter == null || this.TraceWriter.LevelFilter < TraceLevel.Verbose) ? null : new TraceJsonWriter(jsonWriter);

            new JsonSerializerInternalWriter(this).Serialize(traceJsonWriter ?? jsonWriter, value, objectType);
            if (traceJsonWriter != null)
            {
                this.TraceWriter.Trace(TraceLevel.Verbose, traceJsonWriter.GetSerializedJsonMessage(), null);
            }
            if (formatting != null)
            {
                jsonWriter.Formatting = formatting.GetValueOrDefault();
            }
            if (dateFormatHandling != null)
            {
                jsonWriter.DateFormatHandling = dateFormatHandling.GetValueOrDefault();
            }
            if (dateTimeZoneHandling != null)
            {
                jsonWriter.DateTimeZoneHandling = dateTimeZoneHandling.GetValueOrDefault();
            }
            if (floatFormatHandling != null)
            {
                jsonWriter.FloatFormatHandling = floatFormatHandling.GetValueOrDefault();
            }
            if (stringEscapeHandling != null)
            {
                jsonWriter.StringEscapeHandling = stringEscapeHandling.GetValueOrDefault();
            }
            if (this._dateFormatStringSet)
            {
                jsonWriter.DateFormatString = dateFormatString;
            }
            if (cultureInfo != null)
            {
                jsonWriter.Culture = cultureInfo;
            }
        }
 public ReflectionValueProvider(MemberInfo memberInfo)
 {
     ValidationUtils.ArgumentNotNull(memberInfo, "memberInfo");
     this._memberInfo = memberInfo;
 }
Exemplo n.º 3
0
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            JProperty property1;
            JValue    value2;
            JProperty property;

            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo = r as IJsonLineInfo;
            JContainer    parent   = this;

Label_0014:
            property1 = parent as JProperty;
            if (property1 == null)
            {
            }
            if (null.Value != null)
            {
                if (parent == this)
                {
                    return;
                }
                parent = parent.Parent;
            }
            switch (r.TokenType)
            {
            case JsonToken.None:
                goto Label_0247;

            case JsonToken.StartObject:
            {
                JObject content = new JObject();
                content.SetLineInfo(lineInfo, settings);
                parent.Add(content);
                parent = content;
                goto Label_0247;
            }

            case JsonToken.StartArray:
            {
                JArray content = new JArray();
                content.SetLineInfo(lineInfo, settings);
                parent.Add(content);
                parent = content;
                goto Label_0247;
            }

            case JsonToken.StartConstructor:
            {
                JConstructor content = new JConstructor(r.Value.ToString());
                content.SetLineInfo(lineInfo, settings);
                parent.Add(content);
                parent = content;
                goto Label_0247;
            }

            case JsonToken.PropertyName:
            {
                string name = r.Value.ToString();
                property = new JProperty(name);
                property.SetLineInfo(lineInfo, settings);
                JProperty property2 = ((JObject)parent).Property(name);
                if (property2 != null)
                {
                    property2.Replace(property);
                    break;
                }
                parent.Add(property);
                break;
            }

            case JsonToken.Comment:
                if ((settings != null) && (settings.CommentHandling == CommentHandling.Load))
                {
                    value2 = JValue.CreateComment(r.Value.ToString());
                    value2.SetLineInfo(lineInfo, settings);
                    parent.Add(value2);
                }
                goto Label_0247;

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            case JsonToken.Boolean:
            case JsonToken.Date:
            case JsonToken.Bytes:
                value2 = new JValue(r.Value);
                value2.SetLineInfo(lineInfo, settings);
                parent.Add(value2);
                goto Label_0247;

            case JsonToken.Null:
                value2 = JValue.CreateNull();
                value2.SetLineInfo(lineInfo, settings);
                parent.Add(value2);
                goto Label_0247;

            case JsonToken.Undefined:
                value2 = JValue.CreateUndefined();
                value2.SetLineInfo(lineInfo, settings);
                parent.Add(value2);
                goto Label_0247;

            case JsonToken.EndObject:
                if (parent != this)
                {
                    parent = parent.Parent;
                    goto Label_0247;
                }
                return;

            case JsonToken.EndArray:
                if (parent != this)
                {
                    parent = parent.Parent;
                    goto Label_0247;
                }
                return;

            case JsonToken.EndConstructor:
                if (parent != this)
                {
                    parent = parent.Parent;
                    goto Label_0247;
                }
                return;

            default:
                throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
            }
            parent = property;
Label_0247:
            if (r.Read())
            {
                goto Label_0014;
            }
        }
Exemplo n.º 4
0
        // Token: 0x060013CD RID: 5069 RVA: 0x00068C14 File Offset: 0x00066E14
        private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            string typeId  = this.GetTypeId(type, false);
            string typeId2 = this.GetTypeId(type, true);

            if (!StringUtils.IsNullOrEmpty(typeId))
            {
                JsonSchema schema = this._resolver.GetSchema(typeId);
                if (schema != null)
                {
                    if (valueRequired != Required.Always && !JsonSchemaGenerator.HasFlag(schema.Type, JsonSchemaType.Null))
                    {
                        schema.Type |= JsonSchemaType.Null;
                    }
                    if (required)
                    {
                        bool?required2 = schema.Required;
                        if (!(required2.GetValueOrDefault() & required2 != null))
                        {
                            schema.Required = new bool?(true);
                        }
                    }
                    return(schema);
                }
            }
            if (this._stack.Any((JsonSchemaGenerator.TypeSchema tc) => tc.Type == type))
            {
                throw new JsonException("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, type));
            }
            JsonContract jsonContract = this.ContractResolver.ResolveContract(type);
            bool         flag         = (jsonContract.Converter ?? jsonContract.InternalConverter) != null;

            this.Push(new JsonSchemaGenerator.TypeSchema(type, new JsonSchema()));
            if (typeId2 != null)
            {
                this.CurrentSchema.Id = typeId2;
            }
            if (required)
            {
                this.CurrentSchema.Required = new bool?(true);
            }
            this.CurrentSchema.Title       = this.GetTitle(type);
            this.CurrentSchema.Description = this.GetDescription(type);
            if (flag)
            {
                this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
            }
            else
            {
                switch (jsonContract.ContractType)
                {
                case JsonContractType.Object:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateObjectSchema(type, (JsonObjectContract)jsonContract);
                    break;

                case JsonContractType.Array:
                    {
                        this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Array, valueRequired));
                        this.CurrentSchema.Id   = this.GetTypeId(type, false);
                        JsonArrayAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(type);
                        bool flag2 = cachedAttribute == null || cachedAttribute.AllowNullItems;
                        Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                        if (collectionItemType != null)
                        {
                            this.CurrentSchema.Items = new List <JsonSchema>();
                            this.CurrentSchema.Items.Add(this.GenerateInternal(collectionItemType, (!flag2) ? Required.Always : Required.Default, false));
                        }
                        break;
                    }

                case JsonContractType.Primitive:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.GetJsonSchemaType(type, valueRequired));
                    JsonSchemaType?type2 = this.CurrentSchema.Type;
                    if ((type2.GetValueOrDefault() == JsonSchemaType.Integer & type2 != null) && type.IsEnum() && !type.IsDefined(typeof(FlagsAttribute), true))
                    {
                        this.CurrentSchema.Enum = new List <JToken>();
                        EnumInfo enumValuesAndNames = EnumUtils.GetEnumValuesAndNames(type);
                        for (int i = 0; i < enumValuesAndNames.Names.Length; i++)
                        {
                            ulong  value = enumValuesAndNames.Values[i];
                            JToken item  = JToken.FromObject(Enum.ToObject(type, value));
                            this.CurrentSchema.Enum.Add(item);
                        }
                    }
                    break;
                }

                case JsonContractType.String:
                {
                    JsonSchemaType value2 = (!ReflectionUtils.IsNullable(jsonContract.UnderlyingType)) ? JsonSchemaType.String : this.AddNullType(JsonSchemaType.String, valueRequired);
                    this.CurrentSchema.Type = new JsonSchemaType?(value2);
                    break;
                }

                case JsonContractType.Dictionary:
                {
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    Type type3;
                    Type type4;
                    ReflectionUtils.GetDictionaryKeyValueTypes(type, out type3, out type4);
                    if (type3 != null && this.ContractResolver.ResolveContract(type3).ContractType == JsonContractType.Primitive)
                    {
                        this.CurrentSchema.AdditionalProperties = this.GenerateInternal(type4, Required.Default, false);
                    }
                    break;
                }

                case JsonContractType.Dynamic:
                case JsonContractType.Linq:
                    this.CurrentSchema.Type = new JsonSchemaType?(JsonSchemaType.Any);
                    break;

                case JsonContractType.Serializable:
                    this.CurrentSchema.Type = new JsonSchemaType?(this.AddNullType(JsonSchemaType.Object, valueRequired));
                    this.CurrentSchema.Id   = this.GetTypeId(type, false);
                    this.GenerateISerializableContract(type, (JsonISerializableContract)jsonContract);
                    break;

                default:
                    throw new JsonException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, jsonContract));
                }
            }
            return(this.Pop().Schema);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ReflectionValueProvider"/> class.
 /// </summary>
 /// <param name="memberInfo">The member info.</param>
 public ReflectionValueProvider(MemberInfo memberInfo)
 {
     ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo));
     _memberInfo = memberInfo;
 }
        protected JsonSerializerInternalBase(JsonSerializer serializer)
        {
            ValidationUtils.ArgumentNotNull(serializer, "serializer");

            Serializer = serializer;
        }
Exemplo n.º 7
0
        public JsonFormatterConverter(JsonSerializer serializer)
        {
            ValidationUtils.ArgumentNotNull(serializer, "serializer");

            _serializer = serializer;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JEnumerable{T}"/> struct.
        /// </summary>
        /// <param name="enumerable">The enumerable.</param>
        public JEnumerable(IEnumerable <T> enumerable)
        {
            ValidationUtils.ArgumentNotNull(enumerable, nameof(enumerable));

            _enumerable = enumerable;
        }
Exemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonValidatingReader"/> class that
 /// validates the content returned from the given <see cref="JsonReader"/>.
 /// </summary>
 /// <param name="reader">The <see cref="JsonReader"/> to read from while validating.</param>
 public JsonValidatingReader(JsonReader reader)
 {
     ValidationUtils.ArgumentNotNull(reader, "reader");
     _reader = reader;
     _stack  = new Stack <SchemaScope>();
 }
Exemplo n.º 10
0
        private JsonSchema GenerateInternal(Type type, bool valueRequired)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            string resolvedId = GetTypeId(type, false);
            string explicitId = GetTypeId(type, true);

            if (!string.IsNullOrEmpty(resolvedId))
            {
                JsonSchema resolvedSchema = _resolver.GetSchema(resolvedId);
                if (resolvedSchema != null)
                {
                    return(resolvedSchema);
                }
            }

            // test for unresolved circular reference
            if (_stack.Any(tc => tc.Type == type))
            {
                throw new Exception("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, type));
            }

            Push(new TypeSchema(type, new JsonSchema()));

            if (explicitId != null)
            {
                CurrentSchema.Id = explicitId;
            }

            CurrentSchema.Title       = GetTitle(type);
            CurrentSchema.Description = GetDescription(type);

            if (CollectionUtils.IsDictionaryType(type))
            {
                CurrentSchema.Type = JsonSchemaType.Object;

                Type keyType;
                Type valueType;
                ReflectionUtils.GetDictionaryKeyValueTypes(type, out keyType, out valueType);

                if (keyType != null)
                {
                    // can be converted to a string
                    if (typeof(IConvertible).IsAssignableFrom(keyType))
                    {
                        CurrentSchema.AdditionalProperties = GenerateInternal(valueType, false);
                    }
                }
            }
            else if (CollectionUtils.IsCollectionType(type))
            {
                CurrentSchema.Type = JsonSchemaType.Array;

                JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetJsonContainerAttribute(type) as JsonArrayAttribute;
                bool allowNullItem = (arrayAttribute != null) ? arrayAttribute.AllowNullItems : false;

                Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
                if (collectionItemType != null)
                {
                    CurrentSchema.Items = new List <JsonSchema>();
                    CurrentSchema.Items.Add(GenerateInternal(collectionItemType, !allowNullItem));
                }
            }
            else
            {
                CurrentSchema.Type = GetJsonSchemaType(type, valueRequired);

                if (HasFlag(CurrentSchema.Type, JsonSchemaType.Object))
                {
                    CurrentSchema.Id = GetTypeId(type, false);

                    JsonObjectContract contract = ContractResolver.ResolveContract(type) as JsonObjectContract;

                    if (contract == null)
                    {
                        throw new Exception("Could not resolve contract for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type));
                    }

                    CurrentSchema.Properties = new Dictionary <string, JsonSchema>();
                    foreach (JsonProperty property in contract.Properties)
                    {
                        if (!property.Ignored)
                        {
                            Type       propertyMemberType = ReflectionUtils.GetMemberUnderlyingType(property.Member);
                            JsonSchema propertySchema     = GenerateInternal(propertyMemberType, property.Required);

                            if (property.DefaultValue != null)
                            {
                                propertySchema.Default = JToken.FromObject(property.DefaultValue);
                            }

                            CurrentSchema.Properties.Add(property.PropertyName, propertySchema);
                        }
                    }

                    if (type.IsSealed)
                    {
                        CurrentSchema.AllowAdditionalProperties = false;
                    }
                }
                else if (CurrentSchema.Type == JsonSchemaType.Integer && type.IsEnum && !type.IsDefined(typeof(FlagsAttribute), true))
                {
                    CurrentSchema.Enum    = new List <JToken>();
                    CurrentSchema.Options = new Dictionary <JToken, string>();

                    EnumValues <ulong> enumValues = EnumUtils.GetNamesAndValues <ulong>(type);
                    foreach (EnumValue <ulong> enumValue in enumValues)
                    {
                        JToken value = JToken.FromObject(enumValue.Value);

                        CurrentSchema.Enum.Add(value);
                        CurrentSchema.Options.Add(value, enumValue.Name);
                    }
                }
            }

            return(Pop().Schema);
        }
Exemplo n.º 11
0
 internal ValidationEventArgs(JsonSchemaException ex)
 {
     ValidationUtils.ArgumentNotNull(ex, "ex");
     _ex = ex;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ReflectionAttributeProvider"/> class.
 /// </summary>
 /// <param name="attributeProvider">The instance to get attributes for. This parameter should be a <see cref="System.Type"/>, <see cref="MemberInfo"/>, <see cref="ParameterInfo"/> or <see cref="Assembly"/>.</param>
 public ReflectionAttributeProvider(object attributeProvider)
 {
     ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider");
     _attributeProvider = attributeProvider;
 }