508 lines
15 KiB
C#
508 lines
15 KiB
C#
|
// Decompiled with JetBrains decompiler
|
|||
|
// Type: Rei.Fs.JsonObject
|
|||
|
// Assembly: Rei.Fs, Version=1.13.2.8796, Culture=neutral, PublicKeyToken=null
|
|||
|
// MVID: D8B08A8B-697C-4439-9CFF-1BE4EE46F7B0
|
|||
|
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Fs.dll
|
|||
|
|
|||
|
using Microsoft.VisualBasic.CompilerServices;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Globalization;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace Rei.Fs
|
|||
|
{
|
|||
|
public class JsonObject
|
|||
|
{
|
|||
|
private const string JSONIgnoreChars = " \t\r\n\0";
|
|||
|
private JsonObject.JsonType pType;
|
|||
|
private string pString;
|
|||
|
private long pInteger;
|
|||
|
private double pDouble;
|
|||
|
private bool pBoolean;
|
|||
|
private Dictionary<string, JsonObject> pHash;
|
|||
|
private List<JsonObject> pArray;
|
|||
|
|
|||
|
public static JsonObject ReadJson(string str)
|
|||
|
{
|
|||
|
JsonObject jsonObject = (JsonObject) null;
|
|||
|
JsonObject.InnerReadJson(0, str, ref jsonObject);
|
|||
|
return jsonObject;
|
|||
|
}
|
|||
|
|
|||
|
private static string GetSubStrArround(string str, int index)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(str))
|
|||
|
return "";
|
|||
|
checked { index -= 30; }
|
|||
|
if (index < 0)
|
|||
|
index = 0;
|
|||
|
int length = 60;
|
|||
|
if (str.Length < checked (index + length))
|
|||
|
length = checked (str.Length - index);
|
|||
|
return str.Substring(index, length);
|
|||
|
}
|
|||
|
|
|||
|
private static int InnerReadJson(int index, string str, ref JsonObject value)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 123)
|
|||
|
{
|
|||
|
Dictionary<string, JsonObject> hash = new Dictionary<string, JsonObject>();
|
|||
|
JsonObject jsonObject = (JsonObject) null;
|
|||
|
checked { ++index; }
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 125)
|
|||
|
{
|
|||
|
value = new JsonObject(hash);
|
|||
|
return checked (index + 1);
|
|||
|
}
|
|||
|
while (true)
|
|||
|
{
|
|||
|
string key = (string) null;
|
|||
|
index = JsonObject.InnreReadJSON_String(index, str, ref key);
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 58)
|
|||
|
{
|
|||
|
checked { ++index; }
|
|||
|
index = JsonObject.InnerReadJson(index, str, ref jsonObject);
|
|||
|
hash.Add(key, jsonObject);
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 44)
|
|||
|
checked { ++index; }
|
|||
|
else
|
|||
|
goto label_19;
|
|||
|
}
|
|||
|
else
|
|||
|
break;
|
|||
|
}
|
|||
|
throw new Exception();
|
|||
|
label_19:
|
|||
|
if ((int) str[index] != 125)
|
|||
|
throw new Exception();
|
|||
|
checked { ++index; }
|
|||
|
value = new JsonObject(hash);
|
|||
|
return index;
|
|||
|
}
|
|||
|
if ((int) str[index] == 91)
|
|||
|
{
|
|||
|
List<JsonObject> array = new List<JsonObject>();
|
|||
|
JsonObject jsonObject = (JsonObject) null;
|
|||
|
checked { ++index; }
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 93)
|
|||
|
{
|
|||
|
value = new JsonObject(array);
|
|||
|
return checked (index + 1);
|
|||
|
}
|
|||
|
while (true)
|
|||
|
{
|
|||
|
index = JsonObject.InnerReadJson(index, str, ref jsonObject);
|
|||
|
array.Add(jsonObject);
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 44)
|
|||
|
checked { ++index; }
|
|||
|
else
|
|||
|
break;
|
|||
|
}
|
|||
|
if ((int) str[index] != 93)
|
|||
|
throw new Exception();
|
|||
|
checked { ++index; }
|
|||
|
value = new JsonObject(array);
|
|||
|
return index;
|
|||
|
}
|
|||
|
if ((int) str[index] == 34)
|
|||
|
{
|
|||
|
string str1 = (string) null;
|
|||
|
index = JsonObject.InnreReadJSON_String(index, str, ref str1);
|
|||
|
value = new JsonObject(str1);
|
|||
|
return index;
|
|||
|
}
|
|||
|
if (checked (str.Length - index) > 4 && Operators.CompareString(str.Substring(index, 4).ToLowerInvariant(), "true", false) == 0)
|
|||
|
{
|
|||
|
value = new JsonObject(true);
|
|||
|
return checked (index + 4);
|
|||
|
}
|
|||
|
if (checked (str.Length - index) > 5 && Operators.CompareString(str.Substring(index, 5).ToLowerInvariant(), "false", false) == 0)
|
|||
|
{
|
|||
|
value = new JsonObject(false);
|
|||
|
return checked (index + 5);
|
|||
|
}
|
|||
|
if (checked (str.Length - index) > 4 && Operators.CompareString(str.Substring(index, 4).ToLowerInvariant(), "null", false) == 0)
|
|||
|
{
|
|||
|
value = new JsonObject();
|
|||
|
return checked (index + 4);
|
|||
|
}
|
|||
|
int startIndex = index;
|
|||
|
while ("+-0123456789".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 101 || (int) str[index] == 69 || (int) str[index] == 46)
|
|||
|
{
|
|||
|
while ("+-0123456789.eE".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
double result;
|
|||
|
if (double.TryParse(str.Substring(startIndex, checked (index - startIndex)), NumberStyles.Any, (IFormatProvider) CultureInfo.InvariantCulture, out result))
|
|||
|
{
|
|||
|
value = new JsonObject(result);
|
|||
|
return index;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
long result;
|
|||
|
if (long.TryParse(str.Substring(startIndex, checked (index - startIndex)), out result))
|
|||
|
{
|
|||
|
value = new JsonObject(result);
|
|||
|
return index;
|
|||
|
}
|
|||
|
}
|
|||
|
throw new Exception();
|
|||
|
}
|
|||
|
catch (Exception ex) when (
|
|||
|
{
|
|||
|
// ISSUE: unable to correctly present filter
|
|||
|
ProjectData.SetProjectError(ex);
|
|||
|
if (!(ex is InvalidServerResponseException))
|
|||
|
{
|
|||
|
SuccessfulFiltering;
|
|||
|
}
|
|||
|
else
|
|||
|
throw;
|
|||
|
}
|
|||
|
)
|
|||
|
{
|
|||
|
throw new InvalidServerResponseException("Cannot parse server response. index:" + index.ToString() + " arround: \"" + JsonObject.GetSubStrArround(str, index) + "\"");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static int InnreReadJSON_String(int index, string str, ref string value)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
while (" \t\r\n\0".Contains(Conversions.ToString(str[index])))
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] != 34)
|
|||
|
throw new Exception();
|
|||
|
checked { ++index; }
|
|||
|
StringBuilder stringBuilder = new StringBuilder();
|
|||
|
while (true)
|
|||
|
{
|
|||
|
while ((int) str[index] != 92)
|
|||
|
{
|
|||
|
if ((int) str[index] != 34)
|
|||
|
{
|
|||
|
stringBuilder.Append(str[index]);
|
|||
|
checked { ++index; }
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
value = stringBuilder.ToString();
|
|||
|
return checked (index + 1);
|
|||
|
}
|
|||
|
}
|
|||
|
checked { ++index; }
|
|||
|
if ((int) str[index] == 98)
|
|||
|
stringBuilder.Append("\b");
|
|||
|
else if ((int) str[index] == 102)
|
|||
|
stringBuilder.Append("\f");
|
|||
|
else if ((int) str[index] == 110)
|
|||
|
stringBuilder.Append("\n");
|
|||
|
else if ((int) str[index] == 114)
|
|||
|
stringBuilder.Append("\r");
|
|||
|
else if ((int) str[index] == 116)
|
|||
|
stringBuilder.Append("\t");
|
|||
|
else if ((int) str[index] == 117)
|
|||
|
{
|
|||
|
stringBuilder.Append(Convert.ToChar(Convert.ToUInt32(str.Substring(checked (index + 1), 4), 16)));
|
|||
|
checked { index += 4; }
|
|||
|
}
|
|||
|
else
|
|||
|
stringBuilder.Append(str[index]);
|
|||
|
checked { ++index; }
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex) when (
|
|||
|
{
|
|||
|
// ISSUE: unable to correctly present filter
|
|||
|
ProjectData.SetProjectError(ex);
|
|||
|
if (!(ex is InvalidServerResponseException))
|
|||
|
{
|
|||
|
SuccessfulFiltering;
|
|||
|
}
|
|||
|
else
|
|||
|
throw;
|
|||
|
}
|
|||
|
)
|
|||
|
{
|
|||
|
throw new InvalidServerResponseException("Cannot parse server response. index:" + index.ToString() + " arround: \"" + JsonObject.GetSubStrArround(str, index) + "\"");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static string EscapeString(string str)
|
|||
|
{
|
|||
|
StringBuilder stringBuilder = new StringBuilder(checked (str.Length * 2));
|
|||
|
stringBuilder.Append("\"");
|
|||
|
int num1 = 0;
|
|||
|
int num2 = checked (str.Length - 1);
|
|||
|
int index = num1;
|
|||
|
while (index <= num2)
|
|||
|
{
|
|||
|
char ch = str[index];
|
|||
|
if (Operators.CompareString(Conversions.ToString(ch), "\b", false) == 0)
|
|||
|
stringBuilder.Append("\\b");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\f", false) == 0)
|
|||
|
stringBuilder.Append("\\f");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\n", false) == 0)
|
|||
|
stringBuilder.Append("\\n");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\r", false) == 0)
|
|||
|
stringBuilder.Append("\\r");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\t", false) == 0)
|
|||
|
stringBuilder.Append("\\t");
|
|||
|
else if (Convert.ToInt32(ch) < 32)
|
|||
|
stringBuilder.Append("\\u" + Convert.ToInt32(ch).ToString("X4"));
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\"", false) == 0)
|
|||
|
{
|
|||
|
stringBuilder.Append("\\u0022");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if ((int) ch == 34 || (int) ch == 92)
|
|||
|
stringBuilder.Append("\\");
|
|||
|
stringBuilder.Append(ch);
|
|||
|
}
|
|||
|
checked { ++index; }
|
|||
|
}
|
|||
|
stringBuilder.Append("\"");
|
|||
|
return stringBuilder.ToString();
|
|||
|
}
|
|||
|
|
|||
|
public static string EscapeStringAscii(string str)
|
|||
|
{
|
|||
|
StringBuilder stringBuilder = new StringBuilder(checked (str.Length * 2));
|
|||
|
stringBuilder.Append("\"");
|
|||
|
int num1 = 0;
|
|||
|
int num2 = checked (str.Length - 1);
|
|||
|
int index = num1;
|
|||
|
while (index <= num2)
|
|||
|
{
|
|||
|
char ch = str[index];
|
|||
|
if (Operators.CompareString(Conversions.ToString(ch), "\b", false) == 0)
|
|||
|
stringBuilder.Append("\\b");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\f", false) == 0)
|
|||
|
stringBuilder.Append("\\f");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\n", false) == 0)
|
|||
|
stringBuilder.Append("\\n");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\r", false) == 0)
|
|||
|
stringBuilder.Append("\\r");
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\t", false) == 0)
|
|||
|
stringBuilder.Append("\\t");
|
|||
|
else if (Convert.ToInt32(ch) < 32 || Convert.ToInt32(ch) >= (int) sbyte.MaxValue)
|
|||
|
stringBuilder.Append("\\u" + Convert.ToInt32(ch).ToString("X4"));
|
|||
|
else if (Operators.CompareString(Conversions.ToString(ch), "\"", false) == 0)
|
|||
|
{
|
|||
|
stringBuilder.Append("\\u0022");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if ((int) ch == 34 || (int) ch == 92)
|
|||
|
stringBuilder.Append("\\");
|
|||
|
stringBuilder.Append(ch);
|
|||
|
}
|
|||
|
checked { ++index; }
|
|||
|
}
|
|||
|
stringBuilder.Append("\"");
|
|||
|
return stringBuilder.ToString();
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject()
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.Nothing;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject(string str)
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.String;
|
|||
|
this.pString = str;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject(long number)
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.Integer;
|
|||
|
this.pInteger = number;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject(double number)
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.Double;
|
|||
|
this.pDouble = number;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject(bool @bool)
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.Boolean;
|
|||
|
this.pBoolean = @bool;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject(Dictionary<string, JsonObject> hash)
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.Hash;
|
|||
|
this.pHash = hash;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject(List<JsonObject> array)
|
|||
|
{
|
|||
|
this.pType = JsonObject.JsonType.Array;
|
|||
|
this.pArray = array;
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject.JsonType Type
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pType;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string String
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pString;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public long Integer
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pInteger;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public double Double
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pDouble;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool Boolean
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pBoolean;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public Dictionary<string, JsonObject> Hash
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pHash;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<JsonObject> Array
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.pArray;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool IsNothing
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.Type == JsonObject.JsonType.Nothing;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject this[string name]
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (this.Type != JsonObject.JsonType.Hash)
|
|||
|
return new JsonObject();
|
|||
|
if (!this.Hash.ContainsKey(name))
|
|||
|
return new JsonObject();
|
|||
|
return this.Hash[name];
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
if (this.Type != JsonObject.JsonType.Hash)
|
|||
|
throw new InvalidOperationException();
|
|||
|
this.Hash.Add(name, value);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public JsonObject this[int index]
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (this.Type != JsonObject.JsonType.Array)
|
|||
|
return new JsonObject();
|
|||
|
if (index < 0)
|
|||
|
throw new ArgumentOutOfRangeException();
|
|||
|
if (index >= this.Array.Count)
|
|||
|
return new JsonObject();
|
|||
|
return this.Array[index];
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
if (this.Type != JsonObject.JsonType.Array)
|
|||
|
throw new InvalidOperationException();
|
|||
|
if (index < 0)
|
|||
|
throw new ArgumentOutOfRangeException();
|
|||
|
if (index > this.Array.Count)
|
|||
|
throw new ArgumentOutOfRangeException();
|
|||
|
if (index == this.Array.Count)
|
|||
|
this.Array.Add(value);
|
|||
|
else
|
|||
|
this.Array[index] = value;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public DateTime GetAsDateTime()
|
|||
|
{
|
|||
|
if (this.Type == JsonObject.JsonType.Nothing)
|
|||
|
return DateTime.MinValue;
|
|||
|
return DateTime.Parse(this.String, (IFormatProvider) CultureInfo.InvariantCulture);
|
|||
|
}
|
|||
|
|
|||
|
public long IntegerWithDefault(long @default)
|
|||
|
{
|
|||
|
if (this.Type == JsonObject.JsonType.Integer)
|
|||
|
return this.Integer;
|
|||
|
return @default;
|
|||
|
}
|
|||
|
|
|||
|
public string StringWithDefault(string @default)
|
|||
|
{
|
|||
|
if (this.Type == JsonObject.JsonType.String)
|
|||
|
return this.String;
|
|||
|
return @default;
|
|||
|
}
|
|||
|
|
|||
|
public enum JsonType
|
|||
|
{
|
|||
|
String,
|
|||
|
Integer,
|
|||
|
Double,
|
|||
|
Boolean,
|
|||
|
Nothing,
|
|||
|
Hash,
|
|||
|
Array,
|
|||
|
}
|
|||
|
}
|
|||
|
}
|