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);
            }

            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);
                    _scopesCache.Add(scope);
                }
            }

            if (TokenWriter != null && TokenWriter.Top == 0)
            {
                TokenWriter = null;
            }
        }
Example #2
0
        private JToken CreateJObject(JsonReader reader)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

            // this is needed because we've already read inside the object, looking for special properties
            JToken token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                writer.WriteStartObject();

                if (reader.TokenType == JsonToken.PropertyName)
                {
                    writer.WriteToken(reader, reader.Depth - 1);
                }
                else
                {
                    writer.WriteEndObject();
                }

                token = writer.Token;
            }

            return(token);
        }
Example #3
0
        private JToken CreateJObject(JsonReader reader)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

//          throw new Exception("Expected current token of type {0}, got {1}.".FormatWith(CultureInfo.InvariantCulture, JsonToken.PropertyName, reader.TokenType));

            JToken token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                writer.WriteStartObject();

                if (reader.TokenType == JsonToken.PropertyName)
                {
                    writer.WriteToken(reader, reader.Depth - 1);
                }
                else
                {
                    writer.WriteEndObject();
                }

                token = writer.Token;
            }

            return(token);
        }
Example #4
0
        public void AddObject(string pKey, object pData)
        {
            writer.WritePropertyName(pKey);
            var r = new JTokenReader(Newtonsoft.Json.Linq.JToken.FromObject(pData));

            writer.WriteToken(r);
        }
Example #5
0
        public void WriteTokenWithParent()
        {
            JObject o = new JObject
            {
                ["prop1"] = new JArray(1),
                ["prop2"] = 1
            };

            JTokenWriter writer = new JTokenWriter();

            writer.WriteStartArray();

            writer.WriteToken(o.CreateReader());

            Assert.AreEqual(WriteState.Array, writer.WriteState);

            writer.WriteEndArray();

            Console.WriteLine(writer.Token.ToString());

            StringAssert.AreEqual(@"[
  {
    ""prop1"": [
      1
    ],
    ""prop2"": 1
  }
]", writer.Token.ToString());
        }
Example #6
0
 private JToken CreateJToken(JsonReader reader, JsonContract contract)
 {
     ValidationUtils.ArgumentNotNull(reader, "reader");
     if (contract != null && contract.UnderlyingType == typeof(JRaw))
     {
         return(JRaw.Create(reader));
     }
     using (JTokenWriter jTokenWriter = new JTokenWriter())
     {
         jTokenWriter.WriteToken(reader);
         return(jTokenWriter.Token);
     }
 }
Example #7
0
        private JToken CreateJToken(JsonReader reader)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

            JToken token;

            using (JTokenWriter writer = new JTokenWriter())
            {
                writer.WriteToken(reader);
                token = writer.Token;
            }

            return(token);
        }
Example #8
0
        public void WriteTokenDirect()
        {
            JToken token;

            using (JTokenWriter jsonWriter = new JTokenWriter())
            {
                jsonWriter.WriteToken(JsonToken.StartArray);
                jsonWriter.WriteToken(JsonToken.Integer, 1);
                jsonWriter.WriteToken(JsonToken.StartObject);
                jsonWriter.WriteToken(JsonToken.PropertyName, "integer");
                jsonWriter.WriteToken(JsonToken.Integer, int.MaxValue);
                jsonWriter.WriteToken(JsonToken.PropertyName, "null-string");
                jsonWriter.WriteToken(JsonToken.String, null);
                jsonWriter.WriteToken(JsonToken.EndObject);
                jsonWriter.WriteToken(JsonToken.EndArray);

                token = jsonWriter.Token;
            }

            Assert.AreEqual(@"[1,{""integer"":2147483647,""null-string"":null}]", token.ToString(Formatting.None));
        }
Example #9
0
        public static JArray ForceToArray(this JToken token)
        {
            if (token is JArray)
            {
                return((JArray)token);
            }
            if (token is JObject)
            {
                var writer = new JTokenWriter();
                writer.WriteStartArray();
                writer.WriteToken(new JTokenReader(token));
                writer.WriteEndArray();
                return((JArray)writer.Token);
            }

            throw new ArgumentException("token should be either JArray or JObject", nameof(token));
        }
Example #10
0
 private JToken CreateJObject(JsonReader reader)
 {
     ValidationUtils.ArgumentNotNull(reader, "reader");
     using (JTokenWriter jTokenWriter = new JTokenWriter())
     {
         jTokenWriter.WriteStartObject();
         if (reader.TokenType == JsonToken.PropertyName)
         {
             jTokenWriter.WriteToken(reader, reader.Depth - 1);
         }
         else
         {
             jTokenWriter.WriteEndObject();
         }
         return(jTokenWriter.Token);
     }
 }
Example #11
0
        public void ValidateCurrentToken(JsonToken token, object value, int depth)
        {
            if (depth == 0)
            {
                // Handle validating multiple content
                RemoveCompletedScopes();
            }

            if (_scopes.Count == 0)
            {
                if (Schema == null)
                {
                    throw new JSchemaException("No schema has been set for the validator.");
                }

                if (!_hasValidatedLicense)
                {
                    LicenseHelpers.IncrementAndCheckValidationCount();
                    _hasValidatedLicense = true;
                }

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

            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);
                    _scopesCache.Add(scope);
                }
            }

            if (TokenWriter != null && (TokenWriter.WriteState == WriteState.Start || TokenWriter.WriteState == WriteState.Closed))
            {
                TokenWriter = null;
            }
        }
Example #12
0
        public void WriteValueTokenWithParent()
        {
            JValue v = new JValue(1);

            JTokenWriter writer = new JTokenWriter();

            writer.WriteStartArray();

            writer.WriteToken(v.CreateReader());

            Assert.AreEqual(WriteState.Array, writer.WriteState);

            writer.WriteEndArray();

            StringAssert.AreEqual(@"[
  1
]", writer.Token.ToString());
        }
Example #13
0
        public void WriteTokenWithPropertyParent()
        {
            JValue v = new JValue(1);

            JTokenWriter writer = new JTokenWriter();

            writer.WriteStartObject();
            writer.WritePropertyName("Prop1");

            writer.WriteToken(v.CreateReader());

            Assert.AreEqual(WriteState.Object, writer.WriteState);

            writer.WriteEndObject();

            StringAssert.AreEqual(@"{
  ""Prop1"": 1
}", writer.Token.ToString());
        }
Example #14
0
        public void WriteEmptyToken()
        {
            JObject    o      = new JObject();
            JsonReader reader = o.CreateReader();

            while (reader.Read())
            {
            }

            JTokenWriter writer = new JTokenWriter();

            writer.WriteStartArray();

            writer.WriteToken(reader);

            Assert.AreEqual(WriteState.Array, writer.WriteState);

            writer.WriteEndArray();

            StringAssert.AreEqual(@"[]", writer.Token.ToString());
        }
        private JToken CreateJObject(JsonReader reader)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

            // this is needed because we've already read inside the object, looking for metadata properties
            using (JTokenWriter writer = new JTokenWriter())
            {
                writer.WriteStartObject();

                do
                {
                    if (reader.TokenType == JsonToken.PropertyName)
                    {
                        string propertyName = (string)reader.Value;
                        do
                        {
                            if (!reader.Read())
                                break;
                        } while (reader.TokenType == JsonToken.Comment);

                        if (CheckPropertyName(reader, propertyName))
                            continue;

                        writer.WritePropertyName(propertyName);
                        writer.WriteToken(reader, true, true);
                    }
                    else if (reader.TokenType == JsonToken.Comment)
                    {
                        // eat
                    }
                    else
                    {
                        writer.WriteEndObject();
                        return writer.Token;
                    }
                } while (reader.Read());

                throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
            }
        }
        private JToken CreateJToken(JsonReader reader, JsonContract contract)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");

            if (contract != null && contract.UnderlyingType == typeof(JRaw))
            {
                return JRaw.Create(reader);
            }
            else
            {
                JToken token;
                using (JTokenWriter writer = new JTokenWriter())
                {
                    writer.WriteToken(reader);
                    token = writer.Token;
                }

                return token;
            }
        }
Example #17
0
        public IActionResult ImportProjectAjax(string method)
        {
            var result  = false;
            var message = "";
            var array   = new JTokenWriter();

            array.WriteStartArray();

            try
            {
                switch (method)
                {
                case "gitlab":
                    var gitlabImportRequest = new GitlabImportRequest(Request);
                    if (string.IsNullOrWhiteSpace(gitlabImportRequest.GroupName))
                    {
                        throw new ImportRequestException("Missing group name");
                    }
                    if (string.IsNullOrWhiteSpace(gitlabImportRequest.PrivateToken))
                    {
                        throw new ImportRequestException("Missing private token");
                    }

                    var client = new RestClient("https://gitlab.com/api/v4/");

                    client.DefaultParameters.Add(new Parameter("private_token", gitlabImportRequest.PrivateToken, ParameterType.QueryString));

                    int       itemsCount = -1;
                    int       page       = 1;
                    const int perPage    = 100;
                    int       itemsAdded = 0;
                    do
                    {
                        var request = new RestRequest("groups/{username}/projects", Method.GET, DataFormat.Json);

                        request.Parameters.Add(new Parameter("username", gitlabImportRequest.GroupName, ParameterType.UrlSegment));
                        request.Parameters.Add(new Parameter("per_page", perPage, ParameterType.QueryString));
                        request.Parameters.Add(new Parameter("page", page++, ParameterType.QueryString));

                        var response = client.Execute(request);

                        var json = response.Content;

                        var projects = (JToken)JsonConvert.DeserializeObject(json);

                        if (!(projects is JArray))
                        {
                            throw new ImportRequestException("Unexpected response from server");
                        }

                        var projectsArray = (JArray)projects;

                        foreach (var project in projectsArray)
                        {
                            array.WriteStartObject();

                            array.WritePropertyName("name");
                            var name = project.Value <string>("name");
                            array.WriteValue(name);

                            array.WritePropertyName("url");
                            var url = project.Value <string>("http_url_to_repo");
                            array.WriteValue(url);

                            array.WritePropertyName("added");
                            array.WriteValue(mainService.ProjectExists(name, url));

                            array.WriteEndObject();

                            itemsAdded++;
                        }
                    } while (itemsCount == perPage);

                    message = $"{itemsAdded} projects fetched";
                    result  = true;
                    break;

                default:
                    throw new ImportRequestException("Unknown provider: " + method);
                }
            }
            catch (ImportRequestException ex)
            {
                result  = false;
                message = ex.Message;
            }
            catch (Exception ex)
            {
                result  = false;
                message = "Unexpected exception: " + ex.Message;
            }

            var writer = new JTokenWriter();

            array.WriteEndArray();

            writer.WriteStartObject();

            writer.WritePropertyName("status");
            writer.WriteValue(result ? "OK" : "ERROR");

            writer.WritePropertyName("message");
            writer.WriteValue(message);

            writer.WritePropertyName("projects");
            writer.WriteToken(new JTokenReader(array.Token));

            writer.WriteEndObject();

            var content = writer.Token.ToString(Program.Debug ? Formatting.Indented : Formatting.None);

            return(Content(content, "text/javascript"));
        }