diff --git a/LibreMetaverse.StructuredData/JSON/JsonData.cs b/LibreMetaverse.StructuredData/JSON/JsonData.cs index 694d6258..55c6fd67 100644 --- a/LibreMetaverse.StructuredData/JSON/JsonData.cs +++ b/LibreMetaverse.StructuredData/JSON/JsonData.cs @@ -15,6 +15,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; +using System.Linq; namespace LitJson @@ -38,83 +39,44 @@ namespace LitJson #region Properties - public int Count { - get { return EnsureCollection ().Count; } - } + public int Count => EnsureCollection ().Count; - public bool IsArray { - get { return type == JsonType.Array; } - } + public bool IsArray => type == JsonType.Array; - public bool IsBoolean { - get { return type == JsonType.Boolean; } - } + public bool IsBoolean => type == JsonType.Boolean; - public bool IsDouble { - get { return type == JsonType.Double; } - } + public bool IsDouble => type == JsonType.Double; - public bool IsInt { - get { return type == JsonType.Int; } - } + public bool IsInt => type == JsonType.Int; - public bool IsLong { - get { return type == JsonType.Long; } - } + public bool IsLong => type == JsonType.Long; - public bool IsObject { - get { return type == JsonType.Object; } - } + public bool IsObject => type == JsonType.Object; + + public bool IsString => type == JsonType.String; - public bool IsString { - get { return type == JsonType.String; } - } #endregion #region ICollection Properties - int ICollection.Count { - get { - return Count; - } - } + int ICollection.Count => Count; - bool ICollection.IsSynchronized { - get { - return EnsureCollection ().IsSynchronized; - } - } + bool ICollection.IsSynchronized => EnsureCollection ().IsSynchronized; + + object ICollection.SyncRoot => EnsureCollection ().SyncRoot; - object ICollection.SyncRoot { - get { - return EnsureCollection ().SyncRoot; - } - } #endregion #region IDictionary Properties - bool IDictionary.IsFixedSize { - get { - return EnsureDictionary ().IsFixedSize; - } - } + bool IDictionary.IsFixedSize => EnsureDictionary ().IsFixedSize; - bool IDictionary.IsReadOnly { - get { - return EnsureDictionary ().IsReadOnly; - } - } + bool IDictionary.IsReadOnly => EnsureDictionary ().IsReadOnly; ICollection IDictionary.Keys { get { EnsureDictionary (); - IList keys = new List (); - - foreach (KeyValuePair entry in - object_list) { - keys.Add (entry.Key); - } + IList keys = object_list.Select(entry => entry.Key).ToList(); return (ICollection) keys; } @@ -123,12 +85,7 @@ namespace LitJson ICollection IDictionary.Values { get { EnsureDictionary (); - IList values = new List (); - - foreach (KeyValuePair entry in - object_list) { - values.Add (entry.Value); - } + IList values = object_list.Select(entry => entry.Value).ToList(); return (ICollection) values; } @@ -138,48 +95,28 @@ namespace LitJson #region IJsonWrapper Properties - bool IJsonWrapper.IsArray { - get { return IsArray; } - } + bool IJsonWrapper.IsArray => IsArray; - bool IJsonWrapper.IsBoolean { - get { return IsBoolean; } - } + bool IJsonWrapper.IsBoolean => IsBoolean; - bool IJsonWrapper.IsDouble { - get { return IsDouble; } - } + bool IJsonWrapper.IsDouble => IsDouble; - bool IJsonWrapper.IsInt { - get { return IsInt; } - } + bool IJsonWrapper.IsInt => IsInt; - bool IJsonWrapper.IsLong { - get { return IsLong; } - } + bool IJsonWrapper.IsLong => IsLong; - bool IJsonWrapper.IsObject { - get { return IsObject; } - } + bool IJsonWrapper.IsObject => IsObject; + + bool IJsonWrapper.IsString => IsString; - bool IJsonWrapper.IsString { - get { return IsString; } - } #endregion #region IList Properties - bool IList.IsFixedSize { - get { - return EnsureList ().IsFixedSize; - } - } + bool IList.IsFixedSize => EnsureList ().IsFixedSize; + + bool IList.IsReadOnly => EnsureList ().IsReadOnly; - bool IList.IsReadOnly { - get { - return EnsureList ().IsReadOnly; - } - } #endregion @@ -212,14 +149,9 @@ namespace LitJson set { EnsureDictionary (); JsonData data = ToJsonData (value); - - KeyValuePair old_entry = object_list[idx]; - + var old_entry = object_list[idx]; inst_object[old_entry.Key] = data; - - KeyValuePair entry = - new KeyValuePair (old_entry.Key, data); - + var entry = new KeyValuePair (old_entry.Key, data); object_list[idx] = entry; } } @@ -251,19 +183,22 @@ namespace LitJson set { EnsureDictionary (); - - KeyValuePair entry = - new KeyValuePair (prop_name, value); - - if (inst_object.ContainsKey (prop_name)) { - for (int i = 0; i < object_list.Count; i++) { - if (object_list[i].Key == prop_name) { + var entry = new KeyValuePair (prop_name, value); + if (inst_object.ContainsKey(prop_name)) + { + for (int i = 0; i < object_list.Count; i++) + { + if (object_list[i].Key == prop_name) + { object_list[i] = entry; break; } } - } else - object_list.Add (entry); + } + else + { + object_list.Add(entry); + } inst_object[prop_name] = value; @@ -274,11 +209,7 @@ namespace LitJson public JsonData this[int index] { get { EnsureCollection (); - - if (type == JsonType.Array) - return inst_array[index]; - - return object_list[index].Value; + return type == JsonType.Array ? inst_array[index] : object_list[index].Value; } set { @@ -287,9 +218,8 @@ namespace LitJson if (type == JsonType.Array) inst_array[index] = value; else { - KeyValuePair entry = object_list[index]; - KeyValuePair new_entry = - new KeyValuePair (entry.Key, value); + var entry = object_list[index]; + var new_entry = new KeyValuePair (entry.Key, value); object_list[index] = new_entry; inst_object[entry.Key] = value; @@ -465,8 +395,7 @@ namespace LitJson EnsureDictionary ().Add (key, data); - KeyValuePair entry = - new KeyValuePair ((string) key, data); + var entry = new KeyValuePair ((string) key, data); object_list.Add (entry); json = null; @@ -779,8 +708,6 @@ namespace LitJson WriteJson ((JsonData) entry.Value, writer); } writer.WriteObjectEnd (); - - return; } } #endregion @@ -799,21 +726,16 @@ namespace LitJson { if (IsObject) { ((IDictionary) this).Clear (); - return; } - - if (IsArray) { + else if (IsArray) + { ((IList) this).Clear (); - return; } } public bool Equals (JsonData x) { - if (x == null) - return false; - - if (x.type != this.type) + if (x?.type != type) return false; switch (this.type) { @@ -898,8 +820,7 @@ namespace LitJson return json; StringWriter sw = new StringWriter (); - JsonWriter writer = new JsonWriter (sw); - writer.Validate = false; + JsonWriter writer = new JsonWriter(sw) {Validate = false}; WriteJson (this, writer); json = sw.ToString (); @@ -953,24 +874,18 @@ namespace LitJson IEnumerator> list_enumerator; - public object Current { - get { return Entry; } - } + public object Current => Entry; public DictionaryEntry Entry { get { - KeyValuePair curr = list_enumerator.Current; + var curr = list_enumerator.Current; return new DictionaryEntry (curr.Key, curr.Value); } } - public object Key { - get { return list_enumerator.Current.Key; } - } + public object Key => list_enumerator.Current.Key; - public object Value { - get { return list_enumerator.Current.Value; } - } + public object Value => list_enumerator.Current.Value; public OrderedDictionaryEnumerator ( diff --git a/LibreMetaverse.StructuredData/JSON/JsonException.cs b/LibreMetaverse.StructuredData/JSON/JsonException.cs index a356fd66..d99593b2 100644 --- a/LibreMetaverse.StructuredData/JSON/JsonException.cs +++ b/LibreMetaverse.StructuredData/JSON/JsonException.cs @@ -21,28 +21,24 @@ namespace LitJson } internal JsonException (ParserToken token) : - base (String.Format ( - "Invalid token '{0}' in input string", token)) + base ($"Invalid token '{token}' in input string") { } internal JsonException (ParserToken token, Exception inner_exception) : - base (String.Format ( - "Invalid token '{0}' in input string", token), + base ($"Invalid token '{token}' in input string", inner_exception) { } internal JsonException (int c) : - base (String.Format ( - "Invalid character '{0}' in input string", (char) c)) + base ($"Invalid character '{(char) c}' in input string") { } internal JsonException (int c, Exception inner_exception) : - base (String.Format ( - "Invalid character '{0}' in input string", (char) c), + base ($"Invalid character '{(char) c}' in input string", inner_exception) { } diff --git a/LibreMetaverse.StructuredData/JSON/JsonReader.cs b/LibreMetaverse.StructuredData/JSON/JsonReader.cs index a7c3d3ab..a3273001 100644 --- a/LibreMetaverse.StructuredData/JSON/JsonReader.cs +++ b/LibreMetaverse.StructuredData/JSON/JsonReader.cs @@ -12,7 +12,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; namespace LitJson @@ -71,21 +70,14 @@ namespace LitJson set { lexer.AllowSingleQuotedStrings = value; } } - public bool EndOfInput { - get { return end_of_input; } - } + public bool EndOfInput => end_of_input; - public bool EndOfJson { - get { return end_of_json; } - } + public bool EndOfJson => end_of_json; - public JsonToken Token { - get { return token; } - } + public JsonToken Token => token; + + public object Value => token_value; - public object Value { - get { return token_value; } - } #endregion @@ -108,7 +100,7 @@ namespace LitJson private JsonReader (TextReader reader, bool owned) { if (reader == null) - throw new ArgumentNullException ("reader"); + throw new ArgumentNullException (nameof(reader)); parser_in_string = false; parser_return = false; @@ -405,8 +397,6 @@ namespace LitJson } - int[] entry_symbols; - while (true) { if (parser_return) { if (automaton_stack.Peek () == (int) ParserToken.End) @@ -434,6 +424,7 @@ namespace LitJson continue; } + int[] entry_symbols; try { entry_symbols = diff --git a/LibreMetaverse.StructuredData/JSON/JsonWriter.cs b/LibreMetaverse.StructuredData/JSON/JsonWriter.cs index 9dcc59dc..dbc7bb77 100644 --- a/LibreMetaverse.StructuredData/JSON/JsonWriter.cs +++ b/LibreMetaverse.StructuredData/JSON/JsonWriter.cs @@ -48,9 +48,7 @@ namespace LitJson private int indentation; private int indent_value; private StringBuilder inst_string_builder; - private bool pretty_print; - private bool validate; - private TextWriter writer; + #endregion @@ -63,22 +61,11 @@ namespace LitJson } } - public bool PrettyPrint { - get { return pretty_print; } - set { pretty_print = value; } - } - - public TextWriter TextWriter { - get { return writer; } - } - - public bool Validate { - get { return validate; } - set { validate = value; } - } + public bool PrettyPrint { get; set; } + public TextWriter TextWriter { get; } + public bool Validate { get; set; } #endregion - #region Constructors static JsonWriter () { @@ -88,7 +75,7 @@ namespace LitJson public JsonWriter () { inst_string_builder = new StringBuilder (); - writer = new StringWriter (inst_string_builder); + TextWriter = new StringWriter (inst_string_builder); Init (); } @@ -101,9 +88,9 @@ namespace LitJson public JsonWriter (TextWriter writer) { if (writer == null) - throw new ArgumentNullException ("writer"); + throw new ArgumentNullException (nameof(writer)); - this.writer = writer; + this.TextWriter = writer; Init (); } @@ -116,7 +103,7 @@ namespace LitJson if (! context.ExpectingValue) context.Count++; - if (! validate) + if (! Validate) return; if (has_reached_end) @@ -164,8 +151,8 @@ namespace LitJson hex_seq = new char[4]; indentation = 0; indent_value = 4; - pretty_print = false; - validate = true; + PrettyPrint = false; + Validate = true; ctx_stack = new Stack (); context = new WriterContext (); @@ -174,10 +161,8 @@ namespace LitJson private static void IntToHex (int n, char[] hex) { - int num; - for (int i = 0; i < 4; i++) { - num = n % 16; + var num = n % 16; if (num < 10) hex[3 - i] = (char) ('0' + num); @@ -190,88 +175,83 @@ namespace LitJson private void Indent () { - if (pretty_print) + if (PrettyPrint) indentation += indent_value; } private void Put (string str) { - if (pretty_print && ! context.ExpectingValue) + if (PrettyPrint && ! context.ExpectingValue) for (int i = 0; i < indentation; i++) - writer.Write (' '); + TextWriter.Write (' '); - writer.Write (str); + TextWriter.Write (str); } - private void PutNewline () - { - PutNewline (true); - } - - private void PutNewline (bool add_comma) + private void PutNewline (bool add_comma = true) { if (add_comma && ! context.ExpectingValue && context.Count > 1) - writer.Write (','); + TextWriter.Write (','); - if (pretty_print && ! context.ExpectingValue) - writer.Write ('\n'); + if (PrettyPrint && ! context.ExpectingValue) + TextWriter.Write ('\n'); } private void PutString (string str) { - Put (String.Empty); + Put (string.Empty); - writer.Write ('"'); + TextWriter.Write ('"'); int n = str.Length; for (int i = 0; i < n; i++) { switch (str[i]) { case '\n': - writer.Write ("\\n"); + TextWriter.Write ("\\n"); continue; case '\r': - writer.Write ("\\r"); + TextWriter.Write ("\\r"); continue; case '\t': - writer.Write ("\\t"); + TextWriter.Write ("\\t"); continue; case '"': case '\\': - writer.Write ('\\'); - writer.Write (str[i]); + TextWriter.Write ('\\'); + TextWriter.Write (str[i]); continue; case '\f': - writer.Write ("\\f"); + TextWriter.Write ("\\f"); continue; case '\b': - writer.Write ("\\b"); + TextWriter.Write ("\\b"); continue; } - if ((int) str[i] >= 32 && (int) str[i] <= 126) { - writer.Write (str[i]); + if (str[i] >= 32 && str[i] <= 126) { + TextWriter.Write (str[i]); continue; } // Default, turn into a \uXXXX sequence IntToHex ((int) str[i], hex_seq); - writer.Write ("\\u"); - writer.Write (hex_seq); + TextWriter.Write ("\\u"); + TextWriter.Write (hex_seq); } - writer.Write ('"'); + TextWriter.Write ('"'); } private void Unindent () { - if (pretty_print) + if (PrettyPrint) indentation -= indent_value; } #endregion @@ -279,10 +259,7 @@ namespace LitJson public override string ToString () { - if (inst_string_builder == null) - return String.Empty; - - return inst_string_builder.ToString (); + return inst_string_builder?.ToString () ?? string.Empty; } public void Reset () @@ -293,8 +270,7 @@ namespace LitJson context = new WriterContext (); ctx_stack.Push (context); - if (inst_string_builder != null) - inst_string_builder.Remove (0, inst_string_builder.Length); + inst_string_builder?.Remove(0, inst_string_builder.Length); } public void Write (bool boolean) @@ -330,7 +306,7 @@ namespace LitJson Put(str); if (str.IndexOf('.') == -1 && str.IndexOf('E') == -1) - writer.Write(".0"); + TextWriter.Write(".0"); } context.ExpectingValue = false; @@ -403,8 +379,7 @@ namespace LitJson Put ("["); - context = new WriterContext (); - context.InArray = true; + context = new WriterContext {InArray = true}; ctx_stack.Push (context); Indent (); @@ -434,8 +409,7 @@ namespace LitJson Put ("{"); - context = new WriterContext (); - context.InObject = true; + context = new WriterContext {InObject = true}; ctx_stack.Push (context); Indent (); @@ -448,17 +422,17 @@ namespace LitJson PutString (property_name); - if (pretty_print) { + if (PrettyPrint) { if (property_name.Length > context.Padding) context.Padding = property_name.Length; for (int i = context.Padding - property_name.Length; i >= 0; i--) - writer.Write (' '); + TextWriter.Write (' '); - writer.Write (": "); + TextWriter.Write (": "); } else - writer.Write (':'); + TextWriter.Write (':'); context.ExpectingValue = true; } diff --git a/LibreMetaverse.StructuredData/JSON/Lexer.cs b/LibreMetaverse.StructuredData/JSON/Lexer.cs index 66f0be04..1044245f 100644 --- a/LibreMetaverse.StructuredData/JSON/Lexer.cs +++ b/LibreMetaverse.StructuredData/JSON/Lexer.cs @@ -10,7 +10,6 @@ using System; -using System.Collections.Generic; using System.IO; using System.Text; @@ -34,43 +33,22 @@ namespace LitJson private static int[] fsm_return_table; private static StateHandler[] fsm_handler_table; - private bool allow_comments; - private bool allow_single_quoted_strings; - private bool end_of_input; private FsmContext fsm_context; private int input_buffer; private int input_char; private TextReader reader; private int state; private StringBuilder string_buffer; - private string string_value; - private int token; private int unichar; #endregion #region Properties - public bool AllowComments { - get { return allow_comments; } - set { allow_comments = value; } - } - - public bool AllowSingleQuotedStrings { - get { return allow_single_quoted_strings; } - set { allow_single_quoted_strings = value; } - } - - public bool EndOfInput { - get { return end_of_input; } - } - - public int Token { - get { return token; } - } - - public string StringValue { - get { return string_value; } - } + public bool AllowComments { get; set; } + public bool AllowSingleQuotedStrings { get; set; } + public bool EndOfInput { get; private set; } + public int Token { get; private set; } + public string StringValue { get; private set; } #endregion @@ -82,17 +60,16 @@ namespace LitJson public Lexer (TextReader reader) { - allow_comments = true; - allow_single_quoted_strings = true; + AllowComments = true; + AllowSingleQuotedStrings = true; input_buffer = 0; string_buffer = new StringBuilder (128); state = 1; - end_of_input = false; + EndOfInput = false; this.reader = reader; - fsm_context = new FsmContext (); - fsm_context.L = this; + fsm_context = new FsmContext {L = this}; } #endregion @@ -277,7 +254,7 @@ namespace LitJson return true; case '\'': - if (! ctx.L.allow_single_quoted_strings) + if (! ctx.L.AllowSingleQuotedStrings) return false; ctx.L.input_char = '"'; @@ -286,7 +263,7 @@ namespace LitJson return true; case '/': - if (! ctx.L.allow_comments) + if (! ctx.L.AllowComments) return false; ctx.NextState = 25; @@ -403,13 +380,11 @@ namespace LitJson { ctx.L.GetChar (); - if (ctx.L.input_char >= '0' && ctx.L.input_char <= '9') { - ctx.L.string_buffer.Append ((char) ctx.L.input_char); - ctx.NextState = 6; - return true; - } + if (ctx.L.input_char < '0' || ctx.L.input_char > '9') return false; - return false; + ctx.L.string_buffer.Append ((char) ctx.L.input_char); + ctx.NextState = 6; + return true; } private static bool State6 (FsmContext ctx) @@ -855,7 +830,7 @@ namespace LitJson if ((input_char = NextChar ()) != -1) return true; - end_of_input = true; + EndOfInput = true; return false; } @@ -873,25 +848,24 @@ namespace LitJson public bool NextToken () { - StateHandler handler; fsm_context.Return = false; while (true) { - handler = fsm_handler_table[state - 1]; + var handler = fsm_handler_table[state - 1]; if (! handler (fsm_context)) throw new JsonException (input_char); - if (end_of_input) + if (EndOfInput) return false; if (fsm_context.Return) { - string_value = string_buffer.ToString (); + StringValue = string_buffer.ToString (); string_buffer.Remove (0, string_buffer.Length); - token = fsm_return_table[state - 1]; + Token = fsm_return_table[state - 1]; - if (token == (int) ParserToken.Char) - token = input_char; + if (Token == (int) ParserToken.Char) + Token = input_char; state = fsm_context.NextState; diff --git a/LibreMetaverse.StructuredData/JSON/OSDJson.cs b/LibreMetaverse.StructuredData/JSON/OSDJson.cs index 463a86df..d3ec21ad 100644 --- a/LibreMetaverse.StructuredData/JSON/OSDJson.cs +++ b/LibreMetaverse.StructuredData/JSON/OSDJson.cs @@ -39,10 +39,7 @@ namespace OpenMetaverse.StructuredData return OSD.FromReal((double)json); case JsonType.String: string str = (string)json; - if (String.IsNullOrEmpty(str)) - return new OSD(); - else - return OSD.FromString(str); + return String.IsNullOrEmpty(str) ? new OSD() : OSD.FromString(str); case JsonType.Array: OSDArray array = new OSDArray(json.Count); for (int i = 0; i < json.Count; i++) @@ -94,15 +91,15 @@ namespace OpenMetaverse.StructuredData byte[] binary = osd.AsBinary(); JsonData jsonbinarray = new JsonData(); jsonbinarray.SetJsonType(JsonType.Array); - for (int i = 0; i < binary.Length; i++) - jsonbinarray.Add(new JsonData(binary[i])); + foreach (byte t in binary) + jsonbinarray.Add(new JsonData(t)); return jsonbinarray; case OSDType.Array: JsonData jsonarray = new JsonData(); jsonarray.SetJsonType(JsonType.Array); OSDArray array = (OSDArray)osd; - for (int i = 0; i < array.Count; i++) - jsonarray.Add(SerializeJson(array[i], preserveDefaults)); + foreach (OSD t in array) + jsonarray.Add(SerializeJson(t, preserveDefaults)); return jsonarray; case OSDType.Map: JsonData jsonmap = new JsonData(); @@ -110,12 +107,9 @@ namespace OpenMetaverse.StructuredData OSDMap map = (OSDMap)osd; foreach (KeyValuePair kvp in map) { - JsonData data; - - if (preserveDefaults) - data = SerializeJson(kvp.Value, preserveDefaults); - else - data = SerializeJsonNoDefaults(kvp.Value); + var data = preserveDefaults + ? SerializeJson(kvp.Value, preserveDefaults) + : SerializeJsonNoDefaults(kvp.Value); if (data != null) jsonmap[kvp.Key] = data; @@ -133,36 +127,26 @@ namespace OpenMetaverse.StructuredData { case OSDType.Boolean: bool b = osd.AsBoolean(); - if (!b) - return null; + return !b ? null : new JsonData(b); - return new JsonData(b); case OSDType.Integer: int v = osd.AsInteger(); - if (v == 0) - return null; + return v == 0 ? null : new JsonData(v); - return new JsonData(v); case OSDType.Real: double d = osd.AsReal(); - if (d == 0.0d) - return null; + return d == 0.0d ? null : new JsonData(d); - return new JsonData(d); case OSDType.String: case OSDType.Date: case OSDType.URI: string str = osd.AsString(); - if (String.IsNullOrEmpty(str)) - return null; + return String.IsNullOrEmpty(str) ? null : new JsonData(str); - return new JsonData(str); case OSDType.UUID: UUID uuid = osd.AsUUID(); - if (uuid == UUID.Zero) - return null; + return uuid == UUID.Zero ? null : new JsonData(uuid.ToString()); - return new JsonData(uuid.ToString()); case OSDType.Binary: byte[] binary = osd.AsBinary(); if (binary == Utils.EmptyBytes) @@ -170,15 +154,15 @@ namespace OpenMetaverse.StructuredData JsonData jsonbinarray = new JsonData(); jsonbinarray.SetJsonType(JsonType.Array); - for (int i = 0; i < binary.Length; i++) - jsonbinarray.Add(new JsonData(binary[i])); + foreach (byte t in binary) + jsonbinarray.Add(new JsonData(t)); return jsonbinarray; case OSDType.Array: JsonData jsonarray = new JsonData(); jsonarray.SetJsonType(JsonType.Array); OSDArray array = (OSDArray)osd; - for (int i = 0; i < array.Count; i++) - jsonarray.Add(SerializeJson(array[i], false)); + foreach (OSD t in array) + jsonarray.Add(SerializeJson(t, false)); return jsonarray; case OSDType.Map: JsonData jsonmap = new JsonData();