[BF] Fixing Senml

[NF] Senml now has a configure option to setup the guid
[NF] Zway-Bot now listen on /exit
[NF] Implment searchpath for Zway-Bot (/etc/zwaybot and %appdata%/zwaybot)
This commit is contained in:
BlubbFish 2018-05-07 16:52:24 +00:00
parent 6f29227da3
commit a2ec43ed33
2 changed files with 433 additions and 441 deletions

View File

@ -29,87 +29,79 @@ namespace LitJson
internal class WriterContext internal class WriterContext
{ {
public int Count; public Int32 Count;
public bool InArray; public Boolean InArray;
public bool InObject; public Boolean InObject;
public bool ExpectingValue; public Boolean ExpectingValue;
public int Padding; public Int32 Padding;
} }
public class JsonWriter public class JsonWriter {
{
#region Fields #region Fields
private static readonly NumberFormatInfo number_format; private static readonly NumberFormatInfo number_format;
private WriterContext context; private WriterContext context;
private Stack<WriterContext> ctx_stack; private Stack<WriterContext> ctx_stack;
private bool has_reached_end; private Boolean has_reached_end;
private char[] hex_seq; private Char[] hex_seq;
private int indentation; private Int32 indentation;
private int indent_value; private Int32 indent_value;
private StringBuilder inst_string_builder; private StringBuilder inst_string_builder;
private bool pretty_print; private Boolean pretty_print;
private bool validate; private Boolean validate;
private bool lower_case_properties; private Boolean lower_case_properties;
private TextWriter writer; private TextWriter writer;
#endregion #endregion
#region Properties #region Properties
public int IndentValue { public Int32 IndentValue {
get { return indent_value; } get { return this.indent_value; }
set { set {
indentation = (indentation / indent_value) * value; this.indentation = (this.indentation / this.indent_value) * value;
indent_value = value; this.indent_value = value;
} }
} }
public bool PrettyPrint { public Boolean PrettyPrint {
get { return pretty_print; } get { return this.pretty_print; }
set { pretty_print = value; } set { this.pretty_print = value; }
} }
public TextWriter TextWriter { public TextWriter TextWriter {
get { return writer; } get { return this.writer; }
} }
public bool Validate { public Boolean Validate {
get { return validate; } get { return this.validate; }
set { validate = value; } set { this.validate = value; }
} }
public bool LowerCaseProperties { public Boolean LowerCaseProperties {
get { return lower_case_properties; } get { return this.lower_case_properties; }
set { lower_case_properties = value; } 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();
inst_string_builder = new StringBuilder (); this.writer = new StringWriter(this.inst_string_builder);
writer = new StringWriter (inst_string_builder);
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");
if (writer == null)
throw new ArgumentNullException ("writer");
this.writer = writer;
Init(); Init();
} }
@ -117,357 +109,357 @@ namespace LitJson
#region Private Methods #region Private Methods
private void DoValidation (Condition cond) private void DoValidation(Condition cond) {
{ if (!this.context.ExpectingValue) {
if (! context.ExpectingValue) this.context.Count++;
context.Count++; }
if (! validate) if (!this.validate) {
return; return;
}
if (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 (! 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 (! context.InObject || 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 (context.InObject && ! 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 (! context.InObject || 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 (! context.InArray && if (!this.context.InArray &&
(! context.InObject || ! context.ExpectingValue)) (!this.context.InObject || !this.context.ExpectingValue)) {
throw new JsonException( throw new JsonException(
"Can't add a value here"); "Can't add a value here");
}
break; break;
} }
} }
private void Init () private void Init() {
{ this.has_reached_end = false;
has_reached_end = false; this.hex_seq = new Char[4];
hex_seq = new char[4]; this.indentation = 0;
indentation = 0; this.indent_value = 4;
indent_value = 4; this.pretty_print = false;
pretty_print = false; this.validate = true;
validate = true; this.lower_case_properties = false;
lower_case_properties = false;
ctx_stack = new Stack<WriterContext> (); this.ctx_stack = new Stack<WriterContext>();
context = new WriterContext (); this.context = new WriterContext();
ctx_stack.Push (context); this.ctx_stack.Push(this.context);
} }
private static void IntToHex (int n, char[] hex) private static void IntToHex(Int32 n, Char[] hex) {
{ Int32 num;
int num;
for (int i = 0; i < 4; i++) { for (Int32 i = 0; i < 4; i++) {
num = n % 16; num = n % 16;
if (num < 10) if (num < 10) {
hex[3 - i] = (char) ('0' + num); hex[3 - i] = (Char)('0' + num);
else } else {
hex[3 - i] = (char) ('A' + (num - 10)); hex[3 - i] = (Char)('A' + (num - 10));
}
n >>= 4; n >>= 4;
} }
} }
private void Indent () private void Indent() {
{ if (this.pretty_print) {
if (pretty_print) this.indentation += this.indent_value;
indentation += indent_value; }
} }
private void Put(String str) {
private void Put (string str) if (this.pretty_print && !this.context.ExpectingValue) {
{ for (Int32 i = 0; i < this.indentation; i++) {
if (pretty_print && ! context.ExpectingValue) this.writer.Write(' ');
for (int i = 0; i < indentation; i++) }
writer.Write (' ');
writer.Write (str);
} }
private void PutNewline () this.writer.Write(str);
{ }
private void PutNewline() {
PutNewline(true); PutNewline(true);
} }
private void PutNewline (bool add_comma) private void PutNewline(Boolean add_comma) {
{ if (add_comma && !this.context.ExpectingValue &&
if (add_comma && ! context.ExpectingValue && this.context.Count > 1) {
context.Count > 1) this.writer.Write(',');
writer.Write (',');
if (pretty_print && ! context.ExpectingValue)
writer.Write (Environment.NewLine);
} }
private void PutString (string str) if (this.pretty_print && !this.context.ExpectingValue) {
{ this.writer.Write(Environment.NewLine);
}
}
private void PutString(String str) {
Put(String.Empty); Put(String.Empty);
writer.Write ('"'); this.writer.Write('"');
int n = str.Length; Int32 n = str.Length;
for (int i = 0; i < n; i++) { for (Int32 i = 0; i < n; i++) {
switch (str[i]) { switch (str[i]) {
case '\n': case '\n':
writer.Write ("\\n"); this.writer.Write("\\n");
continue; continue;
case '\r': case '\r':
writer.Write ("\\r"); this.writer.Write("\\r");
continue; continue;
case '\t': case '\t':
writer.Write ("\\t"); this.writer.Write("\\t");
continue; continue;
case '"': case '"':
case '\\': case '\\':
writer.Write ('\\'); this.writer.Write('\\');
writer.Write (str[i]); this.writer.Write(str[i]);
continue; continue;
case '\f': case '\f':
writer.Write ("\\f"); this.writer.Write("\\f");
continue; continue;
case '\b': case '\b':
writer.Write ("\\b"); this.writer.Write("\\b");
continue; continue;
} }
if ((int) str[i] >= 32 && (int) str[i] <= 126) { if (str[i] >= 32 && str[i] <= 126) {
writer.Write (str[i]); this.writer.Write(str[i]);
continue; continue;
} }
// Default, turn into a \uXXXX sequence // Default, turn into a \uXXXX sequence
IntToHex ((int) str[i], hex_seq); IntToHex(str[i], this.hex_seq);
writer.Write ("\\u"); this.writer.Write("\\u");
writer.Write (hex_seq); this.writer.Write(this.hex_seq);
} }
writer.Write ('"'); this.writer.Write('"');
} }
private void Unindent () private void Unindent() {
{ if (this.pretty_print) {
if (pretty_print) this.indentation -= this.indent_value;
indentation -= indent_value; }
} }
#endregion #endregion
public override string ToString () public override String ToString() {
{ if (this.inst_string_builder == null) {
if (inst_string_builder == null)
return String.Empty; return String.Empty;
}
return inst_string_builder.ToString (); return this.inst_string_builder.ToString();
} }
public void Reset () public void Reset() {
{ this.has_reached_end = false;
has_reached_end = false;
ctx_stack.Clear (); this.ctx_stack.Clear();
context = new WriterContext (); this.context = new WriterContext();
ctx_stack.Push (context); this.ctx_stack.Push(this.context);
if (inst_string_builder != null) if (this.inst_string_builder != null) {
inst_string_builder.Remove (0, inst_string_builder.Length); this.inst_string_builder.Remove(0, this.inst_string_builder.Length);
}
} }
public void Write (bool boolean) public void Write(Boolean boolean) {
{
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
Put(boolean ? "true" : "false"); Put(boolean ? "true" : "false");
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write (decimal number) public void Write(Decimal number) {
{
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
Put(Convert.ToString(number, number_format)); Put(Convert.ToString(number, number_format));
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write (double number) public void Write(Double number) {
{
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
string str = Convert.ToString (number, number_format); String str = Convert.ToString(number, number_format);
Put(str); Put(str);
if (str.IndexOf ('.') == -1 && if (str.IndexOf('.') == -1 && str.IndexOf('E') == -1) {
str.IndexOf ('E') == -1) this.writer.Write(".0");
writer.Write (".0");
context.ExpectingValue = false;
} }
public void Write (int number) this.context.ExpectingValue = false;
{ }
public void Write(Int32 number) {
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
Put(Convert.ToString(number, number_format)); Put(Convert.ToString(number, number_format));
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write (long number) public void Write(Int64 number) {
{
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
Put(Convert.ToString(number, number_format)); Put(Convert.ToString(number, number_format));
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void Write (string str) public void Write(String str) {
{
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
if (str == null) if (str == null) {
Put("null"); Put("null");
else } else {
PutString(str); PutString(str);
context.ExpectingValue = false;
} }
[CLSCompliant(false)] this.context.ExpectingValue = false;
public void Write (ulong number) }
{
// [CLSCompliant(false)]
public void Write(UInt64 number) {
DoValidation(Condition.Value); DoValidation(Condition.Value);
PutNewline(); PutNewline();
Put(Convert.ToString(number, number_format)); Put(Convert.ToString(number, number_format));
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
public void WriteArrayEnd () public void WriteArrayEnd() {
{
DoValidation(Condition.InArray); DoValidation(Condition.InArray);
PutNewline(false); PutNewline(false);
ctx_stack.Pop (); this.ctx_stack.Pop();
if (ctx_stack.Count == 1) if (this.ctx_stack.Count == 1) {
has_reached_end = true; this.has_reached_end = true;
else { } else {
context = ctx_stack.Peek (); this.context = this.ctx_stack.Peek();
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
Unindent(); Unindent();
Put("]"); Put("]");
} }
public void WriteArrayStart () public void WriteArrayStart() {
{
DoValidation(Condition.NotAProperty); DoValidation(Condition.NotAProperty);
PutNewline(); PutNewline();
Put("["); Put("[");
context = new WriterContext (); this.context = new WriterContext {
context.InArray = true; InArray = true
ctx_stack.Push (context); };
this.ctx_stack.Push(this.context);
Indent(); Indent();
} }
public void WriteObjectEnd () public void WriteObjectEnd() {
{
DoValidation(Condition.InObject); DoValidation(Condition.InObject);
PutNewline(false); PutNewline(false);
ctx_stack.Pop (); this.ctx_stack.Pop();
if (ctx_stack.Count == 1) if (this.ctx_stack.Count == 1) {
has_reached_end = true; this.has_reached_end = true;
else { } else {
context = ctx_stack.Peek (); this.context = this.ctx_stack.Peek();
context.ExpectingValue = false; this.context.ExpectingValue = false;
} }
Unindent(); Unindent();
Put("}"); Put("}");
} }
public void WriteObjectStart () public void WriteObjectStart() {
{
DoValidation(Condition.NotAProperty); DoValidation(Condition.NotAProperty);
PutNewline(); PutNewline();
Put("{"); Put("{");
context = new WriterContext (); this.context = new WriterContext {
context.InObject = true; InObject = true
ctx_stack.Push (context); };
this.ctx_stack.Push(this.context);
Indent(); Indent();
} }
public void WritePropertyName (string property_name) public void WritePropertyName(String property_name) {
{
DoValidation(Condition.Property); DoValidation(Condition.Property);
PutNewline(); PutNewline();
string propertyName = (property_name == null || !lower_case_properties) String propertyName = (property_name == null || !this.lower_case_properties) ? property_name : property_name.ToLowerInvariant();
? property_name
: property_name.ToLowerInvariant();
PutString(propertyName); PutString(propertyName);
if (pretty_print) { if (this.pretty_print) {
if (propertyName.Length > context.Padding) if (propertyName.Length > this.context.Padding) {
context.Padding = propertyName.Length; this.context.Padding = propertyName.Length;
}
for (int i = context.Padding - propertyName.Length; for (Int32 i = this.context.Padding - propertyName.Length;
i >= 0; i--) i >= 0; i--) {
writer.Write (' '); this.writer.Write(' ');
}
writer.Write (": "); this.writer.Write(": ");
} else } else {
writer.Write (':'); this.writer.Write(':');
}
context.ExpectingValue = true; this.context.ExpectingValue = true;
} }
} }
} }

Binary file not shown.