Пример #1
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            if (depth == InitialDepth && JsonTokenHelpers.IsPrimitiveOrEndToken(token))
            {
                if (!GetChildren().All(IsValidPredicate))
                {
                    List<int> invalidIndexes = new List<int>();
                    int index = 0;
                    foreach (SchemaScope schemaScope in GetChildren())
                    {
                        if (!schemaScope.IsValid)
                        {
                            invalidIndexes.Add(index);
                        }

                        index++;
                    }

                    IFormattable message = $"JSON does not match all schemas from 'allOf'. Invalid schema indexes: {StringHelpers.Join(", ", invalidIndexes)}.";
                    RaiseError(message, ErrorType.AllOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                }

                return true;
            }

            return false;
        }
Пример #2
0
        protected static object SkipJsonToken(JsonReader reader, JsonToken type)
        {
            if (!reader.Read() || reader.TokenType != type)
                throw new InvalidDataException(string.Format("Invalid JSON, expected \"{0}\", but got {1}, {2}", type, reader.TokenType, reader.Value));

            return reader.Value;
        }
        protected void EnsureEnum(JsonToken token, object value)
        {
            if (Schema._enum != null && Schema._enum.Count > 0)
            {
                if (JsonTokenHelpers.IsPrimitiveOrStartToken(token))
                {
                    if (Context.TokenWriter == null)
                    {
                        Context.TokenWriter = new JTokenWriter();
                        Context.TokenWriter.WriteToken(token, value);
                    }
                }

                if (JsonTokenHelpers.IsPrimitiveOrEndToken(token))
                {
                    if (!Schema._enum.ContainsValue(Context.TokenWriter.CurrentToken, JToken.EqualityComparer))
                    {
                        StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                        Context.TokenWriter.CurrentToken.WriteTo(new JsonTextWriter(sw));

                        RaiseError($"Value {sw.ToString()} is not defined in enum.", ErrorType.Enum, Schema, value, null);
                    }
                }
            }
        }
Пример #4
0
 public void EvaluateToken(JsonToken token, object value, int depth)
 {
     if (EvaluateTokenCore(token, value, depth))
     {
         Complete = true;
     }
 }
Пример #5
0
		private JsonSerializer bareserializer = new JsonSerializer(); // full default settings, separate context

		private static void ReadExpectType(JsonReader reader, JsonToken expected)
		{
			if (!reader.Read())
				throw new InvalidOperationException();
			if (reader.TokenType != expected)
				throw new InvalidOperationException();
		}
 public void InitializeScopes(JsonToken token, List<JSchema> schemas)
 {
     foreach (JSchema schema in schemas)
     {
         SchemaScope.CreateTokenScope(token, schema, ConditionalContext, this, InitialDepth);
     }
 }
Пример #7
0
		public override bool Read()
		{
			LBTOK l = (LBTOK)r.ReadByte();
			switch (l)
			{
				case LBTOK.StartArray: t = JsonToken.StartArray; v = null; break;
				case LBTOK.EndArray: t = JsonToken.EndArray; v = null; break;
				case LBTOK.StartObject: t = JsonToken.StartObject; v = null; break;
				case LBTOK.EndObject: t = JsonToken.EndObject; v = null; break;
				case LBTOK.Null: t = JsonToken.Null; v = null; break;
				case LBTOK.False: t = JsonToken.Boolean; v = false; break;
				case LBTOK.True: t = JsonToken.Boolean; v = true; break;
				case LBTOK.Property: t = JsonToken.PropertyName; v = r.ReadString(); break;
				case LBTOK.Undefined: t = JsonToken.Undefined; v = null; break;
				case LBTOK.S8: t = JsonToken.Integer; v = r.ReadSByte(); break;
				case LBTOK.U8: t = JsonToken.Integer; v = r.ReadByte(); break;
				case LBTOK.S16: t = JsonToken.Integer; v = r.ReadInt16(); break;
				case LBTOK.U16: t = JsonToken.Integer; v = r.ReadUInt16(); break;
				case LBTOK.S32: t = JsonToken.Integer; v = r.ReadInt32(); break;
				case LBTOK.U32: t = JsonToken.Integer; v = r.ReadUInt32(); break;
				case LBTOK.S64: t = JsonToken.Integer; v = r.ReadInt64(); break;
				case LBTOK.U64: t = JsonToken.Integer; v = r.ReadUInt64(); break;
				case LBTOK.String: t = JsonToken.String; v = r.ReadString(); break;
				case LBTOK.F32: t = JsonToken.Float; v = r.ReadSingle(); break;
				case LBTOK.F64: t = JsonToken.Float; v = r.ReadDouble(); break;
				case LBTOK.ByteArray: t = JsonToken.Bytes; v = r.ReadBytes(r.ReadInt32()); break;

				default:
					throw new InvalidOperationException();
			}
			return true;
		}
Пример #8
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            if (depth == InitialDepth && JsonTokenHelpers.IsPrimitiveOrEndToken(token))
            {
                int validCount = GetChildren().Count(IsValidPredicate);

                if (validCount != 1)
                {
                    List<int> validIndexes = new List<int>();
                    int index = 0;
                    foreach (SchemaScope schemaScope in GetChildren())
                    {
                        if (schemaScope.IsValid)
                            validIndexes.Add(index);

                        index++;
                    }

                    string message = "JSON is valid against more than one schema from 'oneOf'. ";
                    if (validIndexes.Count > 0)
                        message += "Valid schema indexes: {0}.".FormatWith(CultureInfo.InvariantCulture, StringHelpers.Join(", ", validIndexes));
                    else
                        message += "No valid schemas.";

                    RaiseError(message, ErrorType.OneOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                }

                return true;
            }

            return false;
        }
Пример #9
0
        protected void AssertRead(JsonToken[] expectedToken)
        {
            Reader.Read();

            if (!expectedToken.Contains(Reader.TokenType))
                throw new Exception("Expected one of [" + String.Join(", ", expectedToken.Select(x => x.ToString())) + "] but got " + Reader.TokenType);
        }
Пример #10
0
        protected void AssertRead(JsonToken expectedToken)
        {
            Reader.Read();

            if (Reader.TokenType != expectedToken)
                throw new Exception("Expected " + expectedToken + " but got " + Reader.TokenType);
        }
Пример #11
0
        public void ValidateCurrentToken(JsonToken token, object value, int depth)
        {
            if (_scopes.Count == 0)
            {
                if (Schema == null)
                    throw new JSchemaException("No schema has been set for the validator.");

                LicenseHelpers.IncrementAndCheckValidationCount();
                SchemaScope.CreateTokenScope(token, Schema, _context, null, depth);
            }

            if (TokenWriter != null)
                TokenWriter.WriteToken(token, value);

            for (int i = _scopes.Count - 1; i >= 0; i--)
            {
                Scope scope = _scopes[i];

                if (!scope.Complete)
                    scope.EvaluateToken(token, value, depth);
                else
                    _scopes.RemoveAt(i);
            }

            if (TokenWriter != null && TokenWriter.Top == 0)
                TokenWriter = null;
        }
Пример #12
0
 protected override void WriteEnd(JsonToken token)
 {
   base.WriteEnd(token);
   this.RemoveParent();
   if (this.Top != 0)
     return;
   this._writer.WriteToken(this._root);
 }
Пример #13
0
 internal void PushBack(JsonToken token)
 {
     if (bufferedToken != null)
     {
         throw new InvalidOperationException("Can't push back twice");
     }
     bufferedToken = token;
 }
Пример #14
0
        protected static object ReadJsonProperty(JsonReader reader, string propertyname, JsonToken type)
        {
            var p = SkipJsonToken(reader, JsonToken.PropertyName);
            if (p == null || p.ToString() != propertyname)
                throw new InvalidDataException(string.Format("Invalid JSON, expected property \"{0}\", but got {1}, {2}", propertyname, reader.TokenType, reader.Value));

            return SkipJsonToken(reader, type);
        }
Пример #15
0
			public ReadState(JsonToken type, object val = null)
			{
				TokenType = type;
				if(type == JsonToken.String && val != null)
					Value = val.ToString();
				else
					Value = val;
			}
 static void Read(JsonReader jsonReader, JsonToken expected)
 {
     jsonReader.Read();
     if (jsonReader.TokenType != expected)
     {
         throw new Exception(string.Format("expected {0}", expected));
     }
 }
Пример #17
0
        public static SchemaScope CreateTokenScope(JsonToken token, JSchema schema, ContextBase context, Scope parent, int depth)
        {
            SchemaScope scope;

            switch (token)
            {
                case JsonToken.StartObject:
                    var objectScope = new ObjectScope(context, parent, depth, schema);
                    context.Scopes.Add(objectScope);

                    objectScope.InitializeScopes(token);

                    scope = objectScope;
                    break;
                case JsonToken.StartArray:
                case JsonToken.StartConstructor:
                    scope = new ArrayScope(context, parent, depth, schema);
                    context.Scopes.Add(scope);
                    break;
                default:
                    scope = new PrimativeScope(context, parent, depth, schema);
                    context.Scopes.Add(scope);
                    break;
            }

            if (schema._allOf != null && schema._allOf.Count > 0)
            {
                AllOfScope allOfScope = new AllOfScope(scope, context, depth);
                context.Scopes.Add(allOfScope);

                allOfScope.InitializeScopes(token, schema._allOf);
            }
            if (schema._anyOf != null && schema._anyOf.Count > 0)
            {
                AnyOfScope anyOfScope = new AnyOfScope(scope, context, depth);
                context.Scopes.Add(anyOfScope);

                anyOfScope.InitializeScopes(token, schema._anyOf);
            }
            if (schema._oneOf != null && schema._oneOf.Count > 0)
            {
                OneOfScope oneOfScope = new OneOfScope(scope, context, depth);
                context.Scopes.Add(oneOfScope);

                oneOfScope.InitializeScopes(token, schema._oneOf);
            }
            if (schema.Not != null)
            {
                NotScope notScope = new NotScope(scope, context, depth);
                context.Scopes.Add(notScope);

                notScope.InitializeScopes(token, Enumerable.Repeat(schema.Not, 1));
            }

            return scope;
        }
Пример #18
0
    /// <summary>
    /// Writes the end.
    /// </summary>
    /// <param name="token">The token.</param>
    protected override void WriteEnd(JsonToken token)
    {
      base.WriteEnd(token);
      RemoveParent();

      if (Top == 0)
      {
        _writer.WriteToken(_root);
      }
    }
Пример #19
0
 private static bool IsPrimitiveType(JsonToken type)
 {
     bool isPrimitive = type == JsonToken.Integer
         || type == JsonToken.Integer
         || type == JsonToken.Float
         || type == JsonToken.String
         || type == JsonToken.Boolean
         || type == JsonToken.Null;
     return isPrimitive;
 }
Пример #20
0
 internal static bool IsStartToken(JsonToken token)
 {
     switch (token)
     {
         case JsonToken.StartObject:
         case JsonToken.StartArray:
         case JsonToken.StartConstructor:
             return true;
         default:
             return false;
     }
 }
Пример #21
0
	/// <summary>
	/// Writes the specified end token.
	/// </summary>
	protected override void WriteEnd(JsonToken token)
	{
		switch (token)
		{
			case JsonToken.EndArray:
				this.isArray = false;
				this.path.Pop();
				break;
			default:
				break;
		}
	}
Пример #22
0
 internal static bool IsEndToken(JsonToken token)
 {
     switch (token)
     {
         case JsonToken.EndObject:
         case JsonToken.EndArray:
         case JsonToken.EndConstructor:
             return true;
         default:
             return false;
     }
 }
Пример #23
0
        internal virtual void SetFromJsonTextReader(string name, JsonToken token, object value)
        {
            if (name == "code" && token == JsonToken.Integer)
                this.Code = Convert.ToInt32(value);

            if (name == "error" && token == JsonToken.Boolean)
                this.Error = Convert.ToBoolean(value);

            if (name == "errorMessage" && token == JsonToken.String)
                this.ErrorMessage = value.ToString();

            if (name == "errorNum" && token == JsonToken.Integer)
                this.ErrorNum = Convert.ToInt32(value);
        }
Пример #24
0
	private void WriteValueInternal(string value, JsonToken token)
	{
		var property = string.Join(".", this.path.Reverse());

		if (!property.Equals(this.DefaultProperty, StringComparison.OrdinalIgnoreCase))
		{
			this.writer.Write(property);
			this.writer.Write(':');
		}

		this.writer.Write(value);
		if (!this.isArray)
			this.path.Pop();
	}
Пример #25
0
 private void Write(JsonToken token)
 {
     if (this._tokens == null)
     {
         this._tokens = new JsonToken[0x10];
     }
     else if (this._count == this._tokens.Length)
     {
         JsonToken[] array = new JsonToken[this._tokens.Length * 2];
         this._tokens.CopyTo(array, 0);
         this._tokens = array;
     }
     this._tokens[this._count++] = token;
 }
Пример #26
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            if (depth == InitialDepth && JsonTokenHelpers.IsPrimitiveOrEndToken(token))
            {
                if (GetChildren().Any(IsValidPredicate))
                {
                    RaiseError($"JSON is valid against schema from 'not'.", ErrorType.Not, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                }

                return true;
            }

            return false;
        }
Пример #27
0
        protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
        {
            if (depth == InitialDepth && JsonTokenHelpers.IsPrimitiveOrEndToken(token))
            {
                if (!GetChildren().Any(IsValidPredicate))
                {
                    RaiseError("JSON does not match any schemas from 'anyOf'.", ErrorType.AnyOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
                }

                return true;
            }

            return false;
        }
Пример #28
0
 public JsonBufferStorage Write(JsonToken token)
 {
     if (this._tokens == null)
     {
         this._tokens = new JsonToken[0x10];
     }
     else if (this._count == this._tokens.Length)
     {
         JsonToken[] array = new JsonToken[this._tokens.Length * 2];
         this._tokens.CopyTo(array, 0);
         this._tokens = array;
     }
     this._tokens[this._count++] = token;
     return this;
 }
Пример #29
0
 public JsonReader CreatePlayer()
 {
     if ((this.Bracket != JsonWriterBracket.Pending) && (this.Bracket != JsonWriterBracket.Closed))
     {
         throw new InvalidOperationException("JSON data cannot be read before it is complete.");
     }
     JsonToken[] destinationArray = new JsonToken[this._count + 2];
     if (this._count > 0)
     {
         Array.Copy(this._tokens, 0, destinationArray, 1, this._count);
     }
     destinationArray[0] = JsonToken.BOF();
     destinationArray[destinationArray.Length - 1] = JsonToken.EOF();
     return new JsonPlayer(destinationArray);
 }
Пример #30
0
 // TODO: Why do we allow a different token to be pushed back? It might be better to always remember the previous
 // token returned, and allow a parameterless Rewind() method (which could only be called once, just like the current PushBack).
 internal void PushBack(JsonToken token)
 {
     if (bufferedToken != null)
     {
         throw new InvalidOperationException("Can't push back twice");
     }
     bufferedToken = token;
     if (token.Type == JsonToken.TokenType.StartObject)
     {
         ObjectDepth--;
     }
     else if (token.Type == JsonToken.TokenType.EndObject)
     {
         ObjectDepth++;
     }
 }
Пример #31
0
 /// <summary>
 /// Sets the current token.
 /// </summary>
 /// <param name="newToken">The new token.</param>
 protected void SetToken(JsonToken newToken)
 {
     SetToken(newToken, null, true);
 }
Пример #32
0
        public static SchemaScope CreateTokenScope(JsonToken token, JSchema schema, ContextBase context, SchemaScope parent, int depth)
        {
            SchemaScope scope;

            switch (token)
            {
            case JsonToken.StartObject:
                ObjectScope objectScope = context.Validator.GetCachedScope <ObjectScope>(ScopeType.Object);
                if (objectScope == null)
                {
                    objectScope = new ObjectScope();
                }
                objectScope.Initialize(context, parent, depth, schema);
                context.Scopes.Add(objectScope);

                objectScope.InitializeScopes(token);

                scope = objectScope;
                break;

            case JsonToken.StartArray:
            case JsonToken.StartConstructor:
                ArrayScope arrayScope = context.Validator.GetCachedScope <ArrayScope>(ScopeType.Array);
                if (arrayScope == null)
                {
                    arrayScope = new ArrayScope();
                }
                arrayScope.Initialize(context, parent, depth, schema);

                context.Scopes.Add(arrayScope);
                scope = arrayScope;
                break;

            default:
                PrimativeScope primativeScope = context.Validator.GetCachedScope <PrimativeScope>(ScopeType.Primitive);
                if (primativeScope == null)
                {
                    primativeScope = new PrimativeScope();
                }
                primativeScope.Initialize(context, parent, depth, schema);

                scope = primativeScope;
                context.Scopes.Add(scope);
                break;
            }

            if (schema.Ref != null)
            {
                RefScope refScope = context.Validator.GetCachedScope <RefScope>(ScopeType.Ref);
                if (refScope == null)
                {
                    refScope = new RefScope();
                }
                refScope.Initialize(context, scope, depth, ScopeType.Ref);
                scope.AddChildScope(refScope);

                refScope.InitializeScopes(token, new List <JSchema> {
                    schema.Ref
                }, context.Scopes.Count - 1);
            }
            if (!schema._allOf.IsNullOrEmpty())
            {
                AllOfScope allOfScope = context.Validator.GetCachedScope <AllOfScope>(ScopeType.AllOf);
                if (allOfScope == null)
                {
                    allOfScope = new AllOfScope();
                }
                allOfScope.Initialize(context, scope, depth, ScopeType.AllOf);
                scope.AddChildScope(allOfScope);

                allOfScope.InitializeScopes(token, schema._allOf.GetInnerList(), context.Scopes.Count - 1);
            }
            if (!schema._anyOf.IsNullOrEmpty())
            {
                AnyOfScope anyOfScope = context.Validator.GetCachedScope <AnyOfScope>(ScopeType.AnyOf);
                if (anyOfScope == null)
                {
                    anyOfScope = new AnyOfScope();
                }
                anyOfScope.Initialize(context, scope, depth, ScopeType.AnyOf);
                scope.AddChildScope(anyOfScope);

                anyOfScope.InitializeScopes(token, schema._anyOf.GetInnerList(), context.Scopes.Count - 1);
            }
            if (!schema._oneOf.IsNullOrEmpty())
            {
                OneOfScope oneOfScope = context.Validator.GetCachedScope <OneOfScope>(ScopeType.OneOf);
                if (oneOfScope == null)
                {
                    oneOfScope = new OneOfScope();
                }
                oneOfScope.Initialize(context, scope, depth, ScopeType.OneOf);
                scope.AddChildScope(oneOfScope);

                oneOfScope.InitializeScopes(token, schema._oneOf.GetInnerList(), context.Scopes.Count - 1);
            }
            if (schema.Not != null)
            {
                NotScope notScope = context.Validator.GetCachedScope <NotScope>(ScopeType.Not);
                if (notScope == null)
                {
                    notScope = new NotScope();
                }
                notScope.Initialize(context, scope, depth, ScopeType.Not);
                scope.AddChildScope(notScope);

                notScope.InitializeScopes(token, new List <JSchema> {
                    schema.Not
                }, context.Scopes.Count - 1);
            }
            // only makes sense to eval if/then/else when there is an if and either a then or a else
            if (schema.If != null && (schema.Then != null || schema.Else != null))
            {
                IfThenElseScope ifThenElseScope = context.Validator.GetCachedScope <IfThenElseScope>(ScopeType.IfThenElse);
                if (ifThenElseScope == null)
                {
                    ifThenElseScope = new IfThenElseScope();
                }
                ifThenElseScope.Initialize(context, scope, depth, ScopeType.IfThenElse);
                scope.AddChildScope(ifThenElseScope);

                ifThenElseScope.If = schema.If;
                if (schema.Then != null)
                {
                    ifThenElseScope.Then        = schema.Then;
                    ifThenElseScope.ThenContext = scope.CreateConditionalContext();
                }
                if (schema.Else != null)
                {
                    ifThenElseScope.Else        = schema.Else;
                    ifThenElseScope.ElseContext = scope.CreateConditionalContext();
                }

                ifThenElseScope.InitializeScopes(token, context.Scopes.Count - 1);
            }

            return(scope);
        }
Пример #33
0
        protected void EnsureEnum(JsonToken token, object value)
        {
            bool isEnum       = !Schema._enum.IsNullOrEmpty();
            bool hasConst     = Schema.Const != null;
            bool hasValidator = !Schema._validators.IsNullOrEmpty();

            if (isEnum || hasConst || hasValidator)
            {
                if (JsonTokenHelpers.IsPrimitiveOrStartToken(token))
                {
                    if (Context.TokenWriter == null)
                    {
                        Context.TokenWriter = new JTokenWriter();
                        Context.TokenWriter.WriteToken(token, value);
                    }
                }
                else if (token == JsonToken.PropertyName)
                {
                    if (Context.TokenWriter == null)
                    {
                        Context.TokenWriter = new JTokenWriter();
                        Context.TokenWriter.WriteToken(JsonToken.String, value);
                    }
                }

                if (JsonTokenHelpers.IsPrimitiveOrEndToken(token) || token == JsonToken.PropertyName)
                {
                    JToken currentToken = Context.TokenWriter.CurrentToken;

                    if (isEnum)
                    {
                        JToken resolvedToken = (currentToken is JProperty property)
                            ? new JValue(property.Name)
                            : currentToken;

                        bool defined = JsonTokenHelpers.Contains(Schema._enum, resolvedToken);

                        if (!defined)
                        {
                            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                            currentToken.WriteTo(new JsonTextWriter(sw));

                            RaiseError($"Value {sw.ToString()} is not defined in enum.", ErrorType.Enum, Schema, value, null);
                        }
                    }

                    if (hasConst)
                    {
                        bool defined = JsonTokenHelpers.ImplicitDeepEquals(Schema.Const, currentToken);

                        if (!defined)
                        {
                            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                            currentToken.WriteTo(new JsonTextWriter(sw));

                            RaiseError($"Value {sw.ToString()} does not match const.", ErrorType.Const, Schema, value, null);
                        }
                    }

                    if (hasValidator)
                    {
                        JsonValidatorContext context = new JsonValidatorContext(this, Schema);

                        foreach (JsonValidator validator in Schema._validators)
                        {
                            validator.Validate(currentToken, context);
                        }
                    }
                }
            }
        }
Пример #34
0
 private void ConsumeToken()
 {
     _lookAheadToken = JsonToken.None;
 }
        private LdValue ReadValue(JsonReader reader, JsonToken token)
        {
            switch (token)
            {
            case JsonToken.None:
                throw new JsonSerializationException();

            case JsonToken.Null:
            case JsonToken.Undefined:
                return(LdValue.Null);

            case JsonToken.Boolean:
                return(LdValue.Of((bool)reader.Value));

            case JsonToken.Float:
            case JsonToken.Integer:
                switch (reader.Value)
                {
                case int n:
                    return(LdValue.Of(n));

                case long n:
                    return(LdValue.Of(n));

                case float n:
                    return(LdValue.Of(n));

                case double n:
                    return(LdValue.Of(n));

                default:
                    throw new JsonSerializationException();
                }

            case JsonToken.String:
                return(LdValue.Of((string)reader.Value));

            case JsonToken.StartArray:
                var ab = LdValue.BuildArray();
                while (true)
                {
                    reader.Read();
                    var nextToken = reader.TokenType;
                    if (nextToken == JsonToken.EndArray)
                    {
                        return(ab.Build());
                    }
                    ab.Add(ReadValue(reader, nextToken));
                }

            case JsonToken.StartObject:
                var ob = LdValue.BuildObject();
                while (true)
                {
                    reader.Read();
                    var nextToken = reader.TokenType;
                    if (nextToken == JsonToken.EndObject)
                    {
                        return(ob.Build());
                    }
                    if (nextToken != JsonToken.PropertyName)
                    {
                        throw new JsonSerializationException();
                    }
                    var key = reader.Value.ToString();
                    reader.Read();
                    ob.Add(key, ReadValue(reader, reader.TokenType));
                }

            default:
                // all other token types are supposed to be used only for generating output, not returned
                // by the parser
                throw new JsonSerializationException();
            }
        }
Пример #36
0
 public static bool Until(this JsonTextReader reader, JsonToken token)
 {
     return(reader.Read() && reader.TokenType != token);
 }
Пример #37
0
        public bool NextToken()
        {
            var start    = 0;
            var len      = 0;
            var quoted   = false;
            var isMember = false;

            if (!this.NextLexeme(ref start, ref len, ref quoted, ref isMember))             // end of stream
            {
                token         = JsonToken.EndOfStream;
                lazyValue.Raw = null;
                return(false);
            }

            lazyValue.ClearValue();
            lazyValue.SetBufferBounds(start, len);
            if (len == 1 && !quoted && buffer[start] == SIGNIFICANT_BEGIN_ARRAY)
            {
                token = JsonToken.BeginArray;
            }
            else if (len == 1 && !quoted && buffer[start] == SIGNIFICANT_BEGIN_OBJECT)
            {
                token = JsonToken.BeginObject;
            }
            else if (len == 1 && !quoted && buffer[start] == SIGNIFICANT_END_ARRAY)
            {
                token = JsonToken.EndOfArray;
            }
            else if (len == 1 && !quoted && buffer[start] == SIGNIFICANT_END_OBJECT)
            {
                token = JsonToken.EndOfObject;
            }
            else if (len == 4 && !quoted && this.LookupAt(buffer, start, len, "null"))
            {
                token = JsonToken.Null;
            }
            else if (len == 4 && !quoted && this.LookupAt(buffer, start, len, "true"))
            {
                token         = JsonToken.Boolean;
                lazyValue.Raw = true;
            }
            else if (len == 5 && !quoted && this.LookupAt(buffer, start, len, "false"))
            {
                token         = JsonToken.Boolean;
                lazyValue.Raw = false;
            }
            else if (quoted && LookupAt(buffer, start, 6, "/Date(") && LookupAt(buffer, start + len - 2, 2, ")/"))
            {
                var ticks = JsonUtils.StringToInt64(buffer.GetChars(), buffer.Offset + start + 6, len - 8);

                token = JsonToken.DateTime;
                var dateTime = new DateTime(ticks * 0x2710L + UNIX_EPOCH_TICKS, DateTimeKind.Utc);
                lazyValue.Raw = dateTime;
            }
            else if (!quoted && IsNumber(buffer, start, len))
            {
                token = JsonToken.Number;
                lazyValue.SetAsLazyString(false);
            }
            else
            {
                token = isMember ? JsonToken.Member : JsonToken.StringLiteral;
                lazyValue.SetAsLazyString(quoted);
            }

            buffer.FixateLater(start + len + (quoted ? 1 : 0));

            return(true);
        }
Пример #38
0
 public static void ReadAndAssert(this JsonTextReader reader, JsonToken token)
 {
     reader.Read();
     JsonTextReaderEx.AssertTokenType(reader, token);
 }
Пример #39
0
        private bool ReadNormal()
        {
            switch (CurrentState)
            {
            case State.Start:
            {
                JsonToken token = (!_readRootValueAsArray) ? JsonToken.StartObject : JsonToken.StartArray;
                BsonType  type  = (!_readRootValueAsArray) ? BsonType.Object : BsonType.Array;

                SetToken(token);
                ContainerContext newContext = new ContainerContext(type);
                PushContext(newContext);
                newContext.Length = ReadInt32();
                return(true);
            }

            case State.Complete:
            case State.Closed:
                return(false);

            case State.Property:
            {
                ReadType(_currentElementType);
                return(true);
            }

            case State.ObjectStart:
            case State.ArrayStart:
            case State.PostValue:
                ContainerContext context = _currentContext;
                if (context == null)
                {
                    return(false);
                }

                int lengthMinusEnd = context.Length - 1;

                if (context.Position < lengthMinusEnd)
                {
                    if (context.Type == BsonType.Array)
                    {
                        ReadElement();
                        ReadType(_currentElementType);
                        return(true);
                    }
                    else
                    {
                        SetToken(JsonToken.PropertyName, ReadElement());
                        return(true);
                    }
                }
                else if (context.Position == lengthMinusEnd)
                {
                    if (ReadByte() != 0)
                    {
                        throw JsonReaderException.Create(this, "Unexpected end of object byte value.");
                    }

                    PopContext();
                    if (_currentContext != null)
                    {
                        MovePosition(context.Length);
                    }

                    JsonToken endToken = (context.Type == BsonType.Object) ? JsonToken.EndObject : JsonToken.EndArray;
                    SetToken(endToken);
                    return(true);
                }
                else
                {
                    throw JsonReaderException.Create(this, "Read past end of current container context.");
                }

            case State.ConstructorStart:
                break;

            case State.Constructor:
                break;

            case State.Error:
                break;

            case State.Finished:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(false);
        }
Пример #40
0
        private static string ReadResultData(string json, string property)
        {
            //IL_0023: Unknown result type (might be due to invalid IL or missing references)
            //IL_0028: Expected O, but got Unknown
            //IL_0037: Unknown result type (might be due to invalid IL or missing references)
            //IL_003d: Invalid comparison between Unknown and I4
            //IL_0063: Unknown result type (might be due to invalid IL or missing references)
            //IL_0068: Unknown result type (might be due to invalid IL or missing references)
            //IL_006a: Unknown result type (might be due to invalid IL or missing references)
            //IL_006c: Expected I4, but got Unknown
            if (json == null || json.Trim()[0] != '{')
            {
                return(json);
            }
            StringBuilder code = new StringBuilder();

            using (StringReader textReader = new StringReader(json))
            {
                JsonTextReader reader       = new JsonTextReader(textReader);
                bool           isResultData = false;
                int            levle        = 0;
                while (reader.Read())
                {
                    if (!isResultData && (int)reader.TokenType == 4)
                    {
                        if (reader.Value.ToString() == property)
                        {
                            isResultData = true;
                        }
                    }
                    else if (isResultData)
                    {
                        JsonToken tokenType = reader.TokenType;
                        switch ((int)tokenType)
                        {
                        case 2:
                            code.Append('[');
                            continue;

                        case 1:
                            code.Append('{');
                            levle++;
                            continue;

                        case 4:
                            code.Append($"\"{reader.Value}\"=");
                            continue;

                        case 17:
                            code.Append($"\"{reader.Value}\"");
                            goto default;

                        case 9:
                        case 16:
                            code.Append($"\"{reader.Value}\"");
                            goto default;

                        case 7:
                        case 8:
                        case 10:
                            code.Append("null");
                            goto default;

                        case 11:
                            code.Append("null");
                            goto default;

                        case 13:
                            if (code.Length > 0 && code[code.Length - 1] == ',')
                            {
                                code[code.Length - 1] = '}';
                            }
                            else
                            {
                                code.Append('}');
                            }
                            levle--;
                            goto default;

                        case 14:
                            if (code.Length > 0 && code[code.Length - 1] == ',')
                            {
                                code[code.Length - 1] = ']';
                            }
                            else
                            {
                                code.Append(']');
                            }
                            goto default;

                        case 6:
                            code.Append(reader.Value);
                            goto default;

                        default:
                            if (levle == 0)
                            {
                                break;
                            }
                            code.Append(',');
                            continue;
                        }
                        break;
                    }
                }
            }
            if (code.Length > 0 && code[code.Length - 1] == ',')
            {
                code[code.Length - 1] = ' ';
            }
            return(code.ToString().Trim('\'', '"'));
        }
Пример #41
0
 protected abstract bool EvaluateTokenCore(JsonToken token, object value, int depth);
Пример #42
0
 private void WriteValueInternal(string value, JsonToken token)
 {
     _writer.Write(value);
 }
Пример #43
0
        internal void SetToken(JsonToken newToken, object value, bool updateIndex)
        {
            _tokenType = newToken;
            _value     = value;

            switch (newToken)
            {
            case JsonToken.StartObject:
                _currentState = State.ObjectStart;
                Push(JsonContainerType.Object);
                break;

            case JsonToken.StartArray:
                _currentState = State.ArrayStart;
                Push(JsonContainerType.Array);
                break;

            case JsonToken.StartConstructor:
                _currentState = State.ConstructorStart;
                Push(JsonContainerType.Constructor);
                break;

            case JsonToken.EndObject:
                ValidateEnd(JsonToken.EndObject);
                break;

            case JsonToken.EndArray:
                ValidateEnd(JsonToken.EndArray);
                break;

            case JsonToken.EndConstructor:
                ValidateEnd(JsonToken.EndConstructor);
                break;

            case JsonToken.PropertyName:
                _currentState = State.Property;

                _currentPosition.PropertyName = (string)value;
                break;

            case JsonToken.Undefined:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Boolean:
            case JsonToken.Null:
            case JsonToken.Date:
            case JsonToken.String:
            case JsonToken.Raw:
            case JsonToken.Bytes:
                if (Peek() != JsonContainerType.None)
                {
                    _currentState = State.PostValue;
                }
                else
                {
                    SetFinished();
                }

                if (updateIndex)
                {
                    UpdateScopeWithFinishedValue();
                }
                break;
            }
        }
Пример #44
0
 /// <summary>
 /// Sets the current token and value.
 /// </summary>
 /// <param name="newToken">The new token.</param>
 /// <param name="value">The value.</param>
 protected void SetToken(JsonToken newToken, object value)
 {
     SetToken(newToken, value, true);
 }
Пример #45
0
 /// <summary>
 /// Writes the end.
 /// </summary>
 /// <param name="token">The token.</param>
 protected override void WriteEnd(JsonToken token)
 {
     RemoveParent();
 }
Пример #46
0
        private void ProcessSymbol()
        {
            if (current_symbol == '[')
            {
                token         = JsonToken.ArrayStart;
                parser_return = true;
            }
            else if (current_symbol == ']')
            {
                token         = JsonToken.ArrayEnd;
                parser_return = true;
            }
            else if (current_symbol == '{')
            {
                token         = JsonToken.ObjectStart;
                parser_return = true;
            }
            else if (current_symbol == '}')
            {
                token         = JsonToken.ObjectEnd;
                parser_return = true;
            }
            else if (current_symbol == '"')
            {
                if (parser_in_string)
                {
                    parser_in_string = false;

                    parser_return = true;
                }
                else
                {
                    if (token == JsonToken.None)
                    {
                        token = JsonToken.String;
                    }

                    parser_in_string = true;
                }
            }
            else if (current_symbol == (int)ParserToken.CharSeq)
            {
                token_value = lexer.StringValue;
            }
            else if (current_symbol == (int)ParserToken.False)
            {
                token         = JsonToken.Boolean;
                token_value   = false;
                parser_return = true;
            }
            else if (current_symbol == (int)ParserToken.Null)
            {
                token         = JsonToken.Null;
                parser_return = true;
            }
            else if (current_symbol == (int)ParserToken.Number)
            {
                ProcessNumber(lexer.StringValue);

                parser_return = true;
            }
            else if (current_symbol == (int)ParserToken.Pair)
            {
                token = JsonToken.PropertyName;
            }
            else if (current_symbol == (int)ParserToken.True)
            {
                token         = JsonToken.Boolean;
                token_value   = true;
                parser_return = true;
            }
        }
Пример #47
0
        ReadAsSmallDec()
        {
            JsonToken t = GetContentToken();

#if (JSON_SmallDecSupport)
            switch (t)
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                return(SmallDec.NaN);

            case JsonToken.String:
                return(Value.ToString());

            case JsonToken.SmallDec:
                if (Value.GetType() == typeof(SmallDec))
                {
                    return((SmallDec)Value);
                }
                else
                {
                    dynamic changedObj = Convert.ChangeType(Value, Value.GetType(), CultureInfo.CurrentCulture);
                    return((SmallDec)changedObj);
                }

            case JsonToken.Integer:
                return((long)Value);

            case JsonToken.Float:
                return((decimal)Value);

            case JsonToken.Boolean:
                return((SmallDec)(bool)Value);
            }
#else
            Type SmallDecType = Type.GetType("SmallDec", true);
            //dynamic changedObj = Convert.ChangeType(Value, SmallDecType);
            switch (t)
            {
            case JsonToken.None:
            case JsonToken.Null:
            case JsonToken.EndArray:
                dynamic changedObj = Convert.ChangeType(Value, SmallDecType);
                return(changedObj.NaN);

            case JsonToken.String:
                return(Convert.ChangeType(ToString));

            case JsonToken.SmallDec:
                return(Convert.ChangeType(Value, SmallDecType));
            }
#endif
            if (JsonTokenUtils.IsPrimitiveToken(t))
            {
                if (Value != null)
                {
                    string       s;
                    IFormattable formattable = Value as IFormattable;
                    if (formattable != null)
                    {
                        s = formattable.ToString(null, Culture);
                    }
                    else
                    {
                        Uri uri = Value as Uri;
                        s = uri != null ? uri.OriginalString : Value.ToString();
                    }

                    SetToken(JsonToken.SmallDec, (SmallDec)s, false);
                    return(s);
                }
            }
            throw JsonReaderException.Create(this, "Error reading SmallDec. Unexpected token: {0}.".ToString());
        }
Пример #48
0
        public bool Read()
        {
            if (end_of_input)
            {
                return(false);
            }

            if (end_of_json)
            {
                end_of_json = false;
                automaton_stack.Clear();
                automaton_stack.Push((int)ParserToken.End);
                automaton_stack.Push((int)ParserToken.Text);
            }

            parser_in_string = false;
            parser_return    = false;

            token       = JsonToken.None;
            token_value = null;

            if (!read_started)
            {
                read_started = true;

                if (!ReadToken())
                {
                    return(false);
                }
            }


            int[] entry_symbols;

            while (true)
            {
                if (parser_return)
                {
                    if (automaton_stack.Peek() == (int)ParserToken.End)
                    {
                        end_of_json = true;
                    }

                    return(true);
                }

                current_symbol = automaton_stack.Pop();

                ProcessSymbol();

                if (current_symbol == current_input)
                {
                    if (!ReadToken())
                    {
                        if (automaton_stack.Peek() != (int)ParserToken.End)
                        {
                            throw new JsonException(
                                      "Input doesn't evaluate to proper JSON text");
                        }

                        if (parser_return)
                        {
                            return(true);
                        }

                        return(false);
                    }

                    continue;
                }

                try {
                    entry_symbols =
                        parse_table[current_symbol][current_input];
                } catch (KeyNotFoundException e) {
                    throw new JsonException((ParserToken)current_input, e);
                }

                if (entry_symbols[0] == (int)ParserToken.Epsilon)
                {
                    continue;
                }

                for (int i = entry_symbols.Length - 1; i >= 0; i--)
                {
                    automaton_stack.Push(entry_symbols[i]);
                }
            }
        }
Пример #49
0
 public static bool IsMatchNext(this JsonTextReader reader, JsonToken token)
 {
     return(reader.Read() && reader.TokenType == token);
 }
Пример #50
0
 protected override void WriteEnd(JsonToken token)
 {
     base.WriteEnd(token);
     terseFormatting = false;
 }
Пример #51
0
        void ISearchResult <TDocument> .Execute(IList <ISearchParameter> searchParameters, JsonToken currentToken, string currentPath, JsonReader jsonReader)
        {
            if (!currentPath.StartsWith("facet_counts."))
            {
                return;
            }

            this.Data = this.Data ?? new List <IFacetItem>();

            var facetItems = (List <IFacetItem>)((IFacetsResult <TDocument>) this).Data;

            if (jsonReader.Path.StartsWith("facet_counts.facet_fields."))
            {
                facetItems.Add(ProcessFacetFields(jsonReader));
            }
            if (jsonReader.Path.StartsWith("facet_counts.facet_queries"))
            {
                facetItems.AddRange(ProcessFacetQueries(jsonReader));
            }
            if (jsonReader.Path.StartsWith("facet_counts.facet_ranges."))
            {
                facetItems.Add(ProcessFacetRanges(searchParameters, jsonReader));
            }
        }
Пример #52
0
        /// <summary>
        /// Convert from JSON string to Object graph of specific Type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>

        /*public T Deserialize<T>(int start)
         * {
         *      this.index = start;
         *
         *      // should this run through a preliminary test here?
         *      return (T)this.Read(typeof(T), false);
         * }*/

        public object Read(Type expectedType, bool typeIsHint)
        {
            if (expectedType == typeof(Object))
            {
                expectedType = null;
            }

            JsonToken token = this.Tokenize();

            if (expectedType != null && !expectedType.IsPrimitive)
            {
                JsonConverter converter = this.Settings.GetConverter(expectedType);
                if (converter != null)
                {
                    object val;
                    try {
                        val = Read(typeof(Dictionary <string, object>), false);
                        Dictionary <string, object> dict = val as Dictionary <string, object>;
                        if (dict == null)
                        {
                            return(null);
                        }
                        object obj = converter.Read(this, expectedType, dict);
                        return(obj);
                    } catch (JsonTypeCoercionException e) {
                        Console.WriteLine("Could not cast to dictionary for converter processing. Ignoring field.\n" + e);
                    }
                    return(null);
                }
            }

            switch (token)
            {
            case JsonToken.ObjectStart:
            {
                return(this.ReadObject(typeIsHint ? null : expectedType));
            }

            case JsonToken.ArrayStart:
            {
                return(this.ReadArray(typeIsHint ? null : expectedType));
            }

            case JsonToken.String:
            {
                return(this.ReadString(typeIsHint ? null : expectedType));
            }

            case JsonToken.Number:
            {
                return(this.ReadNumber(typeIsHint ? null : expectedType));
            }

            case JsonToken.False:
            {
                this.index += JsonReader.LiteralFalse.Length;
                return(false);
            }

            case JsonToken.True:
            {
                this.index += JsonReader.LiteralTrue.Length;
                return(true);
            }

            case JsonToken.Null:
            {
                this.index += JsonReader.LiteralNull.Length;
                return(null);
            }

            case JsonToken.NaN:
            {
                this.index += JsonReader.LiteralNotANumber.Length;
                return(Double.NaN);
            }

            case JsonToken.PositiveInfinity:
            {
                this.index += JsonReader.LiteralPositiveInfinity.Length;
                return(Double.PositiveInfinity);
            }

            case JsonToken.NegativeInfinity:
            {
                this.index += JsonReader.LiteralNegativeInfinity.Length;
                return(Double.NegativeInfinity);
            }

            case JsonToken.Undefined:
            {
                this.index += JsonReader.LiteralUndefined.Length;
                return(null);
            }

            case JsonToken.End:
            default:
            {
                return(null);
            }
            }
        }
Пример #53
0
 protected DictionaryMapperBase(
     IJsonSerializator <TKey> keySerializator, IJsonSerializator <TValue> valueSerializator,
     char startLiteral, char endLiteral, JsonToken startToken, JsonToken emptyToken, JsonToken endToken)
 {
     _keyMapper    = keySerializator.Mapper;
     _valueMapper  = valueSerializator.Mapper;
     _startLiteral = startLiteral;
     _emptyToken   = emptyToken;
     _endLiteral   = endLiteral;
     _startToken   = startToken;
     _endToken     = endToken;
 }
Пример #54
0
        /// <summary>
        /// Reads the next JSON token from the stream as a <see cref="Byte"/>[].
        /// </summary>
        /// <returns>A <see cref="Byte"/>[] or a null reference if the next JSON token is null. This method will return <c>null</c> at the end of an array.</returns>
        public virtual byte[] ReadAsBytes()
        {
            JsonToken t = GetContentToken();

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

            if (TokenType == JsonToken.StartObject)
            {
                ReadIntoWrappedTypeObject();

                byte[] data = ReadAsBytes();
                ReaderReadAndAssert();

                if (TokenType != JsonToken.EndObject)
                {
                    throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
                }

                SetToken(JsonToken.Bytes, data, false);
                return(data);
            }

            switch (t)
            {
            case JsonToken.String:
            {
                // attempt to convert possible base 64 or GUID string to bytes
                // GUID has to have format 00000000-0000-0000-0000-000000000000
                string s = (string)Value;

                byte[] data;

                Guid g;
                if (s.Length == 0)
                {
                    data = new byte[0];
                }
                else if (ConvertUtils.TryConvertGuid(s, out g))
                {
                    data = g.ToByteArray();
                }
                else
                {
                    data = Convert.FromBase64String(s);
                }

                SetToken(JsonToken.Bytes, data, false);
                return(data);
            }

            case JsonToken.Null:
            case JsonToken.EndArray:
                return(null);

            case JsonToken.Bytes:
                if (ValueType == typeof(Guid))
                {
                    byte[] data = ((Guid)Value).ToByteArray();
                    SetToken(JsonToken.Bytes, data, false);
                    return(data);
                }

                return((byte[])Value);

            case JsonToken.StartArray:
                return(ReadArrayIntoByteArray());
            }

            throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, t));
        }
Пример #55
0
 /// <summary>
 /// Writes the specified end token.
 /// </summary>
 /// <param name="token">The end token to write.</param>
 protected virtual void WriteEnd(JsonToken token)
 {
 }
Пример #56
0
        public Playlist Unmarshall(JsonUnmarshallerContext context)
        {
            if (context.CurrentTokenType == JsonToken.Null)
            {
                return(null);
            }

            Playlist playlist = new Playlist();



            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            while (context.Read())
            {
                if (context.TestExpression("Name", targetDepth))
                {
                    context.Read();
                    playlist.Name = StringUnmarshaller.GetInstance().Unmarshall(context);
                    continue;
                }

                if (context.TestExpression("Format", targetDepth))
                {
                    context.Read();
                    playlist.Format = StringUnmarshaller.GetInstance().Unmarshall(context);
                    continue;
                }

                if (context.TestExpression("OutputKeys", targetDepth))
                {
                    context.Read();

                    if (context.CurrentTokenType == JsonToken.Null)
                    {
                        playlist.OutputKeys = null;
                        continue;
                    }
                    playlist.OutputKeys = new List <String>();
                    StringUnmarshaller unmarshaller = StringUnmarshaller.GetInstance();
                    while (context.Read())
                    {
                        JsonToken token = context.CurrentTokenType;
                        if (token == JsonToken.ArrayStart)
                        {
                            continue;
                        }
                        if (token == JsonToken.ArrayEnd)
                        {
                            break;
                        }
                        playlist.OutputKeys.Add(unmarshaller.Unmarshall(context));
                    }
                    continue;
                }

                if (context.TestExpression("Status", targetDepth))
                {
                    context.Read();
                    playlist.Status = StringUnmarshaller.GetInstance().Unmarshall(context);
                    continue;
                }

                if (context.TestExpression("StatusDetail", targetDepth))
                {
                    context.Read();
                    playlist.StatusDetail = StringUnmarshaller.GetInstance().Unmarshall(context);
                    continue;
                }

                if (context.CurrentDepth <= originalDepth)
                {
                    return(playlist);
                }
            }


            return(playlist);
        }
Пример #57
0
 protected SchemaScope CreateScopesAndEvaluateToken(JsonToken token, object value, int depth, JSchema schema)
 {
     return(CreateScopesAndEvaluateToken(token, value, depth, schema, this, Context));
 }
Пример #58
0
 private void AddValue(object value, JsonToken token)
 {
     AddValue(new JValue(value), token);
 }
Пример #59
0
 /// <summary>
 /// Changes the <see cref="State"/> to Closed.
 /// </summary>
 public virtual void Close()
 {
     _currentState = State.Closed;
     _tokenType    = JsonToken.None;
     _value        = null;
 }
Пример #60
0
        private static bool TryExtractProperty(VowpalWabbitJsonParseState state, string property, string expectedProperty, JsonToken expectedToken, Action <JsonReader> success)
        {
            if (property.Equals(expectedProperty, StringComparison.OrdinalIgnoreCase))
            {
                if (!state.Reader.Read() && state.Reader.TokenType != expectedToken)
                {
                    throw new VowpalWabbitJsonException(state.Reader, $"Property '{expectedProperty}' must be of type '{expectedToken}'");
                }

                success(state.Reader);
                return(true);
            }

            return(false);
        }