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

Binary file not shown.