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

View File

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