From 53a947db75870401f93c5973c1080d233fa99b16 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 24 Nov 2019 04:56:57 +0100 Subject: [PATCH] JsonWriter and JsonParser: Static methods for easier usage. --- VAR.Json/JsonParser.cs | 47 ++++++++++++++++-------------------------- VAR.Json/JsonWriter.cs | 27 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/VAR.Json/JsonParser.cs b/VAR.Json/JsonParser.cs index eae390a..183d1b4 100644 --- a/VAR.Json/JsonParser.cs +++ b/VAR.Json/JsonParser.cs @@ -15,7 +15,7 @@ namespace VAR.Json private ParserContext _ctx; private bool _tainted = false; - private List _knownTypes = new List(); + private readonly List _knownTypes = new List(); #endregion Declarations @@ -35,7 +35,7 @@ namespace VAR.Json #region Private methods - private static Dictionary _dictProperties = new Dictionary(); + private static readonly Dictionary _dictProperties = new Dictionary(); private PropertyInfo[] Type_GetProperties(Type type) { @@ -497,14 +497,14 @@ namespace VAR.Json else if (c == ',') { _ctx.Next(); - c = _ctx.SkipWhite(); + _ctx.SkipWhite(); expectedKey = true; expectedValue = false; } else if (c == '}') { // StrictRules: Mark as tainted on unexpected end of object - if(expectedValue == true || expectedKey == true) + if (expectedValue == true || expectedKey == true) { _tainted = true; } @@ -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 '"': @@ -551,7 +551,7 @@ namespace VAR.Json _tainted = true; token = ParseSingleQuotedString(); break; - + case '{': Dictionary obj = ParseObject(recusiveCount); token = TryConvertToTypes(obj); @@ -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 } } \ No newline at end of file diff --git a/VAR.Json/JsonWriter.cs b/VAR.Json/JsonWriter.cs index 6e0d1e6..7919230 100644 --- a/VAR.Json/JsonWriter.cs +++ b/VAR.Json/JsonWriter.cs @@ -395,6 +395,33 @@ namespace VAR.Json return sbOutput.ToString(); } + private static Dictionary _dictInstances = new Dictionary(); + + 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 } } \ No newline at end of file