Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d02a598035 | |||
| 180a5f0065 | |||
| 53a947db75 | |||
| 6782200917 | |||
| 47f20e03e8 | |||
| 73ab9292e7 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,3 +27,4 @@ obj/
|
||||
_ReSharper*/
|
||||
*.userprefs
|
||||
*.nupkg
|
||||
/.vs/*
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace VAR.Json.Tests
|
||||
}
|
||||
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("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace VAR.Json
|
||||
private ParserContext _ctx;
|
||||
private bool _tainted = false;
|
||||
|
||||
private List<Type> _knownTypes = new List<Type>();
|
||||
private readonly List<Type> _knownTypes = new List<Type>();
|
||||
|
||||
#endregion Declarations
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace VAR.Json
|
||||
|
||||
#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)
|
||||
{
|
||||
@@ -497,14 +497,14 @@ namespace VAR.Json
|
||||
else if (c == ',')
|
||||
{
|
||||
_ctx.Next();
|
||||
c = _ctx.SkipWhite();
|
||||
_ctx.SkipWhite();
|
||||
expectedKey = true;
|
||||
expectedValue = false;
|
||||
}
|
||||
else if (c == '}')
|
||||
{
|
||||
// StrictRules: Mark as tainted on unexpected end of object
|
||||
if(expectedValue == true || expectedKey == true)
|
||||
if (expectedValue == true || expectedKey == true)
|
||||
{
|
||||
_tainted = true;
|
||||
}
|
||||
@@ -517,7 +517,7 @@ namespace VAR.Json
|
||||
if (expectedKey != false)
|
||||
{
|
||||
attributeName = ParseString(true);
|
||||
c = _ctx.SkipWhite();
|
||||
_ctx.SkipWhite();
|
||||
expectedKey = false;
|
||||
expectedValue = true;
|
||||
}
|
||||
@@ -538,8 +538,8 @@ namespace VAR.Json
|
||||
|
||||
private object ParseValue(int recusiveCount = 1)
|
||||
{
|
||||
object token = null;
|
||||
char c = _ctx.SkipWhite();
|
||||
object token;
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
@@ -597,28 +597,6 @@ namespace VAR.Json
|
||||
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
|
||||
|
||||
#region Public methods
|
||||
@@ -648,6 +626,17 @@ namespace VAR.Json
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -6,34 +6,74 @@ using System.Text;
|
||||
|
||||
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
|
||||
{
|
||||
#region Declarations
|
||||
|
||||
private bool _indent = false;
|
||||
private bool _useTabForIndent = false;
|
||||
private int _indentChars = 4;
|
||||
private int _indentThresold = 3;
|
||||
private JsonWriterConfiguration _config = null;
|
||||
|
||||
#endregion Declarations
|
||||
|
||||
#region Creator
|
||||
|
||||
public JsonWriter()
|
||||
public JsonWriter(JsonWriterConfiguration config = null)
|
||||
{
|
||||
}
|
||||
|
||||
public JsonWriter(int indentChars)
|
||||
{
|
||||
_indent = true;
|
||||
_indentChars = indentChars;
|
||||
_useTabForIndent = false;
|
||||
}
|
||||
|
||||
public JsonWriter(bool useTabForIndent)
|
||||
{
|
||||
_indent = true;
|
||||
_useTabForIndent = useTabForIndent;
|
||||
_config = config;
|
||||
if (_config == null)
|
||||
{
|
||||
_config = new JsonWriterConfiguration();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Creator
|
||||
@@ -63,18 +103,18 @@ namespace VAR.Json
|
||||
|
||||
private void WriteIndent(StringBuilder sbOutput, int level)
|
||||
{
|
||||
if (!_indent)
|
||||
if (!_config.Indent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
sbOutput.Append('\n');
|
||||
if (_useTabForIndent)
|
||||
if (_config.UseTabForIndent)
|
||||
{
|
||||
for (int i = 0; i < level; i++) { sbOutput.Append('\t'); }
|
||||
}
|
||||
else
|
||||
{
|
||||
int n = level * _indentChars;
|
||||
int n = level * _config.IndentChars;
|
||||
for (int i = 0; i < n; i++) { sbOutput.Append(' '); }
|
||||
}
|
||||
}
|
||||
@@ -186,7 +226,7 @@ namespace VAR.Json
|
||||
// Write array
|
||||
bool first = true;
|
||||
sbOutput.Append("[ ");
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
}
|
||||
@@ -195,7 +235,7 @@ namespace VAR.Json
|
||||
if (!first)
|
||||
{
|
||||
sbOutput.Append(", ");
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
}
|
||||
@@ -205,7 +245,7 @@ namespace VAR.Json
|
||||
WriteValue(sbOutput, childObj, parentLevels, true);
|
||||
parentLevels.Remove(obj);
|
||||
}
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count);
|
||||
}
|
||||
@@ -238,7 +278,7 @@ namespace VAR.Json
|
||||
// Write object
|
||||
bool first = true;
|
||||
sbOutput.Append("{ ");
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
}
|
||||
@@ -248,7 +288,7 @@ namespace VAR.Json
|
||||
if (!first)
|
||||
{
|
||||
sbOutput.Append(", ");
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
}
|
||||
@@ -260,7 +300,7 @@ namespace VAR.Json
|
||||
WriteValue(sbOutput, value, parentLevels, true);
|
||||
parentLevels.Remove(obj);
|
||||
}
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count);
|
||||
}
|
||||
@@ -302,7 +342,7 @@ namespace VAR.Json
|
||||
// Write object
|
||||
bool first = true;
|
||||
sbOutput.Append("{ ");
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
}
|
||||
@@ -318,7 +358,7 @@ namespace VAR.Json
|
||||
if (!first)
|
||||
{
|
||||
sbOutput.Append(", ");
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count + 1);
|
||||
}
|
||||
@@ -337,7 +377,7 @@ namespace VAR.Json
|
||||
}
|
||||
parentLevels.Remove(obj);
|
||||
}
|
||||
if (!isLeaf || n > _indentThresold)
|
||||
if (!isLeaf || n > _config.IndentThresold)
|
||||
{
|
||||
WriteIndent(sbOutput, parentLevels.Count);
|
||||
}
|
||||
@@ -355,6 +395,33 @@ namespace VAR.Json
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace VAR.Json
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -17,13 +17,18 @@ namespace VAR.Json
|
||||
|
||||
lock (_creators)
|
||||
{
|
||||
if (_creators.ContainsKey(type))
|
||||
{
|
||||
return _creators[type];
|
||||
}
|
||||
|
||||
NewExpression newExp = Expression.New(type);
|
||||
LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp);
|
||||
Func<object> compiledLambdaNew = (Func<object>)lambda.Compile();
|
||||
|
||||
_creators.Add(type, compiledLambdaNew);
|
||||
return _creators[type];
|
||||
}
|
||||
return _creators[type];
|
||||
}
|
||||
|
||||
public static object CreateInstance(Type type)
|
||||
|
||||
@@ -11,4 +11,4 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: Guid("28b3f937-145c-4fd4-a75b-a25ea4cc0428")]
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.1.0.*")]
|
||||
Reference in New Issue
Block a user