diff --git a/src/neo/IO/Json/JArray.cs b/src/neo/IO/Json/JArray.cs index 6de623d29a..8b7dab524d 100644 --- a/src/neo/IO/Json/JArray.cs +++ b/src/neo/IO/Json/JArray.cs @@ -1,9 +1,7 @@ -using System; using System.Collections; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Text; +using System.Text.Json; namespace Neo.IO.Json { @@ -55,7 +53,7 @@ public void Add(JObject item) public override string AsString() { - return string.Join(VALUE_SEPARATOR.ToString(), items.Select(p => p?.AsString())); + return string.Join(",", items.Select(p => p?.AsString())); } public void Clear() @@ -93,32 +91,6 @@ public void Insert(int index, JObject item) items.Insert(index, item); } - internal new static JArray Parse(TextReader reader, int max_nest) - { - SkipSpace(reader); - if (reader.Read() != BEGIN_ARRAY) throw new FormatException(); - JArray array = new JArray(); - SkipSpace(reader); - if (reader.Peek() != END_ARRAY) - { - while (true) - { - JObject obj = JObject.Parse(reader, max_nest - 1); - array.items.Add(obj); - SkipSpace(reader); - char nextchar = (char)reader.Read(); - if (nextchar == VALUE_SEPARATOR) continue; - if (nextchar == END_ARRAY) break; - throw new FormatException(); - } - } - else - { - reader.Read(); - } - return array; - } - public bool Remove(JObject item) { return items.Remove(item); @@ -129,27 +101,22 @@ public void RemoveAt(int index) items.RemoveAt(index); } - public override string ToString() + internal override void Write(Utf8JsonWriter writer) { - StringBuilder sb = new StringBuilder(); - sb.Append(BEGIN_ARRAY); + writer.WriteStartArray(); foreach (JObject item in items) { - if (item == null) - sb.Append(LITERAL_NULL); + if (item is null) + writer.WriteNullValue(); else - sb.Append(item); - sb.Append(VALUE_SEPARATOR); + item.Write(writer); } - if (items.Count == 0) - { - sb.Append(END_ARRAY); - } - else - { - sb[sb.Length - 1] = END_ARRAY; - } - return sb.ToString(); + writer.WriteEndArray(); + } + + public static implicit operator JArray(JObject[] value) + { + return new JArray(value); } } } diff --git a/src/neo/IO/Json/JBoolean.cs b/src/neo/IO/Json/JBoolean.cs index 6b7c7a34c4..fa1554ab56 100644 --- a/src/neo/IO/Json/JBoolean.cs +++ b/src/neo/IO/Json/JBoolean.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.Text.Json; namespace Neo.IO.Json { @@ -27,38 +26,19 @@ public override string AsString() return Value.ToString().ToLowerInvariant(); } - internal static JBoolean Parse(TextReader reader) - { - SkipSpace(reader); - char firstChar = (char)reader.Peek(); - if (firstChar == LITERAL_FALSE[0]) - return ParseFalse(reader); - else if (firstChar == LITERAL_TRUE[0]) - return ParseTrue(reader); - throw new FormatException(); - } - - internal static JBoolean ParseFalse(TextReader reader) + public override string ToString() { - SkipSpace(reader); - for (int i = 0; i < LITERAL_FALSE.Length; i++) - if ((char)reader.Read() != LITERAL_FALSE[i]) - throw new FormatException(); - return new JBoolean(false); + return AsString(); } - internal static JBoolean ParseTrue(TextReader reader) + internal override void Write(Utf8JsonWriter writer) { - SkipSpace(reader); - for (int i = 0; i < LITERAL_TRUE.Length; i++) - if ((char)reader.Read() != LITERAL_TRUE[i]) - throw new FormatException(); - return new JBoolean(true); + writer.WriteBooleanValue(Value); } - public override string ToString() + public static implicit operator JBoolean(bool value) { - return AsString(); + return new JBoolean(value); } } } diff --git a/src/neo/IO/Json/JNumber.cs b/src/neo/IO/Json/JNumber.cs index daac440094..4f933aa3ad 100644 --- a/src/neo/IO/Json/JNumber.cs +++ b/src/neo/IO/Json/JNumber.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; -using System.IO; -using System.Text; +using System.Text.Json; namespace Neo.IO.Json { @@ -14,12 +13,13 @@ public class JNumber : JObject public JNumber(double value = 0) { + if (!double.IsFinite(value)) throw new FormatException(); this.Value = value; } public override bool AsBoolean() { - return Value != 0 && !double.IsNaN(Value); + return Value != 0; } public override double AsNumber() @@ -29,73 +29,9 @@ public override double AsNumber() public override string AsString() { - if (double.IsPositiveInfinity(Value)) throw new FormatException("Positive infinity number"); - if (double.IsNegativeInfinity(Value)) throw new FormatException("Negative infinity number"); return Value.ToString(CultureInfo.InvariantCulture); } - internal static JNumber Parse(TextReader reader) - { - SkipSpace(reader); - StringBuilder sb = new StringBuilder(); - char nextchar = (char)reader.Read(); - if (nextchar == '-') - { - sb.Append(nextchar); - nextchar = (char)reader.Read(); - } - if (nextchar < '0' || nextchar > '9') throw new FormatException(); - sb.Append(nextchar); - if (nextchar > '0') - { - while (true) - { - char c = (char)reader.Peek(); - if (c < '0' || c > '9') break; - sb.Append((char)reader.Read()); - } - } - nextchar = (char)reader.Peek(); - if (nextchar == '.') - { - sb.Append((char)reader.Read()); - nextchar = (char)reader.Read(); - if (nextchar < '0' || nextchar > '9') throw new FormatException(); - sb.Append(nextchar); - while (true) - { - nextchar = (char)reader.Peek(); - if (nextchar < '0' || nextchar > '9') break; - sb.Append((char)reader.Read()); - } - } - if (nextchar == 'e' || nextchar == 'E') - { - sb.Append((char)reader.Read()); - nextchar = (char)reader.Read(); - if (nextchar == '-' || nextchar == '+') - { - sb.Append(nextchar); - nextchar = (char)reader.Read(); - } - if (nextchar < '0' || nextchar > '9') throw new FormatException(); - sb.Append(nextchar); - while (true) - { - nextchar = (char)reader.Peek(); - if (nextchar < '0' || nextchar > '9') break; - sb.Append((char)reader.Read()); - } - } - - var value = double.Parse(sb.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture); - - if (value > MAX_SAFE_INTEGER || value < MIN_SAFE_INTEGER) - throw new FormatException(); - - return new JNumber(value); - } - public override string ToString() { return AsString(); @@ -116,5 +52,15 @@ public override T TryGetEnum(T defaultValue = default, bool ignoreCase = fals object result = Enum.ToObject(enumType, value); return Enum.IsDefined(enumType, result) ? (T)result : defaultValue; } + + internal override void Write(Utf8JsonWriter writer) + { + writer.WriteNumberValue(Value); + } + + public static implicit operator JNumber(double value) + { + return new JNumber(value); + } } } diff --git a/src/neo/IO/Json/JObject.cs b/src/neo/IO/Json/JObject.cs index cf6f68cbc7..11e477704d 100644 --- a/src/neo/IO/Json/JObject.cs +++ b/src/neo/IO/Json/JObject.cs @@ -3,23 +3,12 @@ using System.Collections.Generic; using System.IO; using System.Text; +using System.Text.Json; namespace Neo.IO.Json { public class JObject { - protected const char BEGIN_ARRAY = '['; - protected const char BEGIN_OBJECT = '{'; - protected const char END_ARRAY = ']'; - protected const char END_OBJECT = '}'; - protected const char NAME_SEPARATOR = ':'; - protected const char VALUE_SEPARATOR = ','; - protected const char QUOTATION_MARK = '"'; - protected const string WS = " \t\n\r"; - protected const string LITERAL_FALSE = "false"; - protected const string LITERAL_NULL = "null"; - protected const string LITERAL_TRUE = "true"; - public static readonly JObject Null = null; public IDictionary Properties { get; } = new OrderedDictionary(); @@ -27,8 +16,9 @@ public class JObject { get { - Properties.TryGetValue(name, out JObject value); - return value; + if (Properties.TryGetValue(name, out JObject value)) + return value; + return null; } set { @@ -56,112 +46,114 @@ public bool ContainsProperty(string key) return Properties.ContainsKey(key); } - public static JObject Parse(TextReader reader, int max_nest = 100) - { - if (max_nest < 0) throw new FormatException(); - SkipSpace(reader); - char firstChar = (char)reader.Peek(); - if (firstChar == LITERAL_FALSE[0]) - return JBoolean.ParseFalse(reader); - if (firstChar == LITERAL_NULL[0]) - return ParseNull(reader); - if (firstChar == LITERAL_TRUE[0]) - return JBoolean.ParseTrue(reader); - if (firstChar == BEGIN_OBJECT) - return ParseObject(reader, max_nest); - if (firstChar == BEGIN_ARRAY) - return JArray.Parse(reader, max_nest); - if ((firstChar >= '0' && firstChar <= '9') || firstChar == '-') - return JNumber.Parse(reader); - if (firstChar == QUOTATION_MARK) - return JString.Parse(reader); - throw new FormatException(); + private static string GetString(ref Utf8JsonReader reader) + { + try + { + return reader.GetString(); + } + catch (InvalidOperationException ex) + { + throw new FormatException(ex.Message, ex); + } } - public static JObject Parse(string value, int max_nest = 100) + public static JObject Parse(ReadOnlySpan value, int max_nest = 100) { - using (StringReader reader = new StringReader(value)) + Utf8JsonReader reader = new Utf8JsonReader(value, new JsonReaderOptions + { + AllowTrailingCommas = false, + CommentHandling = JsonCommentHandling.Skip, + MaxDepth = max_nest + }); + try { - JObject json = Parse(reader, max_nest); - SkipSpace(reader); - if (reader.Read() != -1) throw new FormatException(); + JObject json = Read(ref reader); + if (reader.Read()) throw new FormatException(); return json; } + catch (JsonException ex) + { + throw new FormatException(ex.Message, ex); + } } - private static JObject ParseNull(TextReader reader) + public static JObject Parse(string value, int max_nest = 100) { - for (int i = 0; i < LITERAL_NULL.Length; i++) - if ((char)reader.Read() != LITERAL_NULL[i]) - throw new FormatException(); - return null; + return Parse(Encoding.UTF8.GetBytes(value), max_nest); } - private static JObject ParseObject(TextReader reader, int max_nest) + private static JObject Read(ref Utf8JsonReader reader, bool skipReading = false) { - SkipSpace(reader); - if (reader.Read() != BEGIN_OBJECT) throw new FormatException(); - JObject obj = new JObject(); - SkipSpace(reader); - if (reader.Peek() != END_OBJECT) - { - while (true) - { - string name = JString.Parse(reader).Value; - if (obj.Properties.ContainsKey(name)) throw new FormatException(); - SkipSpace(reader); - if (reader.Read() != NAME_SEPARATOR) throw new FormatException(); - JObject value = Parse(reader, max_nest - 1); - obj.Properties.Add(name, value); - SkipSpace(reader); - char nextchar = (char)reader.Read(); - if (nextchar == VALUE_SEPARATOR) continue; - if (nextchar == END_OBJECT) break; - throw new FormatException(); - } - } - else + if (!skipReading && !reader.Read()) throw new FormatException(); + return reader.TokenType switch { - reader.Read(); - } - return obj; + JsonTokenType.False => false, + JsonTokenType.Null => Null, + JsonTokenType.Number => reader.GetDouble(), + JsonTokenType.StartArray => ReadArray(ref reader), + JsonTokenType.StartObject => ReadObject(ref reader), + JsonTokenType.String => GetString(ref reader), + JsonTokenType.True => true, + _ => throw new FormatException(), + }; } - protected static void SkipSpace(TextReader reader) + private static JArray ReadArray(ref Utf8JsonReader reader) { - while (WS.IndexOf((char)reader.Peek()) >= 0) + JArray array = new JArray(); + while (reader.Read()) { - reader.Read(); + switch (reader.TokenType) + { + case JsonTokenType.EndArray: + return array; + default: + array.Add(Read(ref reader, skipReading: true)); + break; + } } + throw new FormatException(); } - public override string ToString() + private static JObject ReadObject(ref Utf8JsonReader reader) { - StringBuilder sb = new StringBuilder(); - sb.Append(BEGIN_OBJECT); - foreach (KeyValuePair pair in Properties) + JObject obj = new JObject(); + while (reader.Read()) { - sb.Append((JObject)pair.Key); - sb.Append(NAME_SEPARATOR); - if (pair.Value == null) - { - sb.Append(LITERAL_NULL); - } - else + switch (reader.TokenType) { - sb.Append(pair.Value); + case JsonTokenType.EndObject: + return obj; + case JsonTokenType.PropertyName: + string name = GetString(ref reader); + if (obj.Properties.ContainsKey(name)) throw new FormatException(); + JObject value = Read(ref reader); + obj.Properties.Add(name, value); + break; + default: + throw new FormatException(); } - sb.Append(VALUE_SEPARATOR); - } - if (Properties.Count == 0) - { - sb.Append(END_OBJECT); } - else + throw new FormatException(); + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool indented) + { + using MemoryStream ms = new MemoryStream(); + using Utf8JsonWriter writer = new Utf8JsonWriter(ms, new JsonWriterOptions { - sb[sb.Length - 1] = END_OBJECT; - } - return sb.ToString(); + Indented = indented, + SkipValidation = true + }); + Write(writer); + writer.Flush(); + return Encoding.UTF8.GetString(ms.ToArray()); } public virtual T TryGetEnum(T defaultValue = default, bool ignoreCase = false) where T : Enum @@ -169,29 +161,43 @@ public override string ToString() return defaultValue; } + internal virtual void Write(Utf8JsonWriter writer) + { + writer.WriteStartObject(); + foreach (KeyValuePair pair in Properties) + { + writer.WritePropertyName(pair.Key); + if (pair.Value is null) + writer.WriteNullValue(); + else + pair.Value.Write(writer); + } + writer.WriteEndObject(); + } + public static implicit operator JObject(Enum value) { - return new JString(value.ToString()); + return (JString)value; } public static implicit operator JObject(JObject[] value) { - return new JArray(value); + return (JArray)value; } public static implicit operator JObject(bool value) { - return new JBoolean(value); + return (JBoolean)value; } public static implicit operator JObject(double value) { - return new JNumber(value); + return (JNumber)value; } public static implicit operator JObject(string value) { - return value == null ? null : new JString(value); + return (JString)value; } } } diff --git a/src/neo/IO/Json/JString.cs b/src/neo/IO/Json/JString.cs index 032470cf4e..a54284c3f0 100644 --- a/src/neo/IO/Json/JString.cs +++ b/src/neo/IO/Json/JString.cs @@ -1,8 +1,6 @@ using System; using System.Globalization; -using System.IO; -using System.Text; -using System.Text.Encodings.Web; +using System.Text.Json; namespace Neo.IO.Json { @@ -31,50 +29,6 @@ public override string AsString() return Value; } - internal static JString Parse(TextReader reader) - { - SkipSpace(reader); - if (reader.Read() != QUOTATION_MARK) throw new FormatException(); - char[] buffer = new char[4]; - StringBuilder sb = new StringBuilder(); - while (true) - { - int c = reader.Read(); - if (c == QUOTATION_MARK) break; - if (c == '\\') - { - c = (char)reader.Read(); - switch (c) - { - case QUOTATION_MARK: c = QUOTATION_MARK; break; - case '\\': c = '\\'; break; - case '/': c = '/'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 'r': c = '\r'; break; - case 't': c = '\t'; break; - case 'u': - reader.Read(buffer, 0, buffer.Length); - c = short.Parse(new string(buffer), NumberStyles.HexNumber); - break; - default: throw new FormatException(); - } - } - else if (c < ' ' || c == -1) - { - throw new FormatException(); - } - sb.Append((char)c); - } - return new JString(sb.ToString()); - } - - public override string ToString() - { - return $"{QUOTATION_MARK}{JavaScriptEncoder.Default.Encode(Value)}{QUOTATION_MARK}"; - } - public override T TryGetEnum(T defaultValue = default, bool ignoreCase = false) { try @@ -86,5 +40,20 @@ public override T TryGetEnum(T defaultValue = default, bool ignoreCase = fals return defaultValue; } } + + internal override void Write(Utf8JsonWriter writer) + { + writer.WriteStringValue(Value); + } + + public static implicit operator JString(Enum value) + { + return new JString(value.ToString()); + } + + public static implicit operator JString(string value) + { + return value == null ? null : new JString(value); + } } } diff --git a/src/neo/Wallets/NEP6/NEP6Wallet.cs b/src/neo/Wallets/NEP6/NEP6Wallet.cs index 3668287b3e..daf1b9158d 100644 --- a/src/neo/Wallets/NEP6/NEP6Wallet.cs +++ b/src/neo/Wallets/NEP6/NEP6Wallet.cs @@ -28,11 +28,7 @@ public NEP6Wallet(string path, string name = null) this.path = path; if (File.Exists(path)) { - JObject wallet; - using (StreamReader reader = new StreamReader(path)) - { - wallet = JObject.Parse(reader); - } + JObject wallet = JObject.Parse(File.ReadAllBytes(path)); LoadFromJson(wallet, out Scrypt, out accounts, out extra); } else diff --git a/tests/neo.UnitTests/IO/Json/UT_JBoolean.cs b/tests/neo.UnitTests/IO/Json/UT_JBoolean.cs index 302d48c52a..ea080cd423 100644 --- a/tests/neo.UnitTests/IO/Json/UT_JBoolean.cs +++ b/tests/neo.UnitTests/IO/Json/UT_JBoolean.cs @@ -1,8 +1,6 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.IO.Json; -using System; -using System.IO; namespace Neo.UnitTests.IO.Json { @@ -25,53 +23,5 @@ public void TestAsNumber() jFalse.AsNumber().Should().Be(0); jTrue.AsNumber().Should().Be(1); } - - [TestMethod] - public void TestParse() - { - TextReader tr1 = new StringReader("true"); - JBoolean ret1 = JBoolean.Parse(tr1); - ret1.AsBoolean().Should().BeTrue(); - - TextReader tr2 = new StringReader("false"); - JBoolean ret2 = JBoolean.Parse(tr2); - ret2.AsBoolean().Should().BeFalse(); - - TextReader tr3 = new StringReader("aaa"); - Action action = () => JBoolean.Parse(tr3); - action.Should().Throw(); - } - - [TestMethod] - public void TestParseFalse() - { - TextReader tr1 = new StringReader("false"); - JBoolean ret1 = JBoolean.ParseFalse(tr1); - ret1.AsBoolean().Should().BeFalse(); - - TextReader tr2 = new StringReader("aaa"); - Action action = () => JBoolean.ParseFalse(tr2); - action.Should().Throw(); - - TextReader tr3 = new StringReader("\t\rfalse"); - JBoolean ret3 = JBoolean.ParseFalse(tr3); - ret3.AsBoolean().Should().BeFalse(); - } - - [TestMethod] - public void TestParseTrue() - { - TextReader tr1 = new StringReader("true"); - JBoolean ret1 = JBoolean.ParseTrue(tr1); - ret1.AsBoolean().Should().BeTrue(); - - TextReader tr2 = new StringReader("aaa"); - Action action = () => JBoolean.ParseTrue(tr2); - action.Should().Throw(); - - TextReader tr3 = new StringReader(" true"); - JBoolean ret3 = JBoolean.ParseTrue(tr3); - ret3.AsBoolean().Should().BeTrue(); - } } } diff --git a/tests/neo.UnitTests/IO/Json/UT_JNumber.cs b/tests/neo.UnitTests/IO/Json/UT_JNumber.cs index e3cbe6d300..677157a1f0 100644 --- a/tests/neo.UnitTests/IO/Json/UT_JNumber.cs +++ b/tests/neo.UnitTests/IO/Json/UT_JNumber.cs @@ -2,7 +2,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.IO.Json; using System; -using System.IO; namespace Neo.UnitTests.IO.Json { @@ -43,6 +42,9 @@ public void TestAsString() Action action2 = () => new JNumber(double.NegativeInfinity).AsString(); action2.Should().Throw(); + + Action action3 = () => new JNumber(double.NaN).AsString(); + action3.Should().Throw(); } [TestMethod] @@ -53,15 +55,5 @@ public void TestTryGetEnum() new JNumber(2).TryGetEnum().Should().Be(Woo.James); new JNumber(3).TryGetEnum().Should().Be(Woo.Tom); } - - [TestMethod] - public void TestParse() - { - Action action1 = () => JNumber.Parse(new StringReader("100.a")); - action1.Should().Throw(); - - Action action2 = () => JNumber.Parse(new StringReader("100.+")); - action2.Should().Throw(); - } } } diff --git a/tests/neo.UnitTests/IO/Json/UT_JObject.cs b/tests/neo.UnitTests/IO/Json/UT_JObject.cs index 9645b04011..fe835016f0 100644 --- a/tests/neo.UnitTests/IO/Json/UT_JObject.cs +++ b/tests/neo.UnitTests/IO/Json/UT_JObject.cs @@ -2,7 +2,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.IO.Json; using System; -using System.IO; namespace Neo.UnitTests.IO.Json { @@ -53,35 +52,28 @@ public void TestAsNumber() [TestMethod] public void TestParse() { - Action action = () => JObject.Parse(new StringReader(""), -1); - action.Should().Throw(); - } - - [TestMethod] - public void TestParseNull() - { - Action action = () => JObject.Parse(new StringReader("naaa")); - action.Should().Throw(); - - JObject.Parse(new StringReader("null")).Should().BeNull(); - } - - [TestMethod] - public void TestParseObject() - { - Action action1 = () => JObject.Parse(new StringReader("{\"k1\":\"v1\",\"k1\":\"v2\"}"), 100); - action1.Should().Throw(); - - Action action2 = () => JObject.Parse(new StringReader("{\"k1\",\"k1\"}"), 100); - action2.Should().Throw(); - - Action action3 = () => JObject.Parse(new StringReader("{\"k1\":\"v1\""), 100); - action3.Should().Throw(); - - Action action4 = () => JObject.Parse(new StringReader("aaa"), 100); - action4.Should().Throw(); - - JObject.Parse(new StringReader("{\"k1\":\"v1\"}"), 100).ToString().Should().Be("{\"k1\":\"v1\"}"); + Assert.ThrowsException(() => JObject.Parse("", -1)); + Assert.ThrowsException(() => JObject.Parse("aaa")); + Assert.ThrowsException(() => JObject.Parse("hello world")); + Assert.ThrowsException(() => JObject.Parse("100.a")); + Assert.ThrowsException(() => JObject.Parse("100.+")); + Assert.ThrowsException(() => JObject.Parse("\"\\s\"")); + Assert.ThrowsException(() => JObject.Parse("\"a")); + Assert.ThrowsException(() => JObject.Parse("{\"k1\":\"v1\",\"k1\":\"v2\"}")); + Assert.ThrowsException(() => JObject.Parse("{\"k1\",\"k1\"}")); + Assert.ThrowsException(() => JObject.Parse("{\"k1\":\"v1\"")); + Assert.ThrowsException(() => JObject.Parse(new byte[] { 0x22, 0x01, 0x22 })); + Assert.ThrowsException(() => JObject.Parse("{\"color\":\"red\",\"\\uDBFF\\u0DFFF\":\"#f00\"}")); + Assert.ThrowsException(() => JObject.Parse("{\"color\":\"\\uDBFF\\u0DFFF\"}")); + Assert.ThrowsException(() => JObject.Parse("\"\\uDBFF\\u0DFFF\"")); + + JObject.Parse("null").Should().BeNull(); + JObject.Parse("true").AsBoolean().Should().BeTrue(); + JObject.Parse("false").AsBoolean().Should().BeFalse(); + JObject.Parse("\"hello world\"").AsString().Should().Be("hello world"); + JObject.Parse("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"").AsString().Should().Be("\"\\/\b\f\n\r\t"); + JObject.Parse("\"\\u0030\"").AsString().Should().Be("0"); + JObject.Parse("{\"k1\":\"v1\"}", 100).ToString().Should().Be("{\"k1\":\"v1\"}"); } [TestMethod] diff --git a/tests/neo.UnitTests/IO/Json/UT_JString.cs b/tests/neo.UnitTests/IO/Json/UT_JString.cs index 5d20b30fa0..0b6df4c087 100644 --- a/tests/neo.UnitTests/IO/Json/UT_JString.cs +++ b/tests/neo.UnitTests/IO/Json/UT_JString.cs @@ -2,7 +2,6 @@ using Neo.IO.Json; using Neo.SmartContract; using System; -using System.IO; namespace Neo.UnitTests.IO { @@ -12,7 +11,7 @@ public class UT_JString [TestMethod] public void TestConstructor() { - String s = "hello world"; + string s = "hello world"; JString jstring = new JString(s); Assert.AreEqual(s, jstring.Value); Assert.ThrowsException(() => new JString(null)); @@ -21,8 +20,8 @@ public void TestConstructor() [TestMethod] public void TestAsBoolean() { - String s1 = "hello world"; - String s2 = ""; + string s1 = "hello world"; + string s2 = ""; JString jstring1 = new JString(s1); JString jstring2 = new JString(s2); Assert.AreEqual(true, jstring1.AsBoolean()); @@ -30,32 +29,17 @@ public void TestAsBoolean() } [TestMethod] - public void TestParse() + public void TestAsNumber() { - TextReader tr = new StringReader("\"hello world\""); - String s = JString.Parse(tr).Value; - Assert.AreEqual("hello world", s); - - tr = new StringReader("hello world"); - Assert.ThrowsException(() => JString.Parse(tr)); - - tr = new StringReader("\"\\s\""); - Assert.ThrowsException(() => JString.Parse(tr)); - - tr = new StringReader("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\""); - s = JString.Parse(tr).Value; - Assert.AreEqual("\"\\/\b\f\n\r\t", s); - - tr = new StringReader("\"\\u0030\""); - s = JString.Parse(tr).Value; - Assert.AreEqual("0", s); - - tr = new StringReader("\"a"); - Assert.ThrowsException(() => JString.Parse(tr)); - - byte[] byteArray = new byte[] { 0x22, 0x01, 0x22 }; - tr = new StringReader(System.Text.Encoding.ASCII.GetString(byteArray)); - Assert.ThrowsException(() => JString.Parse(tr)); + string s1 = "hello world"; + string s2 = "123"; + string s3 = ""; + JString jstring1 = new JString(s1); + JString jstring2 = new JString(s2); + JString jstring3 = new JString(s3); + Assert.AreEqual(double.NaN, jstring1.AsNumber()); + Assert.AreEqual(123, jstring2.AsNumber()); + Assert.AreEqual(0, jstring3.AsNumber()); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs b/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs index 788868622c..5f2758ad36 100644 --- a/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs +++ b/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs @@ -32,9 +32,6 @@ public void JsonTest_WrongJson() json = @"{""length"":99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999}"; Assert.ThrowsException(() => JObject.Parse(json)); - - json = $"{{\"length\":{long.MaxValue}}}"; - Assert.ThrowsException(() => JObject.Parse(json)); } [TestMethod]