ref, and change project settings

This commit is contained in:
Philip Schell 2019-07-24 13:00:06 +02:00
parent 59b7ead200
commit aae12f70d3
2 changed files with 112 additions and 162 deletions

View File

@ -18,8 +18,7 @@ using System.Text;
namespace LitJson namespace LitJson
{ {
internal enum Condition internal enum Condition {
{
InArray, InArray,
InObject, InObject,
NotAProperty, NotAProperty,
@ -27,8 +26,7 @@ namespace LitJson
Value Value
} }
internal class WriterContext internal class WriterContext {
{
public Int32 Count; public Int32 Count;
public Boolean InArray; public Boolean InArray;
public Boolean InObject; public Boolean InObject;
@ -46,121 +44,90 @@ namespace LitJson
private Char[] hex_seq; private Char[] hex_seq;
private Int32 indentation; private Int32 indentation;
private Int32 indent_value; private Int32 indent_value;
private StringBuilder inst_string_builder; private readonly StringBuilder inst_string_builder;
private Boolean pretty_print;
private Boolean validate;
private Boolean lower_case_properties;
private TextWriter writer;
#endregion #endregion
#region Properties #region Properties
public Int32 IndentValue { public Int32 IndentValue {
get { return this.indent_value; } get => this.indent_value;
set { set {
this.indentation = (this.indentation / this.indent_value) * value; this.indentation = (this.indentation / this.indent_value) * value;
this.indent_value = value; this.indent_value = value;
} }
} }
public Boolean PrettyPrint { public Boolean PrettyPrint { get; set; }
get { return this.pretty_print; }
set { this.pretty_print = value; }
}
public TextWriter TextWriter { public TextWriter TextWriter { get; }
get { return this.writer; }
}
public Boolean Validate { public Boolean Validate { get; set; }
get { return this.validate; }
set { this.validate = value; }
}
public Boolean LowerCaseProperties { public Boolean LowerCaseProperties { get; set; }
get { return this.lower_case_properties; }
set { this.lower_case_properties = value; }
}
#endregion #endregion
#region Constructors #region Constructors
static JsonWriter() { static JsonWriter() => number_format = NumberFormatInfo.InvariantInfo;
number_format = NumberFormatInfo.InvariantInfo;
}
public JsonWriter() { public JsonWriter() {
this.inst_string_builder = new StringBuilder(); this.inst_string_builder = new StringBuilder();
this.writer = new StringWriter(this.inst_string_builder); this.TextWriter = new StringWriter(this.inst_string_builder);
this.Init();
Init();
} }
public JsonWriter(StringBuilder sb) : public JsonWriter(StringBuilder sb) : this(new StringWriter(sb)) { }
this(new StringWriter(sb)) {
}
public JsonWriter(TextWriter writer) { public JsonWriter(TextWriter writer) {
this.writer = writer ?? throw new ArgumentNullException("writer"); this.TextWriter = writer ?? throw new ArgumentNullException("writer");
this.Init();
Init();
} }
#endregion #endregion
#region Private Methods #region Private Methods
private void DoValidation(Condition cond) { private void DoValidation(Condition cond) {
if (!this.context.ExpectingValue) { if (!this.context.ExpectingValue) {
this.context.Count++; this.context.Count++;
} }
if (!this.validate) { if (!this.Validate) {
return; return;
} }
if (this.has_reached_end) { if (this.has_reached_end) {
throw new JsonException( throw new JsonException("A complete JSON symbol has already been written");
"A complete JSON symbol has already been written");
} }
switch (cond) { switch (cond) {
case Condition.InArray: case Condition.InArray:
if (!this.context.InArray) { if (!this.context.InArray) {
throw new JsonException( throw new JsonException("Can't close an array here");
"Can't close an array here");
} }
break; break;
case Condition.InObject: case Condition.InObject:
if (!this.context.InObject || this.context.ExpectingValue) { if (!this.context.InObject || this.context.ExpectingValue) {
throw new JsonException( throw new JsonException("Can't close an object here");
"Can't close an object here");
} }
break; break;
case Condition.NotAProperty: case Condition.NotAProperty:
if (this.context.InObject && !this.context.ExpectingValue) { if (this.context.InObject && !this.context.ExpectingValue) {
throw new JsonException( throw new JsonException("Expected a property");
"Expected a property");
} }
break; break;
case Condition.Property: case Condition.Property:
if (!this.context.InObject || this.context.ExpectingValue) { if (!this.context.InObject || this.context.ExpectingValue) {
throw new JsonException( throw new JsonException("Can't add a property here");
"Can't add a property here");
} }
break; break;
case Condition.Value: case Condition.Value:
if (!this.context.InArray && if (!this.context.InArray && (!this.context.InObject || !this.context.ExpectingValue)) {
(!this.context.InObject || !this.context.ExpectingValue)) { throw new JsonException("Can't add a value here");
throw new JsonException(
"Can't add a value here");
} }
break; break;
@ -172,9 +139,9 @@ namespace LitJson
this.hex_seq = new Char[4]; this.hex_seq = new Char[4];
this.indentation = 0; this.indentation = 0;
this.indent_value = 4; this.indent_value = 4;
this.pretty_print = false; this.PrettyPrint = false;
this.validate = true; this.Validate = true;
this.lower_case_properties = false; this.LowerCaseProperties = false;
this.ctx_stack = new Stack<WriterContext>(); this.ctx_stack = new Stack<WriterContext>();
this.context = new WriterContext(); this.context = new WriterContext();
@ -183,114 +150,96 @@ namespace LitJson
private static void IntToHex(Int32 n, Char[] hex) { private static void IntToHex(Int32 n, Char[] hex) {
Int32 num; Int32 num;
for (Int32 i = 0; i < 4; i++) { for (Int32 i = 0; i < 4; i++) {
num = n % 16; num = n % 16;
hex[3 - i] = num < 10 ? (Char)('0' + num) : (Char)('A' + (num - 10));
if (num < 10) {
hex[3 - i] = (Char)('0' + num);
} else {
hex[3 - i] = (Char)('A' + (num - 10));
}
n >>= 4; n >>= 4;
} }
} }
private void Indent() { private void Indent() {
if (this.pretty_print) { if (this.PrettyPrint) {
this.indentation += this.indent_value; this.indentation += this.indent_value;
} }
} }
private void Put(String str) { private void Put(String str) {
if (this.pretty_print && !this.context.ExpectingValue) { if (this.PrettyPrint && !this.context.ExpectingValue) {
for (Int32 i = 0; i < this.indentation; i++) { for (Int32 i = 0; i < this.indentation; i++) {
this.writer.Write(' '); this.TextWriter.Write(' ');
} }
} }
this.TextWriter.Write(str);
}
this.writer.Write(str); private void PutNewline() => this.PutNewline(true);
}
private void PutNewline() {
PutNewline(true);
}
private void PutNewline(Boolean add_comma) { private void PutNewline(Boolean add_comma) {
if (add_comma && !this.context.ExpectingValue && if (add_comma && !this.context.ExpectingValue && this.context.Count > 1) {
this.context.Count > 1) { this.TextWriter.Write(',');
this.writer.Write(',');
} }
if (this.PrettyPrint && !this.context.ExpectingValue) {
if (this.pretty_print && !this.context.ExpectingValue) { this.TextWriter.Write(Environment.NewLine);
this.writer.Write(Environment.NewLine);
} }
} }
private void PutString(String str) { private void PutString(String str) {
Put(String.Empty); this.Put(String.Empty);
this.writer.Write('"'); this.TextWriter.Write('"');
Int32 n = str.Length; Int32 n = str.Length;
for (Int32 i = 0; i < n; i++) { for (Int32 i = 0; i < n; i++) {
switch (str[i]) { switch (str[i]) {
case '\n': case '\n':
this.writer.Write("\\n"); this.TextWriter.Write("\\n");
continue; continue;
case '\r': case '\r':
this.writer.Write("\\r"); this.TextWriter.Write("\\r");
continue; continue;
case '\t': case '\t':
this.writer.Write("\\t"); this.TextWriter.Write("\\t");
continue; continue;
case '"': case '"':
case '\\': case '\\':
this.writer.Write('\\'); this.TextWriter.Write('\\');
this.writer.Write(str[i]); this.TextWriter.Write(str[i]);
continue; continue;
case '\f': case '\f':
this.writer.Write("\\f"); this.TextWriter.Write("\\f");
continue; continue;
case '\b': case '\b':
this.writer.Write("\\b"); this.TextWriter.Write("\\b");
continue; continue;
} }
if (str[i] >= 32 && str[i] <= 126) { if (str[i] >= 32 && str[i] <= 126) {
this.writer.Write(str[i]); this.TextWriter.Write(str[i]);
continue; continue;
} }
// Default, turn into a \uXXXX sequence // Default, turn into a \uXXXX sequence
IntToHex(str[i], this.hex_seq); IntToHex(str[i], this.hex_seq);
this.writer.Write("\\u"); this.TextWriter.Write("\\u");
this.writer.Write(this.hex_seq); this.TextWriter.Write(this.hex_seq);
} }
this.writer.Write('"'); this.TextWriter.Write('"');
} }
private void Unindent() { private void Unindent() {
if (this.pretty_print) { if (this.PrettyPrint) {
this.indentation -= this.indent_value; this.indentation -= this.indent_value;
} }
} }
#endregion #endregion
public override String ToString() => this.inst_string_builder == null ? String.Empty : this.inst_string_builder.ToString();
public override String ToString() {
if (this.inst_string_builder == null) {
return String.Empty;
}
return this.inst_string_builder.ToString();
}
public void Reset() { public void Reset() {
this.has_reached_end = false; this.has_reached_end = false;
@ -305,81 +254,80 @@ namespace LitJson
} }
public void Write(Boolean boolean) { public void Write(Boolean boolean) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
Put(boolean ? "true" : "false"); this.Put(boolean ? "true" : "false");
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write(Decimal number) { public void Write(Decimal number) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
Put(Convert.ToString(number, number_format)); this.Put(Convert.ToString(number, number_format));
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write(Double number) { public void Write(Double number) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
String str = Convert.ToString(number, number_format); String str = Convert.ToString(number, number_format);
Put(str); this.Put(str);
if (str.IndexOf('.') == -1 && str.IndexOf('E') == -1) { if (str.IndexOf('.') == -1 && str.IndexOf('E') == -1) {
this.writer.Write(".0"); this.TextWriter.Write(".0");
} }
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write(Int32 number) { public void Write(Int32 number) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
Put(Convert.ToString(number, number_format)); this.Put(Convert.ToString(number, number_format));
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write(Int64 number) { public void Write(Int64 number) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
Put(Convert.ToString(number, number_format)); this.Put(Convert.ToString(number, number_format));
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write(String str) { public void Write(String str) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
if (str == null) { if (str == null) {
Put("null"); this.Put("null");
} else { } else {
PutString(str); this.PutString(str);
} }
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
// [CLSCompliant(false)]
public void Write(UInt64 number) { public void Write(UInt64 number) {
DoValidation(Condition.Value); this.DoValidation(Condition.Value);
PutNewline(); this.PutNewline();
Put(Convert.ToString(number, number_format)); this.Put(Convert.ToString(number, number_format));
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void WriteArrayEnd() { public void WriteArrayEnd() {
DoValidation(Condition.InArray); this.DoValidation(Condition.InArray);
PutNewline(false); this.PutNewline(false);
this.ctx_stack.Pop(); this.ctx_stack.Pop();
if (this.ctx_stack.Count == 1) { if (this.ctx_stack.Count == 1) {
@ -389,27 +337,27 @@ namespace LitJson
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
Unindent(); this.Unindent();
Put("]"); this.Put("]");
} }
public void WriteArrayStart() { public void WriteArrayStart() {
DoValidation(Condition.NotAProperty); this.DoValidation(Condition.NotAProperty);
PutNewline(); this.PutNewline();
Put("["); this.Put("[");
this.context = new WriterContext { this.context = new WriterContext {
InArray = true InArray = true
}; };
this.ctx_stack.Push(this.context); this.ctx_stack.Push(this.context);
Indent(); this.Indent();
} }
public void WriteObjectEnd() { public void WriteObjectEnd() {
DoValidation(Condition.InObject); this.DoValidation(Condition.InObject);
PutNewline(false); this.PutNewline(false);
this.ctx_stack.Pop(); this.ctx_stack.Pop();
if (this.ctx_stack.Count == 1) { if (this.ctx_stack.Count == 1) {
@ -419,44 +367,44 @@ namespace LitJson
this.context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
Unindent(); this.Unindent();
Put("}"); this.Put("}");
} }
public void WriteObjectStart() { public void WriteObjectStart() {
DoValidation(Condition.NotAProperty); this.DoValidation(Condition.NotAProperty);
PutNewline(); this.PutNewline();
Put("{"); this.Put("{");
this.context = new WriterContext { this.context = new WriterContext {
InObject = true InObject = true
}; };
this.ctx_stack.Push(this.context); this.ctx_stack.Push(this.context);
Indent(); this.Indent();
} }
public void WritePropertyName(String property_name) { public void WritePropertyName(String property_name) {
DoValidation(Condition.Property); this.DoValidation(Condition.Property);
PutNewline(); this.PutNewline();
String propertyName = (property_name == null || !this.lower_case_properties) ? property_name : property_name.ToLowerInvariant(); String propertyName = (property_name == null || !this.LowerCaseProperties) ? property_name : property_name.ToLowerInvariant();
PutString(propertyName); this.PutString(propertyName);
if (this.pretty_print) { if (this.PrettyPrint) {
if (propertyName.Length > this.context.Padding) { if (propertyName.Length > this.context.Padding) {
this.context.Padding = propertyName.Length; this.context.Padding = propertyName.Length;
} }
for (Int32 i = this.context.Padding - propertyName.Length; for (Int32 i = this.context.Padding - propertyName.Length;
i >= 0; i--) { i >= 0; i--) {
this.writer.Write(' '); this.TextWriter.Write(' ');
} }
this.writer.Write(": "); this.TextWriter.Write(": ");
} else { } else {
this.writer.Write(':'); this.TextWriter.Write(':');
} }
this.context.ExpectingValue = true; this.context.ExpectingValue = true;

View File

@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />