JsonWriter: Use custom configuration object for explicit configurabillity.
This commit is contained in:
@@ -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("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,34 +6,74 @@ using System.Text;
|
|||||||
|
|
||||||
namespace VAR.Json
|
namespace VAR.Json
|
||||||
{
|
{
|
||||||
|
public class JsonWriterConfiguration
|
||||||
|
{
|
||||||
|
private bool _indent;
|
||||||
|
public bool Indent { get => _indent; }
|
||||||
|
|
||||||
|
private bool _useTabForIndent;
|
||||||
|
public bool UseTabForIndent { get => _useTabForIndent; }
|
||||||
|
|
||||||
|
private int _indentChars;
|
||||||
|
public int IndentChars { get => _indentChars; }
|
||||||
|
|
||||||
|
private int _indentThresold;
|
||||||
|
public int IndentThresold { get => _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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user