5 Commits
1_1_0 ... 1_1_1

4 changed files with 132 additions and 89 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

@@ -1,8 +1,8 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Reflection; using System.Reflection;
using System.Text;
namespace VAR.Json namespace VAR.Json
{ {
@@ -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;
} }
@@ -44,7 +44,7 @@ namespace VAR.Json
public override bool Equals(object other) public override bool Equals(object other)
{ {
if(other is JsonWriterConfiguration) if (other is JsonWriterConfiguration)
{ {
return Equals(other as JsonWriterConfiguration); return Equals(other as JsonWriterConfiguration);
} }
@@ -101,52 +101,52 @@ namespace VAR.Json
return false; return false;
} }
private void WriteIndent(StringBuilder sbOutput, int level) private void WriteIndent(TextWriter textWriter, int level)
{ {
if (!_config.Indent) if (!_config.Indent)
{ {
return; return;
} }
sbOutput.Append('\n'); textWriter.Write('\n');
if (_config.UseTabForIndent) if (_config.UseTabForIndent)
{ {
for (int i = 0; i < level; i++) { sbOutput.Append('\t'); } for (int i = 0; i < level; i++) { textWriter.Write('\t'); }
} }
else else
{ {
int n = level * _config.IndentChars; 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; char c;
int n = str.Length; int n = str.Length;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
c = str[i]; c = str[i];
if (c == '"') { sbOutput.Append("\\\""); } if (c == '"') { textWriter.Write("\\\""); }
else if (c == '\\') { sbOutput.Append("\\\\"); } else if (c == '\\') { textWriter.Write("\\\\"); }
else if (c == '/') { sbOutput.Append("\\/"); } else if (c == '/') { textWriter.Write("\\/"); }
else if (c == '\b') { sbOutput.Append("\\b"); } else if (c == '\b') { textWriter.Write("\\b"); }
else if (c == '\f') { sbOutput.Append("\\f"); } else if (c == '\f') { textWriter.Write("\\f"); }
else if (c == '\n') { sbOutput.Append("\\n"); } else if (c == '\n') { textWriter.Write("\\n"); }
else if (c == '\r') { sbOutput.Append("\\r"); } else if (c == '\r') { textWriter.Write("\\r"); }
else if (c == '\t') { sbOutput.Append("\\t"); } else if (c == '\t') { textWriter.Write("\\t"); }
else if (c < 32 || c >= 127) { sbOutput.AppendFormat("\\u{0:X04}", (int)c); } else if (c < 32 || c >= 127) { textWriter.Write("\\u{0:X04}", (int)c); }
else { sbOutput.Append(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) if (obj == null || obj is DBNull)
{ {
// NULL // NULL
sbOutput.Append("null"); textWriter.Write("null");
} }
else if ( else if (
(obj is float) || (obj is float) ||
@@ -157,50 +157,50 @@ namespace VAR.Json
false) false)
{ {
// Numbers // Numbers
sbOutput.Append(obj.ToString()); textWriter.Write(obj.ToString());
} }
else if (obj is string) else if (obj is string)
{ {
// Strings // Strings
WriteString(sbOutput, (string)obj); WriteString(textWriter, (string)obj);
} }
else if (obj is bool) else if (obj is bool)
{ {
// Booleans // Booleans
sbOutput.Append(((bool)obj) ? "true" : "false"); textWriter.Write(((bool)obj) ? "true" : "false");
} }
else if (obj is DateTime) else if (obj is DateTime)
{ {
// DateTime // DateTime
sbOutput.Append('"'); textWriter.Write('"');
sbOutput.Append(((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ssZ")); textWriter.Write(((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ssZ"));
sbOutput.Append('"'); textWriter.Write('"');
} }
else if (obj is IDictionary) else if (obj is IDictionary)
{ {
// Objects // Objects
WriteObject(sbOutput, obj, parentLevels); WriteObject(textWriter, obj, parentLevels);
} }
else if (obj is IEnumerable) else if (obj is IEnumerable)
{ {
// Array/List // Array/List
WriteList(sbOutput, obj, parentLevels); WriteList(textWriter, obj, parentLevels);
} }
else else
{ {
if (useReflection) if (useReflection)
{ {
// Reflected object // Reflected object
WriteReflectedObject(sbOutput, obj, parentLevels); WriteReflectedObject(textWriter, obj, parentLevels);
} }
else 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; IEnumerable list = (IEnumerable)obj;
int n = 0; int n = 0;
@@ -219,40 +219,40 @@ namespace VAR.Json
// Empty // Empty
if (n == 0) if (n == 0)
{ {
sbOutput.Append("[ ]"); textWriter.Write("[ ]");
return; return;
} }
// Write array // Write array
bool first = true; bool first = true;
sbOutput.Append("[ "); textWriter.Write("[ ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
foreach (object childObj in list) foreach (object childObj in list)
{ {
if (!first) if (!first)
{ {
sbOutput.Append(", "); textWriter.Write(", ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
} }
first = false; first = false;
parentLevels.Add(obj); parentLevels.Add(obj);
WriteValue(sbOutput, childObj, parentLevels, true); WriteValue(textWriter, childObj, parentLevels, true);
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _config.IndentThresold) 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; IDictionary map = (IDictionary)obj;
int n = map.Count; int n = map.Count;
@@ -260,7 +260,7 @@ namespace VAR.Json
// Empty // Empty
if (map.Count == 0) if (map.Count == 0)
{ {
sbOutput.Append("{ }"); textWriter.Write("{ }");
return; return;
} }
@@ -277,37 +277,37 @@ namespace VAR.Json
// Write object // Write object
bool first = true; bool first = true;
sbOutput.Append("{ "); textWriter.Write("{ ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
foreach (object key in map.Keys) foreach (object key in map.Keys)
{ {
object value = map[key]; object value = map[key];
if (!first) if (!first)
{ {
sbOutput.Append(", "); textWriter.Write(", ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
} }
first = false; first = false;
WriteString(sbOutput, Convert.ToString(key)); WriteString(textWriter, Convert.ToString(key));
sbOutput.Append(": "); textWriter.Write(": ");
parentLevels.Add(obj); parentLevels.Add(obj);
WriteValue(sbOutput, value, parentLevels, true); WriteValue(textWriter, value, parentLevels, true);
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _config.IndentThresold) 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(); Type type = obj.GetType();
PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
@@ -323,7 +323,7 @@ namespace VAR.Json
// Empty // Empty
if (n == 0) if (n == 0)
{ {
sbOutput.Append("{ }"); textWriter.Write("{ }");
return; return;
} }
@@ -341,10 +341,10 @@ namespace VAR.Json
// Write object // Write object
bool first = true; bool first = true;
sbOutput.Append("{ "); textWriter.Write("{ ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
foreach (PropertyInfo property in properties) foreach (PropertyInfo property in properties)
{ {
@@ -357,69 +357,106 @@ namespace VAR.Json
} }
if (!first) if (!first)
{ {
sbOutput.Append(", "); textWriter.Write(", ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
} }
first = false; first = false;
WriteString(sbOutput, property.Name); WriteString(textWriter, property.Name);
sbOutput.Append(": "); textWriter.Write(": ");
parentLevels.Add(obj); parentLevels.Add(obj);
if (value != obj && parentLevels.Contains(value) == false) if (value != obj && parentLevels.Contains(value) == false)
{ {
WriteValue(sbOutput, value, parentLevels, false); WriteValue(textWriter, value, parentLevels, false);
} }
else else
{ {
WriteValue(sbOutput, null, parentLevels, false); WriteValue(textWriter, null, parentLevels, false);
} }
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count); WriteIndent(textWriter, parentLevels.Count);
} }
sbOutput.Append(" }"); textWriter.Write(" }");
} }
#endregion Private methods #endregion Private methods
#region Public 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) public string Write(object obj)
{ {
StringBuilder sbOutput = new StringBuilder(); StringWriter textWriter = new StringWriter();
WriteValue(sbOutput, obj, new List<object>(), true); WriteValue(textWriter, obj, new List<object>(), true);
return sbOutput.ToString(); return textWriter.ToString();
} }
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

View File

@@ -11,4 +11,4 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
[assembly: Guid("28b3f937-145c-4fd4-a75b-a25ea4cc0428")] [assembly: Guid("28b3f937-145c-4fd4-a75b-a25ea4cc0428")]
[assembly: AssemblyVersion("1.1.0.*")] [assembly: AssemblyVersion("1.1.1.*")]

View File

@@ -21,6 +21,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<LangVersion>6</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release .Net 4.6.1|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release .Net 4.6.1|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
@@ -30,6 +31,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<LangVersion>6</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug .Net 3.5|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug .Net 3.5|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@@ -40,6 +42,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<LangVersion>6</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release .Net 3.5|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release .Net 3.5|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
@@ -49,6 +52,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<LangVersion>6</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />