JsonParser and JsonWriter: Easier usage of the static methods.
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
indent: indent,
|
|
||||||
useTablForIndent: useTablForIndent,
|
if (config != null)
|
||||||
indentChars: indentChars,
|
{
|
||||||
indentThresold: indentThresold));
|
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,
|
||||||
|
useTabForIndent: useTabForIndent,
|
||||||
|
indentChars: indentChars,
|
||||||
|
indentThresold: indentThresold);
|
||||||
|
jsonWriter = new JsonWriter(jsonWriterConfiguration);
|
||||||
|
_dictInstances.Add(jsonWriterConfiguration, jsonWriter);
|
||||||
|
|
||||||
|
return jsonWriter.Write(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Public methods
|
#endregion Public methods
|
||||||
|
|||||||
Reference in New Issue
Block a user