Ejemplo n.º 1
0
        private void WriteToken(IList <JsonSchemaModel> schemas)
        {
            foreach (SchemaScope schemaScope in _stack)
            {
                bool isInUniqueArray = (schemaScope.TokenType == JTokenType.Array && schemaScope.IsUniqueArray && schemaScope.ArrayItemCount > 0);

                if (isInUniqueArray || schemas.Any(s => s.Enum != null))
                {
                    if (schemaScope.CurrentItemWriter == null)
                    {
                        if (JsonTokenUtils.IsEndToken(_reader.TokenType))
                        {
                            continue;
                        }

                        schemaScope.CurrentItemWriter = new JTokenWriter();
                    }

                    schemaScope.CurrentItemWriter.WriteToken(_reader, false);

                    // finished writing current item
                    if (schemaScope.CurrentItemWriter.Top == 0 && _reader.TokenType != JsonToken.PropertyName)
                    {
                        JToken finishedItem = schemaScope.CurrentItemWriter.Token;

                        // start next item with new writer
                        schemaScope.CurrentItemWriter = null;

                        if (isInUniqueArray)
                        {
                            if (schemaScope.UniqueArrayItems.Contains(finishedItem, JToken.EqualityComparer))
                            {
                                RaiseError("Non-unique array item at index {0}.".FormatWith(CultureInfo.InvariantCulture, schemaScope.ArrayItemCount - 1), schemaScope.Schemas.First(s => s.UniqueItems));
                            }

                            schemaScope.UniqueArrayItems.Add(finishedItem);
                        }
                        else if (schemas.Any(s => s.Enum != null))
                        {
                            foreach (JsonSchemaModel schema in schemas)
                            {
                                if (schema.Enum != null)
                                {
                                    if (!schema.Enum.ContainsValue(finishedItem, JToken.EqualityComparer))
                                    {
                                        StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                                        finishedItem.WriteTo(new JsonTextWriter(sw));

                                        RaiseError("Value {0} is not defined in enum.".FormatWith(CultureInfo.InvariantCulture, sw.ToString()), schema);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void WriteToken(IList <JsonSchemaModel> schemas)
        {
            foreach (JsonValidatingReader.SchemaScope schemaScope in this._stack)
            {
                bool flag = schemaScope.TokenType == JTokenType.Array && schemaScope.IsUniqueArray &&
                            schemaScope.ArrayItemCount > 0;
                if (flag || schemas.Any <JsonSchemaModel>((Func <JsonSchemaModel, bool>)(s => s.Enum != null)))
                {
                    if (schemaScope.CurrentItemWriter == null)
                    {
                        if (!JsonTokenUtils.IsEndToken(this._reader.TokenType))
                        {
                            schemaScope.CurrentItemWriter = new JTokenWriter();
                        }
                        else
                        {
                            continue;
                        }
                    }

                    schemaScope.CurrentItemWriter.WriteToken(this._reader, false);
                    if (schemaScope.CurrentItemWriter.Top == 0 && this._reader.TokenType != JsonToken.PropertyName)
                    {
                        JToken token = schemaScope.CurrentItemWriter.Token;
                        schemaScope.CurrentItemWriter = (JTokenWriter)null;
                        if (flag)
                        {
                            if (schemaScope.UniqueArrayItems.Contains <JToken>(token,
                                                                               (IEqualityComparer <JToken>)JToken.EqualityComparer))
                            {
                                this.RaiseError(
                                    "Non-unique array item at index {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture,
                                                                                     (object)(schemaScope.ArrayItemCount - 1)),
                                    schemaScope.Schemas.First <JsonSchemaModel>((Func <JsonSchemaModel, bool>)(s => s.UniqueItems)));
                            }
                            schemaScope.UniqueArrayItems.Add(token);
                        }
                        else if (schemas.Any <JsonSchemaModel>((Func <JsonSchemaModel, bool>)(s => s.Enum != null)))
                        {
                            foreach (JsonSchemaModel schema in (IEnumerable <JsonSchemaModel>)schemas)
                            {
                                if (schema.Enum != null &&
                                    !schema.Enum.ContainsValue <JToken>(token, (IEqualityComparer <JToken>)JToken.EqualityComparer))
                                {
                                    StringWriter stringWriter = new StringWriter((IFormatProvider)CultureInfo.InvariantCulture);
                                    token.WriteTo((JsonWriter) new JsonTextWriter((TextWriter)stringWriter));
                                    this.RaiseError(
                                        "Value {0} is not defined in enum.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture,
                                                                                       (object)stringWriter.ToString()), schema);
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        internal string ReadAsStringInternal()
        {
            _readType = ReadType.ReadAsString;

            JsonToken t;

            do
            {
                if (!ReadInternal())
                {
                    SetToken(JsonToken.None);
                    return(null);
                }
                else
                {
                    t = TokenType;
                }
            } while (t == JsonToken.Comment);

            if (t == JsonToken.String)
            {
                return((string)Value);
            }

            if (t == JsonToken.Null)
            {
                return(null);
            }

            if (JsonTokenUtils.IsPrimitiveToken(t))
            {
                if (Value != null)
                {
                    string s;
                    if (Value is IFormattable)
                    {
                        s = ((IFormattable)Value).ToString(null, Culture);
                    }
                    else
                    {
                        s = Value.ToString();
                    }

                    SetToken(JsonToken.String, s, false);
                    return(s);
                }
            }

            if (t == JsonToken.EndArray)
            {
                return(null);
            }

            throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Ejemplo n.º 4
0
 // Token: 0x06000AAC RID: 2732 RVA: 0x000402CC File Offset: 0x0003E4CC
 public void Skip()
 {
     if (this.TokenType == JsonToken.PropertyName)
     {
         this.Read();
     }
     if (JsonTokenUtils.IsStartToken(this.TokenType))
     {
         int depth = this.Depth;
         while (this.Read() && depth < this.Depth)
         {
         }
     }
 }
Ejemplo n.º 5
0
        public virtual string ReadAsString()
        {
            string    str;
            JsonToken contentToken = this.GetContentToken();

            if (contentToken <= JsonToken.String)
            {
                if (contentToken == JsonToken.None)
                {
                    return(null);
                }
                if (contentToken == JsonToken.String)
                {
                    return((string)this.Value);
                }
                goto Label0;
            }
            else if (contentToken != JsonToken.Null)
            {
                if (contentToken != JsonToken.EndArray)
                {
                    goto Label0;
                }
            }
            return(null);

Label0:
            if (JsonTokenUtils.IsPrimitiveToken(contentToken))
            {
                object value = this.Value;
                if (value != null)
                {
                    IFormattable formattable  = value as IFormattable;
                    IFormattable formattable1 = formattable;
                    if (formattable == null)
                    {
                        Uri uri  = value as Uri;
                        Uri uri1 = uri;
                        str = (uri != null ? uri1.OriginalString : value.ToString());
                    }
                    else
                    {
                        str = formattable1.ToString(null, this.Culture);
                    }
                    this.SetToken(JsonToken.String, str, false);
                    return(str);
                }
            }
            throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
        }
Ejemplo n.º 6
0
        // Token: 0x06000C01 RID: 3073 RVA: 0x0004F300 File Offset: 0x0004D500
        public virtual string ReadAsString()
        {
            JsonToken contentToken = this.GetContentToken();

            if (contentToken <= JsonToken.String)
            {
                if (contentToken == JsonToken.None)
                {
                    goto IL_A1;
                }
                if (contentToken == JsonToken.String)
                {
                    return((string)this.Value);
                }
            }
            else
            {
                if (contentToken == JsonToken.Null)
                {
                    goto IL_A1;
                }
                if (contentToken == JsonToken.EndArray)
                {
                    goto IL_A1;
                }
            }
            if (JsonTokenUtils.IsPrimitiveToken(contentToken))
            {
                object value = this.Value;
                if (value != null)
                {
                    IFormattable formattable = value as IFormattable;
                    string       text;
                    if (formattable != null)
                    {
                        text = formattable.ToString(null, this.Culture);
                    }
                    else
                    {
                        Uri uri = value as Uri;
                        text = ((uri != null) ? uri.OriginalString : value.ToString());
                    }
                    this.SetToken(JsonToken.String, text, false);
                    return(text);
                }
            }
            throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
IL_A1:
            return(null);
        }
Ejemplo n.º 7
0
        private int CalculateWriteTokenDepth(JsonReader reader)
        {
            JsonToken tokenType = reader.TokenType;

            if (tokenType == JsonToken.None)
            {
                return(-1);
            }
            if (!JsonTokenUtils.IsStartToken(tokenType))
            {
                return(reader.Depth + 1);
            }
            return(reader.Depth);
        }
Ejemplo n.º 8
0
        public virtual string ReadAsString()
        {
            string    str;
            JsonToken contentToken = this.GetContentToken();

            if (contentToken <= JsonToken.String)
            {
                if (contentToken == JsonToken.None)
                {
                    return(null);
                }
                if (contentToken == JsonToken.String)
                {
                    return((string)this.Value);
                }
                if (!JsonTokenUtils.IsPrimitiveToken(contentToken) || this.Value == null)
                {
                    throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
                }
                if (!(this.Value is IFormattable))
                {
                    str = (!(this.Value is Uri) ? this.Value.ToString() : ((Uri)this.Value).OriginalString);
                }
                else
                {
                    str = ((IFormattable)this.Value).ToString(null, this.Culture);
                }
                this.SetToken(JsonToken.String, str, false);
                return(str);
            }
            else if (contentToken != JsonToken.Null && contentToken != JsonToken.EndArray)
            {
                if (!JsonTokenUtils.IsPrimitiveToken(contentToken) || this.Value == null)
                {
                    throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
                }
                if (!(this.Value is IFormattable))
                {
                    str = (!(this.Value is Uri) ? this.Value.ToString() : ((Uri)this.Value).OriginalString);
                }
                else
                {
                    str = ((IFormattable)this.Value).ToString(null, this.Culture);
                }
                this.SetToken(JsonToken.String, str, false);
                return(str);
            }
            return(null);
        }
Ejemplo n.º 9
0
 // Token: 0x06000D89 RID: 3465 RVA: 0x00054A60 File Offset: 0x00052C60
 private void WriteToken(IList <JsonSchemaModel> schemas)
 {
     foreach (JsonValidatingReader.SchemaScope schemaScope in this._stack)
     {
         bool flag;
         if (!(flag = (schemaScope.TokenType == JTokenType.Array && schemaScope.IsUniqueArray && schemaScope.ArrayItemCount > 0)))
         {
             if (!schemas.Any((JsonSchemaModel s) => s.Enum != null))
             {
                 continue;
             }
         }
         if (schemaScope.CurrentItemWriter == null)
         {
             if (JsonTokenUtils.IsEndToken(this._reader.TokenType))
             {
                 continue;
             }
             schemaScope.CurrentItemWriter = new JTokenWriter();
         }
         schemaScope.CurrentItemWriter.WriteToken(this._reader, false);
         if (schemaScope.CurrentItemWriter.Top == 0 && this._reader.TokenType != JsonToken.PropertyName)
         {
             JToken token = schemaScope.CurrentItemWriter.Token;
             schemaScope.CurrentItemWriter = null;
             if (flag)
             {
                 if (schemaScope.UniqueArrayItems.Contains(token, JToken.EqualityComparer))
                 {
                     this.RaiseError("Non-unique array item at index {0}.".FormatWith(CultureInfo.InvariantCulture, schemaScope.ArrayItemCount - 1), schemaScope.Schemas.First((JsonSchemaModel s) => s.UniqueItems));
                 }
                 schemaScope.UniqueArrayItems.Add(token);
             }
             else if (schemas.Any((JsonSchemaModel s) => s.Enum != null))
             {
                 foreach (JsonSchemaModel jsonSchemaModel in schemas)
                 {
                     if (jsonSchemaModel.Enum != null && !jsonSchemaModel.Enum.ContainsValue(token, JToken.EqualityComparer))
                     {
                         StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                         token.WriteTo(new JsonTextWriter(stringWriter), new JsonConverter[0]);
                         this.RaiseError("Value {0} is not defined in enum.".FormatWith(CultureInfo.InvariantCulture, stringWriter.ToString()), jsonSchemaModel);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Skips the children of the current token.
        /// </summary>
        public void Skip()
        {
            if (TokenType == JsonToken.PropertyName)
            {
                Read();
            }

            if (JsonTokenUtils.IsStartToken(TokenType))
            {
                int depth = Depth;

                while (Read() && (depth < Depth))
                {
                }
            }
        }
        /// <summary>
        /// Asynchronously skips the children of the current token.
        /// </summary>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
        /// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
        /// classes can override this behaviour for true asynchronicity.</remarks>
        public async Task SkipAsync(CancellationToken cancellationToken = default)
        {
            if (TokenType == JsonToken.PropertyName)
            {
                await ReadAsync(cancellationToken).ConfigureAwait(false);
            }

            if (JsonTokenUtils.IsStartToken(TokenType))
            {
                int depth = Depth;

                while (await ReadAsync(cancellationToken).ConfigureAwait(false) && depth < Depth)
                {
                }
            }
        }
Ejemplo n.º 12
0
        public void Skip()
        {
            if (this.TokenType == JsonToken.PropertyName)
            {
                this.Read();
            }
            if (!JsonTokenUtils.IsStartToken(this.TokenType))
            {
                return;
            }
            int depth = this.Depth;

            do
            {
                ;
            }while (this.Read() && depth < this.Depth);
        }
Ejemplo n.º 13
0
        public async Task SkipAsync(CancellationToken cancellationToken = null)
        {
            ConfiguredTaskAwaitable <bool> configuredTaskAwaitable;

            if (this.TokenType == JsonToken.PropertyName)
            {
                configuredTaskAwaitable = this.ReadAsync(cancellationToken).ConfigureAwait(false);
                await configuredTaskAwaitable;
            }
            if (JsonTokenUtils.IsStartToken(this.TokenType))
            {
                int depth = this.Depth;
                do
                {
                    configuredTaskAwaitable = this.ReadAsync(cancellationToken).ConfigureAwait(false);
                }while (await configuredTaskAwaitable && depth < this.Depth);
            }
        }
Ejemplo n.º 14
0
        public virtual string ReadAsString()
        {
            string    originalString;
            JsonToken contentToken = this.GetContentToken();

            if (contentToken <= JsonToken.String)
            {
                switch (contentToken)
                {
                case JsonToken.None:
                    goto Label_0032;

                case JsonToken.String:
                    return((string)this.Value);
                }
                goto Label_0040;
            }
            if ((contentToken != JsonToken.Null) && (contentToken != JsonToken.EndArray))
            {
                goto Label_0040;
            }
            Label_0032:
            return(null);

Label_0040:
            if (!JsonTokenUtils.IsPrimitiveToken(contentToken) || (this.Value == null))
            {
                throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, contentToken));
            }
            if (this.Value is IFormattable)
            {
                originalString = ((IFormattable)this.Value).ToString(null, this.Culture);
            }
            else if (this.Value is Uri)
            {
                originalString = ((Uri)this.Value).OriginalString;
            }
            else
            {
                originalString = this.Value.ToString();
            }
            this.SetToken(JsonToken.String, originalString, false);
            return(originalString);
        }
        private object ReadValue(JsonReader reader)
        {
            if (!reader.MoveToContent())
            {
                throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
            }
            switch (reader.TokenType)
            {
            case JsonToken.StartObject:
                return(this.ReadObject(reader));

            case JsonToken.StartArray:
                return(this.ReadList(reader));
            }
            if (!JsonTokenUtils.IsPrimitiveToken(reader.TokenType))
            {
                throw JsonSerializationException.Create(reader, "Unexpected token when converting ExpandoObject: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }
            return(reader.Value);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="String"/>.
        /// </summary>
        /// <returns>A <see cref="String"/>. This method will return <c>null</c> at the end of an array.</returns>
        public virtual string ReadAsString()
        {
            JsonToken t = GetContentToken();

            switch (t)
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(null);

            case JsonToken.String:
                return((string)Value);
            }

            if (JsonTokenUtils.IsPrimitiveToken(t))
            {
                if (Value != null)
                {
                    string s;
                    if (Value is IFormattable)
                    {
                        s = ((IFormattable)Value).ToString(null, Culture);
                    }
                    else if (Value is Uri)
                    {
                        s = ((Uri)Value).OriginalString;
                    }
                    else
                    {
                        s = Value.ToString();
                    }

                    SetToken(JsonToken.String, s, false);
                    return(s);
                }
            }

            throw JsonReaderException.Create(this, "Error reading string. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Ejemplo n.º 17
0
    private object ReadValue(JsonReader reader, JsonSerializer serializer)
    {
        if (!reader.MoveToContent())
        {
            throw JsonSerializationExceptionHelper.Create(reader, "Unexpected end when reading ExpandoObject.");
        }
        switch (reader.TokenType)
        {
        case JsonToken.StartObject:
            return(ReadObject(reader, serializer));

        case JsonToken.StartArray:
            return(ReadList(reader, serializer));

        default:
            if (JsonTokenUtils.IsPrimitiveToken(reader.TokenType))
            {
                return(reader.Value);
            }
            throw JsonSerializationExceptionHelper.Create(reader, string.Format("Unexpected token when converting ExpandoObject: {0}", reader.TokenType));
        }
    }
Ejemplo n.º 18
0
        internal virtual void WriteToken(
            JsonReader reader,
            bool writeChildren,
            bool writeDateConstructorAsDate,
            bool writeComments)
        {
            int writeTokenDepth = this.CalculateWriteTokenDepth(reader);

            do
            {
                if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor &&
                    string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
                {
                    this.WriteConstructorDate(reader);
                }
                else if (writeComments || reader.TokenType != JsonToken.Comment)
                {
                    this.WriteToken(reader.TokenType, reader.Value);
                }
            } while (writeTokenDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0) &
                     writeChildren && reader.Read());
        }
Ejemplo n.º 19
0
        public virtual string ReadAsString()
        {
            JsonToken contentToken = this.GetContentToken();

            switch (contentToken)
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return((string)null);

            case JsonToken.String:
                return((string)this.Value);

            default:
                if (!JsonTokenUtils.IsPrimitiveToken(contentToken) || this.Value == null)
                {
                    throw JsonReaderException.Create(this,
                                                     "Error reading string. Unexpected token: {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture,
                                                                                                               (object)contentToken));
                }
                IFormattable formattable = this.Value as IFormattable;
                string       str;
                if (formattable != null)
                {
                    str = formattable.ToString((string)null, (IFormatProvider)this.Culture);
                }
                else
                {
                    Uri uri = this.Value as Uri;
                    str = uri != (Uri)null ? uri.OriginalString : this.Value.ToString();
                }

                this.SetToken(JsonToken.String, (object)str, false);
                return(str);
            }
        }
        private void ValidateCurrentToken()
        {
            // first time validate has been called. build model
            if (_model == null)
            {
                JsonSchemaModelBuilder builder = new JsonSchemaModelBuilder();
                _model = builder.Build(_schema);

                if (!JsonTokenUtils.IsStartToken(_reader.TokenType))
                {
                    Push(new SchemaScope(JTokenType.None, CurrentMemberSchemas));
                }
            }

            switch (_reader.TokenType)
            {
            case JsonToken.StartObject:
                ProcessValue();
                IList <JsonSchemaModel> objectSchemas = CurrentMemberSchemas.Where(ValidateObject).ToList();
                Push(new SchemaScope(JTokenType.Object, objectSchemas));
                WriteToken(CurrentSchemas);
                break;

            case JsonToken.StartArray:
                ProcessValue();
                IList <JsonSchemaModel> arraySchemas = CurrentMemberSchemas.Where(ValidateArray).ToList();
                Push(new SchemaScope(JTokenType.Array, arraySchemas));
                WriteToken(CurrentSchemas);
                break;

            case JsonToken.StartConstructor:
                ProcessValue();
                Push(new SchemaScope(JTokenType.Constructor, null));
                WriteToken(CurrentSchemas);
                break;

            case JsonToken.PropertyName:
                WriteToken(CurrentSchemas);
                foreach (JsonSchemaModel schema in CurrentSchemas)
                {
                    ValidatePropertyName(schema);
                }
                break;

            case JsonToken.Raw:
                ProcessValue();
                break;

            case JsonToken.Integer:
                ProcessValue();
                WriteToken(CurrentMemberSchemas);
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateInteger(schema);
                }
                break;

            case JsonToken.Float:
                ProcessValue();
                WriteToken(CurrentMemberSchemas);
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateFloat(schema);
                }
                break;

            case JsonToken.String:
                ProcessValue();
                WriteToken(CurrentMemberSchemas);
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateString(schema);
                }
                break;

            case JsonToken.Boolean:
                ProcessValue();
                WriteToken(CurrentMemberSchemas);
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateBoolean(schema);
                }
                break;

            case JsonToken.Null:
                ProcessValue();
                WriteToken(CurrentMemberSchemas);
                foreach (JsonSchemaModel schema in CurrentMemberSchemas)
                {
                    ValidateNull(schema);
                }
                break;

            case JsonToken.EndObject:
                WriteToken(CurrentSchemas);
                foreach (JsonSchemaModel schema in CurrentSchemas)
                {
                    ValidateEndObject(schema);
                }
                Pop();
                break;

            case JsonToken.EndArray:
                WriteToken(CurrentSchemas);
                foreach (JsonSchemaModel schema in CurrentSchemas)
                {
                    ValidateEndArray(schema);
                }
                Pop();
                break;

            case JsonToken.EndConstructor:
                WriteToken(CurrentSchemas);
                Pop();
                break;

            case JsonToken.Undefined:
            case JsonToken.Date:
            case JsonToken.Bytes:
                // these have no equivalent in JSON schema
                WriteToken(CurrentMemberSchemas);
                break;

            case JsonToken.None:
                // no content, do nothing
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Returns the <see cref="bool"/> value of the item at the specified <paramref name="index"/> in the array, or
 /// <paramref name="fallback"/> of a matching token isn't found or the token value can not be parsed to a
 /// boolean value.
 /// </summary>
 /// <param name="array">The parent array.</param>
 /// <param name="index">The index of the token.</param>
 /// <param name="fallback">The fallback value.</param>
 /// <returns>An instance of <see cref="bool"/>.</returns>
 public static bool GetBoolean(this JArray array, int index, bool fallback)
 {
     return(JsonTokenUtils.GetBoolean(array?[index], fallback));
 }
Ejemplo n.º 22
0
        private void ValidateCurrentToken()
        {
            if (this._model == null)
            {
                this._model = new JsonSchemaModelBuilder().Build(this._schema);
                if (!JsonTokenUtils.IsStartToken(this._reader.TokenType))
                {
                    this.Push(new JsonValidatingReader.SchemaScope(JTokenType.None, this.CurrentMemberSchemas));
                }
            }

            switch (this._reader.TokenType)
            {
            case JsonToken.None:
                break;

            case JsonToken.StartObject:
                this.ProcessValue();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Object,
                                                               (IList <JsonSchemaModel>) this.CurrentMemberSchemas
                                                               .Where <JsonSchemaModel>(new Func <JsonSchemaModel, bool>(this.ValidateObject)).ToList <JsonSchemaModel>()));
                this.WriteToken(this.CurrentSchemas);
                break;

            case JsonToken.StartArray:
                this.ProcessValue();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Array,
                                                               (IList <JsonSchemaModel>) this.CurrentMemberSchemas
                                                               .Where <JsonSchemaModel>(new Func <JsonSchemaModel, bool>(this.ValidateArray)).ToList <JsonSchemaModel>()));
                this.WriteToken(this.CurrentSchemas);
                break;

            case JsonToken.StartConstructor:
                this.ProcessValue();
                this.Push(new JsonValidatingReader.SchemaScope(JTokenType.Constructor, (IList <JsonSchemaModel>)null));
                this.WriteToken(this.CurrentSchemas);
                break;

            case JsonToken.PropertyName:
                this.WriteToken(this.CurrentSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentSchemas.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        this.ValidatePropertyName(enumerator.Current);
                    }
                    break;
                }

            case JsonToken.Raw:
                this.ProcessValue();
                break;

            case JsonToken.Integer:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        this.ValidateInteger(enumerator.Current);
                    }
                    break;
                }

            case JsonToken.Float:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        this.ValidateFloat(enumerator.Current);
                    }
                    break;
                }

            case JsonToken.String:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        this.ValidateString(enumerator.Current);
                    }
                    break;
                }

            case JsonToken.Boolean:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        this.ValidateBoolean(enumerator.Current);
                    }
                    break;
                }

            case JsonToken.Null:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                using (IEnumerator <JsonSchemaModel> enumerator = this.CurrentMemberSchemas.GetEnumerator()) {
                    while (enumerator.MoveNext())
                    {
                        this.ValidateNull(enumerator.Current);
                    }
                    break;
                }

            case JsonToken.Undefined:
            case JsonToken.Date:
            case JsonToken.Bytes:
                this.WriteToken(this.CurrentMemberSchemas);
                break;

            case JsonToken.EndObject:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel currentSchema in (IEnumerable <JsonSchemaModel>) this.CurrentSchemas)
                {
                    this.ValidateEndObject(currentSchema);
                }
                this.Pop();
                break;

            case JsonToken.EndArray:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel currentSchema in (IEnumerable <JsonSchemaModel>) this.CurrentSchemas)
                {
                    this.ValidateEndArray(currentSchema);
                }
                this.Pop();
                break;

            case JsonToken.EndConstructor:
                this.WriteToken(this.CurrentSchemas);
                this.Pop();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Gets the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>, or
 /// <paramref name="fallback"/> of a matching token isn't found or the token value can not be parsed to a
 /// boolean value.
 /// </summary>
 /// <param name="array">The parent array.</param>
 /// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
 /// <param name="fallback">The fallback value.</param>
 /// <returns>An instance of <see cref="bool"/>.</returns>
 public static bool GetBoolean(this JArray array, string path, bool fallback)
 {
     return(JsonTokenUtils.GetBoolean(array?.SelectToken(path), fallback));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Attempts to get a boolean value from the token with the specified <paramref name="index"/>.
 /// </summary>
 /// <param name="array">The parent array.</param>
 /// <param name="index">The index of the token.</param>
 /// <param name="result">When this method returns, if the conversion succeeded, contains the parsed boolean value. If the conversion failed, contains <c>false</c>.</param>
 /// <returns><c>true</c> if value was converted successfully; otherwise, <c>false</c>.</returns>
 public static bool TryGetBoolean(this JArray array, int index, out bool result)
 {
     return(JsonTokenUtils.TryGetBoolean(array?[index], out result));
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Attempts to get a boolean value from the token with the specified <paramref name="path"/>.
 /// </summary>
 /// <param name="array">The parent array.</param>
 /// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
 /// <param name="result">When this method returns, if the conversion succeeded, contains the parsed boolean value. If the conversion failed, contains <c>false</c>.</param>
 /// <returns><c>true</c> if value was converted successfully; otherwise, <c>false</c>.</returns>
 public static bool TryGetBoolean(this JArray array, string path, out bool result)
 {
     return(JsonTokenUtils.TryGetBoolean(array?.SelectToken(path), out result));
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Gets the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>, or
 /// <paramref name="fallback"/> if <paramref name="path"/> doesn't match a token.
 /// </summary>
 /// <param name="obj">The parent object.</param>
 /// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
 /// <param name="fallback">The fallback value.</param>
 /// <returns>An instance of <see cref="bool"/>.</returns>
 public static bool GetBoolean(this JObject obj, string path, bool fallback)
 {
     return(JsonTokenUtils.GetBoolean(obj?.SelectToken(path), fallback));
 }
Ejemplo n.º 27
0
        private void ValidateCurrentToken()
        {
            if (this._model == null)
            {
                this._model = new JsonSchemaModelBuilder().Build(this._schema);
                if (!JsonTokenUtils.IsStartToken(this._reader.TokenType))
                {
                    this.Push(new SchemaScope(JTokenType.None, this.CurrentMemberSchemas));
                }
            }
            switch (this._reader.TokenType)
            {
            case JsonToken.None:
                return;

            case JsonToken.StartObject:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas = this.CurrentMemberSchemas.Where <JsonSchemaModel>(new Func <JsonSchemaModel, bool>(this.ValidateObject)).ToList <JsonSchemaModel>();
                this.Push(new SchemaScope(JTokenType.Object, schemas));
                this.WriteToken(this.CurrentSchemas);
                return;
            }

            case JsonToken.StartArray:
            {
                this.ProcessValue();
                IList <JsonSchemaModel> schemas = this.CurrentMemberSchemas.Where <JsonSchemaModel>(new Func <JsonSchemaModel, bool>(this.ValidateArray)).ToList <JsonSchemaModel>();
                this.Push(new SchemaScope(JTokenType.Array, schemas));
                this.WriteToken(this.CurrentSchemas);
                return;
            }

            case JsonToken.StartConstructor:
                this.ProcessValue();
                this.Push(new SchemaScope(JTokenType.Constructor, null));
                this.WriteToken(this.CurrentSchemas);
                return;

            case JsonToken.PropertyName:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel model in this.CurrentSchemas)
                {
                    this.ValidatePropertyName(model);
                }
                return;

            case JsonToken.Raw:
                this.ProcessValue();
                return;

            case JsonToken.Integer:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                foreach (JsonSchemaModel model2 in this.CurrentMemberSchemas)
                {
                    this.ValidateInteger(model2);
                }
                return;

            case JsonToken.Float:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                foreach (JsonSchemaModel model3 in this.CurrentMemberSchemas)
                {
                    this.ValidateFloat(model3);
                }
                return;

            case JsonToken.String:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                foreach (JsonSchemaModel model4 in this.CurrentMemberSchemas)
                {
                    this.ValidateString(model4);
                }
                return;

            case JsonToken.Boolean:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                foreach (JsonSchemaModel model5 in this.CurrentMemberSchemas)
                {
                    this.ValidateBoolean(model5);
                }
                return;

            case JsonToken.Null:
                this.ProcessValue();
                this.WriteToken(this.CurrentMemberSchemas);
                foreach (JsonSchemaModel model6 in this.CurrentMemberSchemas)
                {
                    this.ValidateNull(model6);
                }
                return;

            case JsonToken.Undefined:
            case JsonToken.Date:
            case JsonToken.Bytes:
                this.WriteToken(this.CurrentMemberSchemas);
                return;

            case JsonToken.EndObject:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel model7 in this.CurrentSchemas)
                {
                    this.ValidateEndObject(model7);
                }
                this.Pop();
                return;

            case JsonToken.EndArray:
                this.WriteToken(this.CurrentSchemas);
                foreach (JsonSchemaModel model8 in this.CurrentSchemas)
                {
                    this.ValidateEndArray(model8);
                }
                this.Pop();
                return;

            case JsonToken.EndConstructor:
                this.WriteToken(this.CurrentSchemas);
                this.Pop();
                return;
            }
            throw new ArgumentOutOfRangeException();
        }
Ejemplo n.º 28
0
        private Dictionary <string, string> ReadAttributeElements(JsonReader reader, XmlNamespaceManager manager)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();
            bool flag  = false;
            bool flag2 = false;

            if ((((reader.TokenType != JsonToken.String) && (reader.TokenType != JsonToken.Null)) && ((reader.TokenType != JsonToken.Boolean) && (reader.TokenType != JsonToken.Integer))) && (((reader.TokenType != JsonToken.Float) && (reader.TokenType != JsonToken.Date)) && (reader.TokenType != JsonToken.StartConstructor)))
            {
                while ((!flag && !flag2) && reader.Read())
                {
                    string    str2;
                    JsonToken tokenType = reader.TokenType;
                    if (tokenType != JsonToken.PropertyName)
                    {
                        if (tokenType != JsonToken.Comment)
                        {
                            if (tokenType != JsonToken.EndObject)
                            {
                                throw JsonSerializationException.Create(reader, "Unexpected JsonToken: " + reader.TokenType);
                            }
                            goto Label_0279;
                        }
                        goto Label_0280;
                    }
                    string str = reader.Value.ToString();
                    if (string.IsNullOrEmpty(str))
                    {
                        goto Label_0272;
                    }
                    char ch = str[0];
                    if (ch != '$')
                    {
                        if (ch != '@')
                        {
                            goto Label_026B;
                        }
                        str = str.Substring(1);
                        reader.Read();
                        str2 = this.ConvertTokenToXmlValue(reader);
                        dictionary.Add(str, str2);
                        if (this.IsNamespaceAttribute(str, out string str3))
                        {
                            manager.AddNamespace(str3, str2);
                        }
                    }
                    else if (((str == "$values") || (str == "$id")) || (((str == "$ref") || (str == "$type")) || (str == "$value")))
                    {
                        string prefix = manager.LookupPrefix("http://james.newtonking.com/projects/json");
                        if (prefix == null)
                        {
                            int?nullable = null;
                            while (manager.LookupNamespace("json" + nullable) != null)
                            {
                                nullable = new int?(nullable.GetValueOrDefault() + 1);
                            }
                            prefix = "json" + nullable;
                            dictionary.Add("xmlns:" + prefix, "http://james.newtonking.com/projects/json");
                            manager.AddNamespace(prefix, "http://james.newtonking.com/projects/json");
                        }
                        if (str == "$values")
                        {
                            flag = true;
                        }
                        else
                        {
                            str = str.Substring(1);
                            reader.Read();
                            if (!JsonTokenUtils.IsPrimitiveToken(reader.TokenType))
                            {
                                throw JsonSerializationException.Create(reader, "Unexpected JsonToken: " + reader.TokenType);
                            }
                            str2 = reader.Value?.ToString();
                            dictionary.Add(prefix + ":" + str, str2);
                        }
                    }
                    else
                    {
                        flag = true;
                    }
                    continue;
Label_026B:
                    flag = true;
                    continue;
Label_0272:
                    flag = true;
                    continue;
Label_0279:
                    flag2 = true;
                    continue;
Label_0280:
                    flag2 = true;
                }
            }
            return(dictionary);
        }
Ejemplo n.º 29
0
        private Dictionary <string, string> ReadAttributeElements(
            JsonReader reader,
            XmlNamespaceManager manager)
        {
            switch (reader.TokenType)
            {
            case JsonToken.StartConstructor:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            case JsonToken.Boolean:
            case JsonToken.Null:
            case JsonToken.Date:
                return((Dictionary <string, string>)null);

            default:
                Dictionary <string, string> dictionary = (Dictionary <string, string>)null;
                bool flag = false;
                while (!flag && reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonToken.PropertyName:
                        string str1 = reader.Value.ToString();
                        if (!string.IsNullOrEmpty(str1))
                        {
                            switch (str1[0])
                            {
                            case '$':
                                if (str1 == "$values" || str1 == "$id" || (str1 == "$ref" || str1 == "$type") ||
                                    str1 == "$value")
                                {
                                    string prefix = manager.LookupPrefix("http://james.newtonking.com/projects/json");
                                    if (prefix == null)
                                    {
                                        if (dictionary == null)
                                        {
                                            dictionary = new Dictionary <string, string>();
                                        }
                                        int?nullable = new int?();
                                        while (manager.LookupNamespace("json" + (object)nullable) != null)
                                        {
                                            nullable = new int?(nullable.GetValueOrDefault() + 1);
                                        }
                                        prefix = "json" + (object)nullable;
                                        dictionary.Add("xmlns:" + prefix, "http://james.newtonking.com/projects/json");
                                        manager.AddNamespace(prefix, "http://james.newtonking.com/projects/json");
                                    }

                                    if (str1 == "$values")
                                    {
                                        flag = true;
                                        continue;
                                    }

                                    string str2 = str1.Substring(1);
                                    reader.Read();
                                    if (!JsonTokenUtils.IsPrimitiveToken(reader.TokenType))
                                    {
                                        throw JsonSerializationException.Create(reader,
                                                                                "Unexpected JsonToken: " + (object)reader.TokenType);
                                    }
                                    if (dictionary == null)
                                    {
                                        dictionary = new Dictionary <string, string>();
                                    }
                                    string str3 = reader.Value?.ToString();
                                    dictionary.Add(prefix + ":" + str2, str3);
                                    continue;
                                }

                                flag = true;
                                continue;

                            case '@':
                                if (dictionary == null)
                                {
                                    dictionary = new Dictionary <string, string>();
                                }
                                string str4 = str1.Substring(1);
                                reader.Read();
                                string xmlValue = this.ConvertTokenToXmlValue(reader);
                                dictionary.Add(str4, xmlValue);
                                string prefix1;
                                if (this.IsNamespaceAttribute(str4, out prefix1))
                                {
                                    manager.AddNamespace(prefix1, xmlValue);
                                    continue;
                                }

                                continue;

                            default:
                                flag = true;
                                continue;
                            }
                        }
                        else
                        {
                            flag = true;
                            continue;
                        }

                    case JsonToken.Comment:
                    case JsonToken.EndObject:
                        flag = true;
                        continue;

                    default:
                        throw JsonSerializationException.Create(reader, "Unexpected JsonToken: " + (object)reader.TokenType);
                    }
                }

                return(dictionary);
            }
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Gets the <see cref="bool"/> value of the token matching the specified <paramref name="path"/> and parses
 /// it into an instance of <typeparamref name="T"/>, or the default value of <typeparamref name="T"/> if
 /// <paramref name="path"/> doesn't match a token.
 /// </summary>
 /// <param name="obj">The parent object.</param>
 /// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
 /// <param name="callback">A callback function used for parsing or converting the token value.</param>
 /// <returns>An instance of <see cref="bool"/>, or <c>false</c> if <paramref name="path"/>
 /// doesn't match a token.</returns>
 public static T GetBoolean <T>(this JObject obj, string path, Func <bool, T> callback)
 {
     return(JsonTokenUtils.TryGetBoolean(obj?.SelectToken(path), out bool result) ? callback(result) : default);