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 ParserContext _ctx;
private bool _tainted = false; private bool _tainted = false;
private List<Type> _knownTypes = new List<Type>(); private readonly List<Type> _knownTypes = new List<Type>();
#endregion Declarations #endregion Declarations
@@ -35,7 +35,7 @@ namespace VAR.Json
#region Private methods #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) private PropertyInfo[] Type_GetProperties(Type type)
{ {
@@ -497,14 +497,14 @@ namespace VAR.Json
else if (c == ',') else if (c == ',')
{ {
_ctx.Next(); _ctx.Next();
c = _ctx.SkipWhite(); _ctx.SkipWhite();
expectedKey = true; expectedKey = true;
expectedValue = false; expectedValue = false;
} }
else if (c == '}') else if (c == '}')
{ {
// StrictRules: Mark as tainted on unexpected end of object // StrictRules: Mark as tainted on unexpected end of object
if(expectedValue == true || expectedKey == true) if (expectedValue == true || expectedKey == true)
{ {
_tainted = true; _tainted = true;
} }
@@ -517,7 +517,7 @@ namespace VAR.Json
if (expectedKey != false) if (expectedKey != false)
{ {
attributeName = ParseString(true); attributeName = ParseString(true);
c = _ctx.SkipWhite(); _ctx.SkipWhite();
expectedKey = false; expectedKey = false;
expectedValue = true; expectedValue = true;
} }
@@ -538,8 +538,8 @@ namespace VAR.Json
private object ParseValue(int recusiveCount = 1) private object ParseValue(int recusiveCount = 1)
{ {
object token = null;
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
object token;
switch (c) switch (c)
{ {
case '"': case '"':
@@ -551,7 +551,7 @@ namespace VAR.Json
_tainted = true; _tainted = true;
token = ParseSingleQuotedString(); token = ParseSingleQuotedString();
break; break;
case '{': case '{':
Dictionary<string, object> obj = ParseObject(recusiveCount); Dictionary<string, object> obj = ParseObject(recusiveCount);
token = TryConvertToTypes(obj); token = TryConvertToTypes(obj);
@@ -597,28 +597,6 @@ namespace VAR.Json
return token; 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 #endregion Private methods
#region Public methods #region Public methods
@@ -648,6 +626,17 @@ namespace VAR.Json
return obj; 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 #endregion Public methods
} }
} }

View File

@@ -395,6 +395,33 @@ namespace VAR.Json
return sbOutput.ToString(); 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 #endregion Public methods
} }
} }