JsonParser and JsonWriter: Easier usage of the static methods.

This commit is contained in:
2019-11-24 13:52:51 +01:00
parent c3f3049174
commit 2f81aaa73c
2 changed files with 51 additions and 22 deletions

View File

@@ -628,12 +628,14 @@ namespace VAR.Json
private static JsonParser _currentInstance = null; 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 = new JsonParser();
} }
_currentInstance.KnownTypes.Clear();
_currentInstance.KnownTypes.AddRange(knownTypes);
return _currentInstance.Parse(text); return _currentInstance.Parse(text);
} }

View File

@@ -22,12 +22,12 @@ namespace VAR.Json
public JsonWriterConfiguration( public JsonWriterConfiguration(
bool indent = false, bool indent = false,
bool useTablForIndent = false, bool useTabForIndent = false,
int indentChars = 4, int indentChars = 4,
int indentThresold = 3) int indentThresold = 3)
{ {
_indent = indent; _indent = indent;
_useTabForIndent = useTablForIndent; _useTabForIndent = useTabForIndent;
_indentChars = indentChars; _indentChars = indentChars;
_indentThresold = indentThresold; _indentThresold = indentThresold;
} }
@@ -397,29 +397,56 @@ namespace VAR.Json
private static Dictionary<JsonWriterConfiguration, JsonWriter> _dictInstances = new Dictionary<JsonWriterConfiguration, JsonWriter>(); 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, public static string WriteObject(object obj,
JsonWriterConfiguration config = null,
bool indent = false, bool indent = false,
bool useTablForIndent = false, bool useTabForIndent = false,
int indentChars = 4, int indentChars = 4,
int indentThresold = 3) int indentThresold = 3)
{ {
return WriteObject(obj, new JsonWriterConfiguration( 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<JsonWriterConfiguration, JsonWriter> 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, indent: indent,
useTablForIndent: useTablForIndent, useTabForIndent: useTabForIndent,
indentChars: indentChars, indentChars: indentChars,
indentThresold: indentThresold)); indentThresold: indentThresold);
jsonWriter = new JsonWriter(jsonWriterConfiguration);
_dictInstances.Add(jsonWriterConfiguration, jsonWriter);
return jsonWriter.Write(obj);
} }
#endregion Public methods #endregion Public methods