コード例 #1
0
        protected ValidationError CreateError(string message, ErrorType errorType, JSchema schema, object value, IList<ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            if (_schemaDiscovery == null)
            {
                _schemaDiscovery = new JSchemaDiscovery();
                _schemaDiscovery.Discover(Schema, null);
            }

            ValidationError error = new ValidationError();
            error.Message = message;
            error.ErrorType = errorType;
            error.Path = path;
            if (lineInfo != null)
            {
                error.LineNumber = lineInfo.LineNumber;
                error.LinePosition = lineInfo.LinePosition;
            }
            error.Schema = schema;
            error.SchemaId = _schemaDiscovery.KnownSchemas.Single(s => s.Schema == schema).Id;
            error.SchemaBaseUri = schema.BaseUri;
            error.Value = value;
            error.ChildErrors = childErrors;

            return error;
        }
コード例 #2
0
        internal static string FormatMessage(IJsonLineInfo lineInfo, string path, string message)
        {
            if (!message.EndsWith(Environment.NewLine, StringComparison.Ordinal))
            {
                message = message.Trim();

                if (!message.EndsWith('.'))
                    message += ".";
            }

            if (path == null && !lineInfo.HasLineInfo())
                return message;

            message += " ";

            if (path != null)
            {
                message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);

                if (lineInfo != null && lineInfo.HasLineInfo())
                    message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);
            }
            else
            {
                message += "Line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);
            }

            message += ".";
            return message;
        }
コード例 #3
0
        private PackageSpecFormatException WithLineInfo(IJsonLineInfo lineInfo)
        {
            Line = lineInfo.LineNumber;
            Column = lineInfo.LinePosition;

            return this;
        }
コード例 #4
0
ファイル: FileFormatException.cs プロジェクト: elanwu123/dnx
        private FileFormatException WithLineInfo(IJsonLineInfo lineInfo)
        {
            if (lineInfo != null)
            {
                Line = lineInfo.LineNumber;
                Column = lineInfo.LinePosition;
            }

            return this;
        }
コード例 #5
0
        protected ValidationError CreateError(string message, ErrorType errorType, JSchema schema, object value, IList<ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            if (_schemaDiscovery == null)
            {
                _schemaDiscovery = new JSchemaDiscovery();
                _schemaDiscovery.Discover(Schema, null);
            }

            Uri schemaId = _schemaDiscovery.KnownSchemas.Single(s => s.Schema == schema).Id;

            ValidationError error = ValidationError.CreateValidationError(message, errorType, schema, schemaId, value, childErrors, lineInfo, path);

            return error;
        }
コード例 #6
0
ファイル: JsonException.cs プロジェクト: Zeludon/FEZ
 internal static string FormatExceptionMessage(IJsonLineInfo lineInfo, string path, string message)
 {
   if (!message.EndsWith(Environment.NewLine))
   {
     message = message.Trim();
     if (!message.EndsWith("."))
       message = message + ".";
     message = message + " ";
   }
   message = message + StringUtils.FormatWith("Path '{0}'", (IFormatProvider) CultureInfo.InvariantCulture, (object) path);
   if (lineInfo != null && lineInfo.HasLineInfo())
     message = message + StringUtils.FormatWith(", line {0}, position {1}", (IFormatProvider) CultureInfo.InvariantCulture, (object) lineInfo.LineNumber, (object) lineInfo.LinePosition);
   message = message + ".";
   return message;
 }
コード例 #7
0
    internal static string FormatExceptionMessage(IJsonLineInfo lineInfo, string path, string message)
    {
      // don't add a fullstop and space when message ends with a new line
      if (!message.EndsWith(Environment.NewLine))
      {
        message = message.Trim();

        if (!message.EndsWith("."))
          message += ".";

        message += " ";
      }

      message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);

      if (lineInfo != null && lineInfo.HasLineInfo())
        message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);

      message += ".";

      return message;
    }
コード例 #8
0
        public void YahooFinance()
        {
            JObject o =
                new JObject(
                    new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)),
                    new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))),
                    new JProperty("Test3", "Test3Value"),
                    new JProperty("Test4", null)
                    );

            using (JTokenReader jsonReader = new JTokenReader(o))
            {
                IJsonLineInfo lineInfo = jsonReader;

                jsonReader.Read();
                Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test1", jsonReader.Value);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
                Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), jsonReader.Value);
                Assert.AreEqual(false, lineInfo.HasLineInfo());
                Assert.AreEqual(0, lineInfo.LinePosition);
                Assert.AreEqual(0, lineInfo.LineNumber);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test2", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
                Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test3", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.String, jsonReader.TokenType);
                Assert.AreEqual("Test3Value", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test4", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Null, jsonReader.TokenType);
                Assert.AreEqual(null, jsonReader.Value);

                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);

                Assert.IsFalse(jsonReader.Read());
                Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
            }

            using (JsonReader jsonReader = new JTokenReader(o.Property("Test2")))
            {
                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test2", jsonReader.Value);

                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
                Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);

                Assert.IsFalse(jsonReader.Read());
                Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
            }
        }
コード例 #9
0
ファイル: JContainer.cs プロジェクト: djdeathgirl/Jabbo
        internal void ReadContentFrom(JsonReader r)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo = r as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if (parent is JProperty && ((JProperty)parent).Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(r.Value.ToString());
                    constructor.SetLineInfo(constructor);
                    parent.AddObjectSkipNotify(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                    JValue v = new JValue(r.Value);
                    v.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(v);
                    break;

                case JsonToken.Comment:
                    v = JValue.CreateComment(r.Value.ToString());
                    v.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(v);
                    break;

                case JsonToken.Null:
                    v = new JValue(null, JsonTokenType.Null);
                    v.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(v);
                    break;

                case JsonToken.Undefined:
                    v = new JValue(null, JsonTokenType.Undefined);
                    v.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(v);
                    break;

                case JsonToken.PropertyName:
                    JProperty property = new JProperty(r.Value.ToString());
                    property.SetLineInfo(lineInfo);
                    parent.AddObjectSkipNotify(property);
                    parent = property;
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            }while (r.Read());
        }
コード例 #10
0
        internal void ReadContentFrom(JsonReader r)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo   = r as IJsonLineInfo;
            JContainer    jContainer = this;

            do
            {
                if (jContainer is JProperty && ((JProperty)jContainer).Value != null)
                {
                    if (jContainer == this)
                    {
                        break;
                    }
                    jContainer = jContainer.Parent;
                }
                switch (r.TokenType)
                {
                case JsonToken.StartArray:
                {
                    JArray jArray = new JArray();
                    jArray.SetLineInfo(lineInfo);
                    jContainer.Add(jArray);
                    jContainer = jArray;
                    break;
                }

                case JsonToken.EndArray:
                    if (jContainer == this)
                    {
                        return;
                    }
                    jContainer = jContainer.Parent;
                    break;

                case JsonToken.StartObject:
                {
                    JObject jObject2 = new JObject();
                    jObject2.SetLineInfo(lineInfo);
                    jContainer.Add(jObject2);
                    jContainer = jObject2;
                    break;
                }

                case JsonToken.EndObject:
                    if (jContainer == this)
                    {
                        return;
                    }
                    jContainer = jContainer.Parent;
                    break;

                case JsonToken.StartConstructor:
                {
                    JConstructor jConstructor = new JConstructor(r.Value.ToString());
                    jConstructor.SetLineInfo(jConstructor);
                    jContainer.Add(jConstructor);
                    jContainer = jConstructor;
                    break;
                }

                case JsonToken.EndConstructor:
                    if (jContainer == this)
                    {
                        return;
                    }
                    jContainer = jContainer.Parent;
                    break;

                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Date:
                case JsonToken.Bytes:
                {
                    JValue jValue = new JValue(r.Value);
                    jValue.SetLineInfo(lineInfo);
                    jContainer.Add(jValue);
                    break;
                }

                case JsonToken.Comment:
                {
                    JValue jValue = JValue.CreateComment(r.Value.ToString());
                    jValue.SetLineInfo(lineInfo);
                    jContainer.Add(jValue);
                    break;
                }

                case JsonToken.Null:
                {
                    JValue jValue = new JValue(null, JTokenType.Null);
                    jValue.SetLineInfo(lineInfo);
                    jContainer.Add(jValue);
                    break;
                }

                case JsonToken.Undefined:
                {
                    JValue jValue = new JValue(null, JTokenType.Undefined);
                    jValue.SetLineInfo(lineInfo);
                    jContainer.Add(jValue);
                    break;
                }

                case JsonToken.PropertyName:
                {
                    string    name      = r.Value.ToString();
                    JProperty jProperty = new JProperty(name);
                    jProperty.SetLineInfo(lineInfo);
                    JObject   jObject    = (JObject)jContainer;
                    JProperty jProperty2 = jObject.Property(name);
                    if (jProperty2 == null)
                    {
                        jContainer.Add(jProperty);
                    }
                    else
                    {
                        jProperty2.Replace(jProperty);
                    }
                    jContainer = jProperty;
                    break;
                }

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));

                case JsonToken.None:
                    break;
                }
            }while (r.Read());
        }
コード例 #11
0
        bool IJsonLineInfo.HasLineInfo()
        {
            IJsonLineInfo info = this._reader as IJsonLineInfo;

            return((info != null) && info.HasLineInfo());
        }
コード例 #12
0
        protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, IJsonLineInfo lineInfo, [Nullable(1)] string path, [Nullable(1)] Exception ex)
        {
            ErrorContext errorContext = this.GetErrorContext(currentObject, keyValue, path, ex);

            if (this.TraceWriter != null && this.TraceWriter.LevelFilter >= TraceLevel.Error && !errorContext.Traced)
            {
                errorContext.Traced = true;
                string text = (base.GetType() == typeof(JsonSerializerInternalWriter)) ? "Error serializing" : "Error deserializing";
                if (contract != null)
                {
                    string str            = text;
                    string str2           = " ";
                    Type   underlyingType = contract.UnderlyingType;
                    text = str + str2 + ((underlyingType != null) ? underlyingType.ToString() : null);
                }
                text = text + ". " + ex.Message;
                if (!(ex is JsonException))
                {
                    text = JsonPosition.FormatMessage(lineInfo, path, text);
                }
                this.TraceWriter.Trace(TraceLevel.Error, text, ex);
            }
            if (contract != null && currentObject != null)
            {
                contract.InvokeOnError(currentObject, this.Serializer.Context, errorContext);
            }
            if (!errorContext.Handled)
            {
                this.Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
            }
            return(errorContext.Handled);
        }
コード例 #13
0
        protected ValidationError CreateError(IFormattable message, ErrorType errorType, JSchema schema, object value, IList<ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            ValidationError error = ValidationError.CreateValidationError(message, errorType, schema, null, value, childErrors, lineInfo, path);

            return error;
        }
コード例 #14
0
        bool IJsonLineInfo.HasLineInfo()
        {
            IJsonLineInfo lineInfo = _reader as IJsonLineInfo;

            return((lineInfo != null) ? lineInfo.HasLineInfo() : false);
        }
コード例 #15
0
 public JsonResolveLinkContext (Type type, JsonSerializer serializer, IJsonLineInfo lineInfo)
 {
     Type = type;
     Serializer = serializer;
     LineInfo = lineInfo;
 }
コード例 #16
0
        protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, IJsonLineInfo lineInfo, string path, Exception ex)
        {
            ErrorContext errorContext = GetErrorContext(currentObject, keyValue, path, ex);

            if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Error && !errorContext.Traced)
            {
                // only write error once
                errorContext.Traced = true;

                string message = (_serializing) ? "Error serializing" : "Error deserializing";
                if (contract != null)
                {
                    message += " " + contract.UnderlyingType;
                }
                message += ". " + ex.Message;

                // add line information to non-json.net exception message
                if (!(ex is JsonException))
                {
                    message = JsonPosition.FormatMessage(lineInfo, path, message);
                }

                TraceWriter.Trace(TraceLevel.Error, message, ex);
            }

            if (contract != null)
            {
                contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
            }

            if (!errorContext.Handled)
            {
                Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
            }

            return(errorContext.Handled);
        }
コード例 #17
0
    internal JsonReaderException CreateReaderException(IJsonLineInfo lineInfo, string message, Exception ex)
    {
      message = FormatExceptionMessage(lineInfo, message);

      int lineNumber;
      int linePosition;
      if (lineInfo != null && lineInfo.HasLineInfo())
      {
        lineNumber = lineInfo.LineNumber;
        linePosition = lineInfo.LinePosition;
      }
      else
      {
        lineNumber = 0;
        linePosition = 0;
      }

      return new JsonReaderException(message, ex, lineNumber, linePosition);
    }
コード例 #18
0
ファイル: TraceJsonReader.cs プロジェクト: EnergonV/BestCS
 bool IJsonLineInfo.HasLineInfo()
 {
   IJsonLineInfo lineInfo = _innerReader as IJsonLineInfo;
   return lineInfo != null && lineInfo.HasLineInfo();
 }
コード例 #19
0
        bool Newtonsoft.Json.IJsonLineInfo.HasLineInfo()
        {
            IJsonLineInfo jsonLineInfo = this._reader as IJsonLineInfo;

            return(jsonLineInfo == null ? false : jsonLineInfo.HasLineInfo());
        }
コード例 #20
0
 internal static JSchemaReaderException Create(IJsonLineInfo lineInfo, Uri baseUri, string path, string message)
 {
     return Create(lineInfo, baseUri, path, message, null);
 }
コード例 #21
0
        bool IJsonLineInfo.HasLineInfo()
        {
            IJsonLineInfo lineInfo = _reader as IJsonLineInfo;

            return(lineInfo != null && lineInfo.HasLineInfo());
        }
コード例 #22
0
        public static JsonException Create(IJsonLineInfo lineInfo, string path, string message)
        {
            message = JsonPosition.FormatMessage(lineInfo, path, message);

            return(new JsonException(message));
        }
コード例 #23
0
 // Token: 0x060007DA RID: 2010
 // RVA: 0x0000AA6C File Offset: 0x00008C6C
 internal static JsonSerializationException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex)
 {
     message = JsonPosition.FormatMessage(lineInfo, path, message);
     return new JsonSerializationException(message, ex);
 }
コード例 #24
0
 private static string FormatMessage(string message, IJsonLineInfo jsonLineInfo)
 => $"[{jsonLineInfo.LineNumber}:{jsonLineInfo.LinePosition}]: {message}";
コード例 #25
0
        bool IJsonLineInfo.HasLineInfo()
        {
            IJsonLineInfo jsonLineInfo = this._reader as IJsonLineInfo;

            return(jsonLineInfo != null && jsonLineInfo.HasLineInfo());
        }
コード例 #26
0
ファイル: JContainer.cs プロジェクト: mdX7/WoWLegionCompanion
        internal void ReadContentFrom(JsonReader r)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo   = r as IJsonLineInfo;
            JContainer    jcontainer = this;

            for (;;)
            {
                if (jcontainer is JProperty && ((JProperty)jcontainer).Value != null)
                {
                    if (jcontainer == this)
                    {
                        break;
                    }
                    jcontainer = jcontainer.Parent;
                }
                switch (r.TokenType)
                {
                case JsonToken.None:
                    goto IL_244;

                case JsonToken.StartObject:
                {
                    JObject jobject = new JObject();
                    jobject.SetLineInfo(lineInfo);
                    jcontainer.Add(jobject);
                    jcontainer = jobject;
                    goto IL_244;
                }

                case JsonToken.StartArray:
                {
                    JArray jarray = new JArray();
                    jarray.SetLineInfo(lineInfo);
                    jcontainer.Add(jarray);
                    jcontainer = jarray;
                    goto IL_244;
                }

                case JsonToken.StartConstructor:
                {
                    JConstructor jconstructor = new JConstructor(r.Value.ToString());
                    jconstructor.SetLineInfo(jconstructor);
                    jcontainer.Add(jconstructor);
                    jcontainer = jconstructor;
                    goto IL_244;
                }

                case JsonToken.PropertyName:
                {
                    string    name      = r.Value.ToString();
                    JProperty jproperty = new JProperty(name);
                    jproperty.SetLineInfo(lineInfo);
                    JObject   jobject2   = (JObject)jcontainer;
                    JProperty jproperty2 = jobject2.Property(name);
                    if (jproperty2 == null)
                    {
                        jcontainer.Add(jproperty);
                    }
                    else
                    {
                        jproperty2.Replace(jproperty);
                    }
                    jcontainer = jproperty;
                    goto IL_244;
                }

                case JsonToken.Comment:
                {
                    JValue jvalue = JValue.CreateComment(r.Value.ToString());
                    jvalue.SetLineInfo(lineInfo);
                    jcontainer.Add(jvalue);
                    goto IL_244;
                }

                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Date:
                case JsonToken.Bytes:
                {
                    JValue jvalue = new JValue(r.Value);
                    jvalue.SetLineInfo(lineInfo);
                    jcontainer.Add(jvalue);
                    goto IL_244;
                }

                case JsonToken.Null:
                {
                    JValue jvalue = new JValue(null, JTokenType.Null);
                    jvalue.SetLineInfo(lineInfo);
                    jcontainer.Add(jvalue);
                    goto IL_244;
                }

                case JsonToken.Undefined:
                {
                    JValue jvalue = new JValue(null, JTokenType.Undefined);
                    jvalue.SetLineInfo(lineInfo);
                    jcontainer.Add(jvalue);
                    goto IL_244;
                }

                case JsonToken.EndObject:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto IL_244;

                case JsonToken.EndArray:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto IL_244;

                case JsonToken.EndConstructor:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto IL_244;
                }
                goto Block_4;
IL_244:
                if (!r.Read())
                {
                    return;
                }
            }
            return;

Block_4:
            throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[]
            {
                r.TokenType
            }));
        }
コード例 #27
0
 internal void SetLineInfo(IJsonLineInfo lineInfo)
 {
     _lineNumber   = lineInfo.LineNumber;
     _linePosition = lineInfo.LinePosition;
 }
コード例 #28
0
 internal static ValidationError CreateValidationError(IFormattable message, ErrorType errorType, JSchema schema, Uri schemaId, object value, IList<ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
 {
     ValidationError error = new ValidationError();
     error._formattable = message;
     error.ErrorType = errorType;
     error.Path = path;
     if (lineInfo != null)
     {
         error.LineNumber = lineInfo.LineNumber;
         error.LinePosition = lineInfo.LinePosition;
     }
     error.Schema = schema;
     error.SchemaId = schemaId;
     error.SchemaBaseUri = schema.BaseUri;
     error.Value = value;
     error.ChildErrors = childErrors;
     return error;
 }
コード例 #29
0
 public ConversionFailedException(string message, IJsonLineInfo jsonLineInfo)
     : base(FormatMessage(message, jsonLineInfo))
 {
 }
コード例 #30
0
 internal static JSchemaReaderException Create(IJsonLineInfo lineInfo, Uri baseUri, string path, string message)
 {
     return(Create(lineInfo, baseUri, path, message, null));
 }
コード例 #31
0
    // Token: 0x0600094B RID: 2379 RVA: 0x00033128 File Offset: 0x00031328
    protected bool method_4(object object_0, JsonContract jsonContract_0, object object_1, IJsonLineInfo ijsonLineInfo_0, string string_0, Exception exception_0)
    {
        ErrorContext errorContext = this.method_2(object_0, object_1, string_0, exception_0);

        if (this.itraceWriter_0 != null && this.itraceWriter_0.LevelFilter >= TraceLevel.Error && !errorContext.method_0())
        {
            errorContext.method_1(true);
            string text = (base.GetType() == typeof(Class119)) ? "Error serializing" : "Error deserializing";
            if (jsonContract_0 != null)
            {
                text = text + " " + jsonContract_0.UnderlyingType;
            }
            text = text + ". " + exception_0.Message;
            if (!(exception_0 is JsonException))
            {
                text = Struct0.smethod_1(ijsonLineInfo_0, string_0, text);
            }
            this.itraceWriter_0.Trace(TraceLevel.Error, text, exception_0);
        }
        if (jsonContract_0 != null && object_0 != null)
        {
            jsonContract_0.method_4(object_0, this.jsonSerializer_0.Context, errorContext);
        }
        if (!errorContext.Handled)
        {
            this.jsonSerializer_0.method_6(new ErrorEventArgs(object_0, errorContext));
        }
        return(errorContext.Handled);
    }
コード例 #32
0
 public TokenError(IJsonLineInfo lineInfo, string message)
     : this(lineInfo.LineNumber, lineInfo.LinePosition, message)
 {
 }
コード例 #33
0
        protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, IJsonLineInfo lineInfo, string path, Exception ex)
        {
            ErrorContext errorContext = GetErrorContext(currentObject, keyValue, path, ex);

            if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Error && !errorContext.Traced)
            {
                // only write error once
                errorContext.Traced = true;

                // kind of a hack but meh. might clean this up later
                string message = (GetType() == typeof(JsonSerializerInternalWriter)) ? "Error serializing" : "Error deserializing";
                if (contract != null)
                {
                    message += " " + contract.UnderlyingType;
                }
                message += ". " + ex.Message;

                // add line information to non-json.net exception message
                if (!(ex is JsonException))
                {
                    message = JsonPosition.FormatMessage(lineInfo, path, message);
                }

                TraceWriter.Trace(TraceLevel.Error, message, ex);
            }

            // attribute method is non-static so don't invoke if no object
            if (contract != null && currentObject != null)
            {
                contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
            }

            if (!errorContext.Handled)
            {
                Serializer.OnError(new ErrorEventArgs(currentObject, errorContext));
            }

            return(errorContext.Handled);
        }
コード例 #34
0
        protected ValidationError CreateError(string message, ErrorType errorType, JSchema schema, object value, IList <ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            ValidationError error = new ValidationError();

            error.Message   = message;
            error.ErrorType = errorType;
            error.Path      = path;
            if (lineInfo != null)
            {
                error.LineNumber   = lineInfo.LineNumber;
                error.LinePosition = lineInfo.LinePosition;
            }
            error.Schema      = schema;
            error.Value       = value;
            error.ChildErrors = childErrors;

            return(error);
        }
コード例 #35
0
        internal static ValidationError CreateValidationError(IFormattable message, ErrorType errorType, JSchema schema, Uri schemaId, object value, IList <ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            ValidationError error = new ValidationError();

            error._formattable = message;
            error._childErrors = childErrors;

            error.ErrorType = errorType;
            error.Path      = path;
            if (lineInfo != null)
            {
                error.LineNumber   = lineInfo.LineNumber;
                error.LinePosition = lineInfo.LinePosition;
            }
            error.Schema        = schema;
            error.SchemaId      = schemaId;
            error.SchemaBaseUri = schema.BaseUri;
            error.Value         = value;
            return(error);
        }
コード例 #36
0
        protected ValidationError CreateError(IFormattable message, ErrorType errorType, JSchema schema, object value, IList <ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            ValidationError error = ValidationError.CreateValidationError(message, errorType, schema, null, value, childErrors, lineInfo, path);

            return(error);
        }
コード例 #37
0
        internal void ReadContentFrom(JsonReader r)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo = r as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if (parent is JProperty && ((JProperty)parent).Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(r.Value.ToString());
                    constructor.SetLineInfo(lineInfo);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                case JsonToken.Bytes:
                    JValue v = new JValue(r.Value);
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.Comment:
                    v = JValue.CreateComment(r.Value.ToString());
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    string    propertyName = r.Value.ToString();
                    JProperty property     = new JProperty(propertyName);
                    property.SetLineInfo(lineInfo);
                    JObject parentObject = (JObject)parent;
                    // handle multiple properties with the same name in JSON
                    JProperty existingPropertyWithName = parentObject.Property(propertyName);
                    if (existingPropertyWithName == null)
                    {
                        parent.Add(property);
                    }
                    else
                    {
                        existingPropertyWithName.Replace(property);
                    }
                    parent = property;
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            } while (r.Read());
        }
コード例 #38
0
        internal void method_11(JsonReader jsonReader_0)
        {
            JValue    value2;
            JProperty property;

            Class203.smethod_2(jsonReader_0, "r");
            IJsonLineInfo info   = jsonReader_0 as IJsonLineInfo;
            JContainer    parent = this;

            goto Label_0204;
Label_01D8:
            if (!jsonReader_0.Read())
            {
                return;
            }
Label_0204:
            if ((parent is JProperty) && (((JProperty)parent).Value != null))
            {
                if (parent == this)
                {
                    return;
                }
                parent = parent.Parent;
            }
            switch (jsonReader_0.JsonToken_0)
            {
            case JsonToken.None:
                goto Label_01D8;

            case JsonToken.StartObject:
            {
                JObject content = new JObject();
                content.method_0(info);
                parent.Add(content);
                parent = content;
                goto Label_01D8;
            }

            case JsonToken.StartArray:
            {
                JArray array = new JArray();
                array.method_0(info);
                parent.Add(array);
                parent = array;
                goto Label_01D8;
            }

            case JsonToken.StartConstructor:
            {
                JConstructor constructor = new JConstructor(jsonReader_0.Object_0.ToString());
                constructor.method_0(constructor);
                parent.Add(constructor);
                parent = constructor;
                goto Label_01D8;
            }

            case JsonToken.PropertyName:
            {
                string name = jsonReader_0.Object_0.ToString();
                property = new JProperty(name);
                property.method_0(info);
                JProperty property2 = ((JObject)parent).Property(name);
                if (property2 != null)
                {
                    property2.Replace(property);
                    break;
                }
                parent.Add(property);
                break;
            }

            case JsonToken.Comment:
                value2 = JValue.CreateComment(jsonReader_0.Object_0.ToString());
                value2.method_0(info);
                parent.Add(value2);
                goto Label_01D8;

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            case JsonToken.Boolean:
            case JsonToken.Date:
            case JsonToken.Bytes:
                value2 = new JValue(jsonReader_0.Object_0);
                value2.method_0(info);
                parent.Add(value2);
                goto Label_01D8;

            case JsonToken.Null:
                value2 = new JValue(null, JTokenType.Null);
                value2.method_0(info);
                parent.Add(value2);
                goto Label_01D8;

            case JsonToken.Undefined:
                value2 = new JValue(null, JTokenType.Undefined);
                value2.method_0(info);
                parent.Add(value2);
                goto Label_01D8;

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

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

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

            default:
                throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".smethod_0(CultureInfo.InvariantCulture, jsonReader_0.JsonToken_0));
            }
            parent = property;
            goto Label_01D8;
        }
コード例 #39
0
        public void ReadLineInfo()
        {
            string input = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    ""500 gigabyte hard drive""
  ]
}";

            StringReader sr = new StringReader(input);

            JObject o = JObject.Parse(input);

            using (JTokenReader jsonReader = new JTokenReader(o))
            {
                IJsonLineInfo lineInfo = jsonReader;

                Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
                Assert.AreEqual(0, lineInfo.LineNumber);
                Assert.AreEqual(0, lineInfo.LinePosition);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
                Assert.AreEqual(1, lineInfo.LineNumber);
                Assert.AreEqual(1, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
                Assert.AreEqual(jsonReader.Value, "CPU");
                Assert.AreEqual(2, lineInfo.LineNumber);
                Assert.AreEqual(7, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "Intel");
                Assert.AreEqual(2, lineInfo.LineNumber);
                Assert.AreEqual(15, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
                Assert.AreEqual(jsonReader.Value, "Drives");
                Assert.AreEqual(3, lineInfo.LineNumber);
                Assert.AreEqual(10, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
                Assert.AreEqual(3, lineInfo.LineNumber);
                Assert.AreEqual(12, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "DVD read/writer");
                Assert.AreEqual(4, lineInfo.LineNumber);
                Assert.AreEqual(22, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "500 gigabyte hard drive");
                Assert.AreEqual(5, lineInfo.LineNumber);
                Assert.AreEqual(30, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
                Assert.AreEqual(0, lineInfo.LineNumber);
                Assert.AreEqual(0, lineInfo.LinePosition);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
                Assert.AreEqual(0, lineInfo.LineNumber);
                Assert.AreEqual(0, lineInfo.LinePosition);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
            }
        }
コード例 #40
0
ファイル: JsonReader.cs プロジェクト: pvasek/Newtonsoft.Json
    internal static string FormatExceptionMessage(IJsonLineInfo lineInfo, string message)
    {
      if (!message.EndsWith("."))
        message += ".";

      if (lineInfo != null && lineInfo.HasLineInfo())
        message += " Line {0}, position {1}.".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);

      return message;
    }
コード例 #41
0
        internal static JsonSerializationException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex)
        {
            message = JsonPosition.FormatMessage(lineInfo, path, message);

            return(new JsonSerializationException(message, ex));
        }
コード例 #42
0
        private void RaiseError(string message, JsonSchemaModel schema)
        {
            IJsonLineInfo jsonLineInfo = (IJsonLineInfo)this;

            this.OnValidationEvent(new JsonSchemaException(jsonLineInfo.HasLineInfo() ? message + StringUtils.FormatWith(" Line {0}, position {1}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)jsonLineInfo.LineNumber, (object)jsonLineInfo.LinePosition) : message, (Exception)null, this.Path, jsonLineInfo.LineNumber, jsonLineInfo.LinePosition));
        }
コード例 #43
0
        private async Task ReadContentFromAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default)
        {
            IJsonLineInfo lineInfo = reader as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if (parent is JProperty p && p.Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (reader.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo, settings);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo, settings);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(reader.Value.ToString());
                    constructor.SetLineInfo(lineInfo, settings);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                case JsonToken.Bytes:
                    JValue v = new JValue(reader.Value);
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Comment:
                    if (settings != null && settings.CommentHandling == CommentHandling.Load)
                    {
                        v = JValue.CreateComment(reader.Value.ToString());
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                    }
                    break;

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    JProperty property = ReadProperty(reader, settings, lineInfo, parent);
                    if (property != null)
                    {
                        parent = property;
                    }
                    else
                    {
                        await reader.SkipAsync().ConfigureAwait(false);
                    }
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                }
            } while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false));
        }
コード例 #44
0
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull(r, nameof(r));
            IJsonLineInfo lineInfo = r as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if ((parent as JProperty)?.Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo, settings);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo, settings);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(r.Value.ToString());
                    constructor.SetLineInfo(lineInfo, settings);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

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

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

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    string    propertyName = r.Value.ToString();
                    JProperty property     = new JProperty(propertyName);
                    property.SetLineInfo(lineInfo, settings);
                    JObject parentObject = (JObject)parent;
                    // handle multiple properties with the same name in JSON
                    JProperty existingPropertyWithName = parentObject.Property(propertyName);
                    if (existingPropertyWithName == null)
                    {
                        parent.Add(property);
                    }
                    else
                    {
                        existingPropertyWithName.Replace(property);
                    }
                    parent = property;
                    break;

                case JsonToken.SmallDec:
                case JsonToken.PercentValV2:
                    try
                    {
                        v = new JValue(r.Value);
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                        break;
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("Exception called from JContainer->ReadContentFrom of type " + ex.ToString());
                        throw;
                    }
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            } while (r.Read());
        }
コード例 #45
0
        internal static JsonReaderException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex)
        {
            message = JsonPosition.FormatMessage(lineInfo, path, message);

            int lineNumber;
            int linePosition;
            if (lineInfo != null && lineInfo.HasLineInfo())
            {
                lineNumber = lineInfo.LineNumber;
                linePosition = lineInfo.LinePosition;
            }
            else
            {
                lineNumber = 0;
                linePosition = 0;
            }

            return new JsonReaderException(message, ex, path, lineNumber, linePosition);
        }
コード例 #46
0
 internal void SetLineInfo(IJsonLineInfo lineInfo)
 {
     _lineNumber = lineInfo.LineNumber;
     _linePosition = lineInfo.LinePosition;
 }
コード例 #47
0
    private JsonSerializationException CreateSerializationException(IJsonLineInfo lineInfo, string message, Exception ex)
    {
      message = JsonReader.FormatExceptionMessage(lineInfo, message);

      return new JsonSerializationException(message, ex);
    }
コード例 #48
0
    internal static string FormatExceptionMessage(IJsonLineInfo lineInfo, string path, string message)
    {
      message = message.Trim();

      if (!message.EndsWith("."))
        message += ".";

      message += " Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);

      if (lineInfo != null && lineInfo.HasLineInfo())
        message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);

      message += ".";

      return message;
    }