6 Commits
1_0 ... 1_1_0

6 changed files with 125 additions and 63 deletions

1
.gitignore vendored
View File

@@ -27,3 +27,4 @@ obj/
_ReSharper*/ _ReSharper*/
*.userprefs *.userprefs
*.nupkg *.nupkg
/.vs/*

View File

@@ -96,7 +96,7 @@ namespace VAR.Json.Tests
} }
if (obj != null && (obj is Exception) == false) if (obj != null && (obj is Exception) == false)
{ {
JsonWriter writter = new JsonWriter(true); JsonWriter writter = new JsonWriter(new JsonWriterConfiguration(indent: true));
Console.Out.WriteLine("Parsed:\n{0}", writter.Write(obj)); Console.Out.WriteLine("Parsed:\n{0}", writter.Write(obj));
Console.Out.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Console.Out.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} }

View File

@@ -15,7 +15,7 @@ namespace VAR.Json
private ParserContext _ctx; private ParserContext _ctx;
private bool _tainted = false; private bool _tainted = false;
private List<Type> _knownTypes = new List<Type>(); private readonly List<Type> _knownTypes = new List<Type>();
#endregion Declarations #endregion Declarations
@@ -35,7 +35,7 @@ namespace VAR.Json
#region Private methods #region Private methods
private static Dictionary<Type, PropertyInfo[]> _dictProperties = new Dictionary<Type, PropertyInfo[]>(); private static readonly Dictionary<Type, PropertyInfo[]> _dictProperties = new Dictionary<Type, PropertyInfo[]>();
private PropertyInfo[] Type_GetProperties(Type type) private PropertyInfo[] Type_GetProperties(Type type)
{ {
@@ -497,14 +497,14 @@ namespace VAR.Json
else if (c == ',') else if (c == ',')
{ {
_ctx.Next(); _ctx.Next();
c = _ctx.SkipWhite(); _ctx.SkipWhite();
expectedKey = true; expectedKey = true;
expectedValue = false; expectedValue = false;
} }
else if (c == '}') else if (c == '}')
{ {
// StrictRules: Mark as tainted on unexpected end of object // StrictRules: Mark as tainted on unexpected end of object
if(expectedValue == true || expectedKey == true) if (expectedValue == true || expectedKey == true)
{ {
_tainted = true; _tainted = true;
} }
@@ -517,7 +517,7 @@ namespace VAR.Json
if (expectedKey != false) if (expectedKey != false)
{ {
attributeName = ParseString(true); attributeName = ParseString(true);
c = _ctx.SkipWhite(); _ctx.SkipWhite();
expectedKey = false; expectedKey = false;
expectedValue = true; expectedValue = true;
} }
@@ -538,8 +538,8 @@ namespace VAR.Json
private object ParseValue(int recusiveCount = 1) private object ParseValue(int recusiveCount = 1)
{ {
object token = null;
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
object token;
switch (c) switch (c)
{ {
case '"': case '"':
@@ -551,7 +551,7 @@ namespace VAR.Json
_tainted = true; _tainted = true;
token = ParseSingleQuotedString(); token = ParseSingleQuotedString();
break; break;
case '{': case '{':
Dictionary<string, object> obj = ParseObject(recusiveCount); Dictionary<string, object> obj = ParseObject(recusiveCount);
token = TryConvertToTypes(obj); token = TryConvertToTypes(obj);
@@ -597,28 +597,6 @@ namespace VAR.Json
return token; return token;
} }
private string CleanIdentifier(string input)
{
int i;
char c;
i = input.Length - 1;
if (i < 0)
{
return input;
}
c = input[i];
while (char.IsLetter(c) || char.IsDigit(c) || c == '_')
{
i--;
if (i < 0)
{
break;
}
c = input[i];
}
return input.Substring(i + 1);
}
#endregion Private methods #endregion Private methods
#region Public methods #region Public methods
@@ -648,6 +626,17 @@ namespace VAR.Json
return obj; return obj;
} }
private static JsonParser _currentInstance = null;
public static object ParseText(string text)
{
if(_currentInstance == null)
{
_currentInstance = new JsonParser();
}
return _currentInstance.Parse(text);
}
#endregion Public methods #endregion Public methods
} }
} }

View File

@@ -6,34 +6,74 @@ using System.Text;
namespace VAR.Json namespace VAR.Json
{ {
public class JsonWriterConfiguration
{
private bool _indent;
public bool Indent { get { return _indent; } }
private bool _useTabForIndent;
public bool UseTabForIndent { get { return _useTabForIndent; } }
private int _indentChars;
public int IndentChars { get { return _indentChars; } }
private int _indentThresold;
public int IndentThresold { get { return _indentThresold; } }
public JsonWriterConfiguration(
bool indent = false,
bool useTablForIndent = false,
int indentChars = 4,
int indentThresold = 3)
{
_indent = indent;
_useTabForIndent = useTablForIndent;
_indentChars = indentChars;
_indentThresold = indentThresold;
}
public bool Equals(JsonWriterConfiguration other)
{
return
other.Indent == Indent &&
other.UseTabForIndent == UseTabForIndent &&
other.IndentChars == IndentChars &&
other.IndentThresold == IndentThresold &&
true;
}
public override bool Equals(object other)
{
if(other is JsonWriterConfiguration)
{
return Equals(other as JsonWriterConfiguration);
}
return false;
}
public override int GetHashCode()
{
return _indent.GetHashCode() ^ _useTabForIndent.GetHashCode() ^ _indentChars.GetHashCode() ^ _indentThresold.GetHashCode();
}
}
public class JsonWriter public class JsonWriter
{ {
#region Declarations #region Declarations
private bool _indent = false; private JsonWriterConfiguration _config = null;
private bool _useTabForIndent = false;
private int _indentChars = 4;
private int _indentThresold = 3;
#endregion Declarations #endregion Declarations
#region Creator #region Creator
public JsonWriter() public JsonWriter(JsonWriterConfiguration config = null)
{ {
} _config = config;
if (_config == null)
public JsonWriter(int indentChars) {
{ _config = new JsonWriterConfiguration();
_indent = true; }
_indentChars = indentChars;
_useTabForIndent = false;
}
public JsonWriter(bool useTabForIndent)
{
_indent = true;
_useTabForIndent = useTabForIndent;
} }
#endregion Creator #endregion Creator
@@ -63,18 +103,18 @@ namespace VAR.Json
private void WriteIndent(StringBuilder sbOutput, int level) private void WriteIndent(StringBuilder sbOutput, int level)
{ {
if (!_indent) if (!_config.Indent)
{ {
return; return;
} }
sbOutput.Append('\n'); sbOutput.Append('\n');
if (_useTabForIndent) if (_config.UseTabForIndent)
{ {
for (int i = 0; i < level; i++) { sbOutput.Append('\t'); } for (int i = 0; i < level; i++) { sbOutput.Append('\t'); }
} }
else else
{ {
int n = level * _indentChars; int n = level * _config.IndentChars;
for (int i = 0; i < n; i++) { sbOutput.Append(' '); } for (int i = 0; i < n; i++) { sbOutput.Append(' '); }
} }
} }
@@ -186,7 +226,7 @@ namespace VAR.Json
// Write array // Write array
bool first = true; bool first = true;
sbOutput.Append("[ "); sbOutput.Append("[ ");
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(sbOutput, parentLevels.Count + 1);
} }
@@ -195,7 +235,7 @@ namespace VAR.Json
if (!first) if (!first)
{ {
sbOutput.Append(", "); sbOutput.Append(", ");
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(sbOutput, parentLevels.Count + 1);
} }
@@ -205,7 +245,7 @@ namespace VAR.Json
WriteValue(sbOutput, childObj, parentLevels, true); WriteValue(sbOutput, childObj, parentLevels, true);
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count); WriteIndent(sbOutput, parentLevels.Count);
} }
@@ -238,7 +278,7 @@ namespace VAR.Json
// Write object // Write object
bool first = true; bool first = true;
sbOutput.Append("{ "); sbOutput.Append("{ ");
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(sbOutput, parentLevels.Count + 1);
} }
@@ -248,7 +288,7 @@ namespace VAR.Json
if (!first) if (!first)
{ {
sbOutput.Append(", "); sbOutput.Append(", ");
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(sbOutput, parentLevels.Count + 1);
} }
@@ -260,7 +300,7 @@ namespace VAR.Json
WriteValue(sbOutput, value, parentLevels, true); WriteValue(sbOutput, value, parentLevels, true);
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count); WriteIndent(sbOutput, parentLevels.Count);
} }
@@ -302,7 +342,7 @@ namespace VAR.Json
// Write object // Write object
bool first = true; bool first = true;
sbOutput.Append("{ "); sbOutput.Append("{ ");
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(sbOutput, parentLevels.Count + 1);
} }
@@ -318,7 +358,7 @@ namespace VAR.Json
if (!first) if (!first)
{ {
sbOutput.Append(", "); sbOutput.Append(", ");
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count + 1); WriteIndent(sbOutput, parentLevels.Count + 1);
} }
@@ -337,7 +377,7 @@ namespace VAR.Json
} }
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _indentThresold) if (!isLeaf || n > _config.IndentThresold)
{ {
WriteIndent(sbOutput, parentLevels.Count); WriteIndent(sbOutput, parentLevels.Count);
} }
@@ -355,6 +395,33 @@ namespace VAR.Json
return sbOutput.ToString(); return sbOutput.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,
bool indent = false,
bool useTablForIndent = false,
int indentChars = 4,
int indentThresold = 3)
{
return WriteObject(obj, new JsonWriterConfiguration(
indent: indent,
useTablForIndent: useTablForIndent,
indentChars: indentChars,
indentThresold: indentThresold));
}
#endregion Public methods #endregion Public methods
} }
} }

View File

@@ -6,7 +6,7 @@ namespace VAR.Json
{ {
public class ObjectActivator public class ObjectActivator
{ {
private static Dictionary<Type, Func<object>> _creators = new Dictionary<Type, Func<object>>(); private static readonly Dictionary<Type, Func<object>> _creators = new Dictionary<Type, Func<object>>();
public static Func<object> GetLambdaNew(Type type) public static Func<object> GetLambdaNew(Type type)
{ {
@@ -17,13 +17,18 @@ namespace VAR.Json
lock (_creators) lock (_creators)
{ {
if (_creators.ContainsKey(type))
{
return _creators[type];
}
NewExpression newExp = Expression.New(type); NewExpression newExp = Expression.New(type);
LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp); LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp);
Func<object> compiledLambdaNew = (Func<object>)lambda.Compile(); Func<object> compiledLambdaNew = (Func<object>)lambda.Compile();
_creators.Add(type, compiledLambdaNew); _creators.Add(type, compiledLambdaNew);
return _creators[type];
} }
return _creators[type];
} }
public static object CreateInstance(Type type) public static object CreateInstance(Type type)

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.0.*")] [assembly: AssemblyVersion("1.1.0.*")]