Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 00664880fb | |||
| 814800200f | |||
| 2f81aaa73c | |||
| c3f3049174 | |||
| 09bdde310d |
@@ -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)
|
||||
{
|
||||
_currentInstance = new JsonParser();
|
||||
}
|
||||
_currentInstance.KnownTypes.Clear();
|
||||
_currentInstance.KnownTypes.AddRange(knownTypes);
|
||||
return _currentInstance.Parse(text);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace VAR.Json
|
||||
{
|
||||
@@ -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;
|
||||
}
|
||||
@@ -101,52 +101,52 @@ namespace VAR.Json
|
||||
return false;
|
||||
}
|
||||
|
||||
private void WriteIndent(StringBuilder sbOutput, int level)
|
||||
private void WriteIndent(TextWriter textWriter, int level)
|
||||
{
|
||||
if (!_config.Indent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
sbOutput.Append('\n');
|
||||
textWriter.Write('\n');
|
||||
if (_config.UseTabForIndent)
|
||||
{
|
||||
for (int i = 0; i < level; i++) { sbOutput.Append('\t'); }
|
||||
for (int i = 0; i < level; i++) { textWriter.Write('\t'); }
|
||||
}
|
||||
else
|
||||
{
|
||||
int n = level * _config.IndentChars;
|
||||
for (int i = 0; i < n; i++) { sbOutput.Append(' '); }
|
||||
for (int i = 0; i < n; i++) { textWriter.Write(' '); }
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteString(StringBuilder sbOutput, string str)
|
||||
private void WriteString(TextWriter textWriter, string str)
|
||||
{
|
||||
sbOutput.Append('"');
|
||||
textWriter.Write('"');
|
||||
char c;
|
||||
int n = str.Length;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
c = str[i];
|
||||
if (c == '"') { sbOutput.Append("\\\""); }
|
||||
else if (c == '\\') { sbOutput.Append("\\\\"); }
|
||||
else if (c == '/') { sbOutput.Append("\\/"); }
|
||||
else if (c == '\b') { sbOutput.Append("\\b"); }
|
||||
else if (c == '\f') { sbOutput.Append("\\f"); }
|
||||
else if (c == '\n') { sbOutput.Append("\\n"); }
|
||||
else if (c == '\r') { sbOutput.Append("\\r"); }
|
||||
else if (c == '\t') { sbOutput.Append("\\t"); }
|
||||
else if (c < 32 || c >= 127) { sbOutput.AppendFormat("\\u{0:X04}", (int)c); }
|
||||
else { sbOutput.Append(c); }
|
||||
if (c == '"') { textWriter.Write("\\\""); }
|
||||
else if (c == '\\') { textWriter.Write("\\\\"); }
|
||||
else if (c == '/') { textWriter.Write("\\/"); }
|
||||
else if (c == '\b') { textWriter.Write("\\b"); }
|
||||
else if (c == '\f') { textWriter.Write("\\f"); }
|
||||
else if (c == '\n') { textWriter.Write("\\n"); }
|
||||
else if (c == '\r') { textWriter.Write("\\r"); }
|
||||
else if (c == '\t') { textWriter.Write("\\t"); }
|
||||
else if (c < 32 || c >= 127) { textWriter.Write("\\u{0:X04}", (int)c); }
|
||||
else { textWriter.Write(c); }
|
||||
}
|
||||
sbOutput.Append('"');
|
||||
textWriter.Write('"');
|
||||
}
|
||||
|
||||
private void WriteValue(StringBuilder sbOutput, object obj, List<object> parentLevels, bool useReflection)
|
||||
private void WriteValue(TextWriter textWriter, object obj, List<object> parentLevels, bool useReflection)
|
||||
{
|
||||
if (obj == null || obj is DBNull)
|
||||
{
|
||||
// NULL
|
||||
sbOutput.Append("null");
|
||||
textWriter.Write("null");
|
||||
}
|
||||
else if (
|
||||
(obj is float) ||
|
||||
@@ -157,50 +157,50 @@ namespace VAR.Json
|
||||
false)
|
||||
{
|
||||
// Numbers
|
||||
sbOutput.Append(obj.ToString());
|
||||
textWriter.Write(obj.ToString());
|
||||
}
|
||||
else if (obj is string)
|
||||
{
|
||||
// Strings
|
||||
WriteString(sbOutput, (string)obj);
|
||||
WriteString(textWriter, (string)obj);
|
||||
}
|
||||
else if (obj is bool)
|
||||
{
|
||||
// Booleans
|
||||
sbOutput.Append(((bool)obj) ? "true" : "false");
|
||||
textWriter.Write(((bool)obj) ? "true" : "false");
|
||||
}
|
||||
else if (obj is DateTime)
|
||||
{
|
||||
// DateTime
|
||||
sbOutput.Append('"');
|
||||
sbOutput.Append(((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ssZ"));
|
||||
sbOutput.Append('"');
|
||||
textWriter.Write('"');
|
||||
textWriter.Write(((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ssZ"));
|
||||
textWriter.Write('"');
|
||||
}
|
||||
else if (obj is IDictionary)
|
||||
{
|
||||
// Objects
|
||||
WriteObject(sbOutput, obj, parentLevels);
|
||||
WriteObject(textWriter, obj, parentLevels);
|
||||
}
|
||||
else if (obj is IEnumerable)
|
||||
{
|
||||
// Array/List
|
||||
WriteList(sbOutput, obj, parentLevels);
|
||||
WriteList(textWriter, obj, parentLevels);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (useReflection)
|
||||
{
|
||||
// Reflected object
|
||||
WriteReflectedObject(sbOutput, obj, parentLevels);
|
||||
WriteReflectedObject(textWriter, obj, parentLevels);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteString(sbOutput, Convert.ToString(obj));
|
||||
WriteString(textWriter, Convert.ToString(obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteList(StringBuilder sbOutput, object obj, List<object> parentLevels)
|
||||
private void WriteList(TextWriter textWriter, object obj, List<object> parentLevels)
|
||||
{
|
||||
IEnumerable list = (IEnumerable)obj;
|
||||
int n = 0;
|
||||
@@ -219,40 +219,40 @@ namespace VAR.Json
|
||||
// Empty
|
||||
if (n == 0)
|
||||
{
|
||||
sbOutput.Append("[ ]");
|
||||
textWriter.Write("[ ]");
|
||||
return;
|
||||
}
|
||||
|
||||
// Write array
|
||||
bool first = true;
|
||||
sbOutput.Append("[ ");
|
||||
textWriter.Write("[ ");
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||
}
|
||||
foreach (object childObj in list)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
sbOutput.Append(", ");
|
||||
textWriter.Write(", ");
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||
}
|
||||
}
|
||||
first = false;
|
||||
parentLevels.Add(obj);
|
||||
WriteValue(sbOutput, childObj, parentLevels, true);
|
||||
WriteValue(textWriter, childObj, parentLevels, true);
|
||||
parentLevels.Remove(obj);
|
||||
}
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count);
|
||||
WriteIndent(textWriter, parentLevels.Count);
|
||||
}
|
||||
sbOutput.Append(" ]");
|
||||
textWriter.Write(" ]");
|
||||
}
|
||||
|
||||
private void WriteObject(StringBuilder sbOutput, object obj, List<object> parentLevels)
|
||||
private void WriteObject(TextWriter textWriter, object obj, List<object> parentLevels)
|
||||
{
|
||||
IDictionary map = (IDictionary)obj;
|
||||
int n = map.Count;
|
||||
@@ -260,7 +260,7 @@ namespace VAR.Json
|
||||
// Empty
|
||||
if (map.Count == 0)
|
||||
{
|
||||
sbOutput.Append("{ }");
|
||||
textWriter.Write("{ }");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -277,37 +277,37 @@ namespace VAR.Json
|
||||
|
||||
// Write object
|
||||
bool first = true;
|
||||
sbOutput.Append("{ ");
|
||||
textWriter.Write("{ ");
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||
}
|
||||
foreach (object key in map.Keys)
|
||||
{
|
||||
object value = map[key];
|
||||
if (!first)
|
||||
{
|
||||
sbOutput.Append(", ");
|
||||
textWriter.Write(", ");
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||
}
|
||||
}
|
||||
first = false;
|
||||
WriteString(sbOutput, Convert.ToString(key));
|
||||
sbOutput.Append(": ");
|
||||
WriteString(textWriter, Convert.ToString(key));
|
||||
textWriter.Write(": ");
|
||||
parentLevels.Add(obj);
|
||||
WriteValue(sbOutput, value, parentLevels, true);
|
||||
WriteValue(textWriter, value, parentLevels, true);
|
||||
parentLevels.Remove(obj);
|
||||
}
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count);
|
||||
WriteIndent(textWriter, parentLevels.Count);
|
||||
}
|
||||
sbOutput.Append(" }");
|
||||
textWriter.Write(" }");
|
||||
}
|
||||
|
||||
private void WriteReflectedObject(StringBuilder sbOutput, object obj, List<object> parentLevels)
|
||||
private void WriteReflectedObject(TextWriter textWriter, object obj, List<object> parentLevels)
|
||||
{
|
||||
Type type = obj.GetType();
|
||||
PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
@@ -323,7 +323,7 @@ namespace VAR.Json
|
||||
// Empty
|
||||
if (n == 0)
|
||||
{
|
||||
sbOutput.Append("{ }");
|
||||
textWriter.Write("{ }");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -341,10 +341,10 @@ namespace VAR.Json
|
||||
|
||||
// Write object
|
||||
bool first = true;
|
||||
sbOutput.Append("{ ");
|
||||
textWriter.Write("{ ");
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||
}
|
||||
foreach (PropertyInfo property in properties)
|
||||
{
|
||||
@@ -357,69 +357,106 @@ namespace VAR.Json
|
||||
}
|
||||
if (!first)
|
||||
{
|
||||
sbOutput.Append(", ");
|
||||
textWriter.Write(", ");
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||
}
|
||||
}
|
||||
first = false;
|
||||
WriteString(sbOutput, property.Name);
|
||||
sbOutput.Append(": ");
|
||||
WriteString(textWriter, property.Name);
|
||||
textWriter.Write(": ");
|
||||
parentLevels.Add(obj);
|
||||
if (value != obj && parentLevels.Contains(value) == false)
|
||||
{
|
||||
WriteValue(sbOutput, value, parentLevels, false);
|
||||
WriteValue(textWriter, value, parentLevels, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteValue(sbOutput, null, parentLevels, false);
|
||||
WriteValue(textWriter, null, parentLevels, false);
|
||||
}
|
||||
parentLevels.Remove(obj);
|
||||
}
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count);
|
||||
WriteIndent(textWriter, parentLevels.Count);
|
||||
}
|
||||
sbOutput.Append(" }");
|
||||
textWriter.Write(" }");
|
||||
}
|
||||
|
||||
#endregion Private methods
|
||||
|
||||
#region Public methods
|
||||
|
||||
public TextWriter Write(object obj, TextWriter textWriter)
|
||||
{
|
||||
if (textWriter == null)
|
||||
{
|
||||
textWriter = new StringWriter();
|
||||
}
|
||||
WriteValue(textWriter, obj, new List<object>(), true);
|
||||
return textWriter;
|
||||
}
|
||||
|
||||
public string Write(object obj)
|
||||
{
|
||||
StringBuilder sbOutput = new StringBuilder();
|
||||
WriteValue(sbOutput, obj, new List<object>(), true);
|
||||
return sbOutput.ToString();
|
||||
StringWriter textWriter = new StringWriter();
|
||||
WriteValue(textWriter, obj, new List<object>(), true);
|
||||
return textWriter.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,
|
||||
JsonWriterConfiguration config = null,
|
||||
bool indent = false,
|
||||
bool useTablForIndent = false,
|
||||
bool useTabForIndent = false,
|
||||
int indentChars = 4,
|
||||
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,
|
||||
useTablForIndent: useTablForIndent,
|
||||
useTabForIndent: useTabForIndent,
|
||||
indentChars: indentChars,
|
||||
indentThresold: indentThresold));
|
||||
indentThresold: indentThresold);
|
||||
jsonWriter = new JsonWriter(jsonWriterConfiguration);
|
||||
_dictInstances.Add(jsonWriterConfiguration, jsonWriter);
|
||||
|
||||
return jsonWriter.Write(obj);
|
||||
}
|
||||
|
||||
#endregion Public methods
|
||||
|
||||
@@ -11,4 +11,4 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: Guid("28b3f937-145c-4fd4-a75b-a25ea4cc0428")]
|
||||
[assembly: AssemblyVersion("1.1.0.*")]
|
||||
[assembly: AssemblyVersion("1.1.1.*")]
|
||||
@@ -21,6 +21,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<LangVersion>6</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release .Net 4.6.1|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@@ -30,6 +31,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<LangVersion>6</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug .Net 3.5|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -40,6 +42,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<LangVersion>6</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release .Net 3.5|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@@ -49,6 +52,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<LangVersion>6</LangVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
|
||||
Reference in New Issue
Block a user