JsonWriter and JsonParser: Static methods for easier usage.

This commit is contained in:
2019-11-24 04:56:57 +01:00
parent 6782200917
commit 53a947db75
2 changed files with 45 additions and 29 deletions

View File

@@ -15,7 +15,7 @@ namespace VAR.Json
private ParserContext _ctx;
private bool _tainted = false;
private List<Type> _knownTypes = new List<Type>();
private readonly List<Type> _knownTypes = new List<Type>();
#endregion Declarations
@@ -35,7 +35,7 @@ namespace VAR.Json
#region Private methods
private static Dictionary<Type, PropertyInfo[]> _dictProperties = new Dictionary<Type, PropertyInfo[]>();
private static readonly Dictionary<Type, PropertyInfo[]> _dictProperties = new Dictionary<Type, PropertyInfo[]>();
private PropertyInfo[] Type_GetProperties(Type type)
{
@@ -497,7 +497,7 @@ namespace VAR.Json
else if (c == ',')
{
_ctx.Next();
c = _ctx.SkipWhite();
_ctx.SkipWhite();
expectedKey = true;
expectedValue = false;
}
@@ -517,7 +517,7 @@ namespace VAR.Json
if (expectedKey != false)
{
attributeName = ParseString(true);
c = _ctx.SkipWhite();
_ctx.SkipWhite();
expectedKey = false;
expectedValue = true;
}
@@ -538,8 +538,8 @@ namespace VAR.Json
private object ParseValue(int recusiveCount = 1)
{
object token = null;
char c = _ctx.SkipWhite();
object token;
switch (c)
{
case '"':
@@ -597,28 +597,6 @@ namespace VAR.Json
return token;
}
private string CleanIdentifier(string input)
{
int i;
char c;
i = input.Length - 1;
if (i < 0)
{
return input;
}
c = input[i];
while (char.IsLetter(c) || char.IsDigit(c) || c == '_')
{
i--;
if (i < 0)
{
break;
}
c = input[i];
}
return input.Substring(i + 1);
}
#endregion Private methods
#region Public methods
@@ -648,6 +626,17 @@ namespace VAR.Json
return obj;
}
private static JsonParser _currentInstance = null;
public static object ParseText(string text)
{
if(_currentInstance == null)
{
_currentInstance = new JsonParser();
}
return _currentInstance.Parse(text);
}
#endregion Public methods
}
}

View File

@@ -395,6 +395,33 @@ namespace VAR.Json
return sbOutput.ToString();
}
private static Dictionary<JsonWriterConfiguration, JsonWriter> _dictInstances = new Dictionary<JsonWriterConfiguration, JsonWriter>();
public static string WriteObject(object obj, JsonWriterConfiguration config = null)
{
if(_dictInstances.ContainsKey(config) == false)
{
JsonWriter newJsonWriter = new JsonWriter(config);
_dictInstances.Add(config, newJsonWriter);
}
JsonWriter jsonWriter = _dictInstances[config];
return jsonWriter.Write(obj);
}
public static string WriteObject(object obj,
bool indent = false,
bool useTablForIndent = false,
int indentChars = 4,
int indentThresold = 3)
{
return WriteObject(obj, new JsonWriterConfiguration(
indent: indent,
useTablForIndent: useTablForIndent,
indentChars: indentChars,
indentThresold: indentThresold));
}
#endregion Public methods
}
}