diff --git a/VAR.Json/JsonParser.cs b/VAR.Json/JsonParser.cs index 183d1b4..cebbb47 100644 --- a/VAR.Json/JsonParser.cs +++ b/VAR.Json/JsonParser.cs @@ -628,12 +628,14 @@ namespace VAR.Json private static JsonParser _currentInstance = null; - public static object ParseText(string text) + public static object ParseText(string text, params Type[] knownTypes) { - if(_currentInstance == null) + if (_currentInstance == null) { _currentInstance = new JsonParser(); } + _currentInstance.KnownTypes.Clear(); + _currentInstance.KnownTypes.AddRange(knownTypes); return _currentInstance.Parse(text); } diff --git a/VAR.Json/JsonWriter.cs b/VAR.Json/JsonWriter.cs index 0bb8c82..4775d26 100644 --- a/VAR.Json/JsonWriter.cs +++ b/VAR.Json/JsonWriter.cs @@ -22,12 +22,12 @@ namespace VAR.Json public JsonWriterConfiguration( bool indent = false, - bool useTablForIndent = false, + bool useTabForIndent = false, int indentChars = 4, int indentThresold = 3) { _indent = indent; - _useTabForIndent = useTablForIndent; + _useTabForIndent = useTabForIndent; _indentChars = indentChars; _indentThresold = indentThresold; } @@ -397,29 +397,56 @@ namespace VAR.Json 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, + JsonWriterConfiguration config = null, bool indent = false, - bool useTablForIndent = false, + bool useTabForIndent = false, int indentChars = 4, int indentThresold = 3) { - return WriteObject(obj, new JsonWriterConfiguration( - indent: indent, - useTablForIndent: useTablForIndent, - indentChars: indentChars, - indentThresold: indentThresold)); + JsonWriter jsonWriter = null; + + if (config != null) + { + if (_dictInstances.ContainsKey(config) == false) + { + jsonWriter = new JsonWriter(config); + _dictInstances.Add(config, jsonWriter); + } + else + { + jsonWriter = _dictInstances[config]; + } + return jsonWriter.Write(obj); + } + + foreach (KeyValuePair pair in _dictInstances) + { + if ( + pair.Key.Indent == indent && + pair.Key.UseTabForIndent == useTabForIndent && + pair.Key.IndentChars == indentChars && + pair.Key.IndentThresold == indentThresold && + true) + { + jsonWriter = pair.Value; + break; + } + } + if (jsonWriter != null) + { + return jsonWriter.Write(obj); + } + + JsonWriterConfiguration jsonWriterConfiguration = new JsonWriterConfiguration( + indent: indent, + useTabForIndent: useTabForIndent, + indentChars: indentChars, + indentThresold: indentThresold); + jsonWriter = new JsonWriter(jsonWriterConfiguration); + _dictInstances.Add(jsonWriterConfiguration, jsonWriter); + + return jsonWriter.Write(obj); } #endregion Public methods