JsonPosition
struct JsonPosition
using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace Newtonsoft.Json
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal struct JsonPosition
{
private static readonly char[] SpecialCharacters = new char[18] {
'.',
' ',
'\'',
'/',
'"',
'[',
']',
'(',
')',
'\t',
'\n',
'\r',
'',
'\b',
'\\',
'
',
'
',
'
'
};
internal JsonContainerType Type;
internal int Position;
[System.Runtime.CompilerServices.Nullable(2)]
internal string PropertyName;
internal bool HasIndex;
public JsonPosition(JsonContainerType type)
{
Type = type;
HasIndex = TypeHasIndex(type);
Position = -1;
PropertyName = null;
}
internal int CalculateLength()
{
JsonContainerType type = Type;
if (type == JsonContainerType.Object)
return PropertyName.Length + 5;
if ((uint)(type - 2) <= 1)
return MathUtils.IntLength((ulong)Position) + 2;
throw new ArgumentOutOfRangeException("Type");
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal void WriteTo([System.Runtime.CompilerServices.Nullable(1)] StringBuilder sb, ref StringWriter writer, ref char[] buffer)
{
JsonContainerType type = Type;
if (type != JsonContainerType.Object) {
if ((uint)(type - 2) <= 1) {
sb.Append('[');
sb.Append(Position);
sb.Append(']');
}
} else {
string propertyName = PropertyName;
if (propertyName.IndexOfAny(SpecialCharacters) != -1) {
sb.Append("['");
if (writer == null)
writer = new StringWriter(sb);
JavaScriptUtils.WriteEscapedJavaScriptString(writer, propertyName, '\'', false, JavaScriptUtils.SingleQuoteCharEscapeFlags, StringEscapeHandling.Default, null, ref buffer);
sb.Append("']");
} else {
if (sb.Length > 0)
sb.Append('.');
sb.Append(propertyName);
}
}
}
internal static bool TypeHasIndex(JsonContainerType type)
{
if (type != JsonContainerType.Array)
return type == JsonContainerType.Constructor;
return true;
}
internal static string BuildPath(List<JsonPosition> positions, JsonPosition? currentPosition)
{
int num = 0;
JsonPosition jsonPosition;
if (positions != null) {
for (int i = 0; i < positions.Count; i++) {
int num2 = num;
jsonPosition = positions[i];
num = num2 + jsonPosition.CalculateLength();
}
}
if (currentPosition.HasValue) {
int num3 = num;
jsonPosition = currentPosition.GetValueOrDefault();
num = num3 + jsonPosition.CalculateLength();
}
StringBuilder stringBuilder = new StringBuilder(num);
StringWriter writer = null;
char[] buffer = null;
if (positions != null) {
foreach (JsonPosition position in positions) {
position.WriteTo(stringBuilder, ref writer, ref buffer);
}
}
if (currentPosition.HasValue) {
jsonPosition = currentPosition.GetValueOrDefault();
jsonPosition.WriteTo(stringBuilder, ref writer, ref buffer);
}
return stringBuilder.ToString();
}
internal static string FormatMessage([System.Runtime.CompilerServices.Nullable(2)] IJsonLineInfo lineInfo, string path, string message)
{
if (!message.EndsWith(Environment.NewLine, StringComparison.Ordinal)) {
message = message.Trim();
if (!StringUtils.EndsWith(message, '.'))
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;
}
}
}