Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8013865
Json prototype (#1)
kasiabulat Jul 17, 2019
0f7df28
Json prototype - transformation API (#2)
kasiabulat Jul 19, 2019
6cd2efb
Json prototype (#1)
kasiabulat Jul 17, 2019
409e575
Json prototype - transformation API (#2)
kasiabulat Jul 19, 2019
25109c3
Merge branch 'master' of https://github.com/kasiabulat/corefx
kasiabulat Jul 29, 2019
9dcfc4b
JsonNumber implementation and tests (#3)
kasiabulat Jul 30, 2019
38af6d9
Merge remote-tracking branch 'upstream/master'
kasiabulat Jul 30, 2019
08f4734
all unimplemented classes and methods with accompanying tests removed
kasiabulat Jul 30, 2019
63ff329
First part of documentation added
kasiabulat Jul 30, 2019
0e0fb7c
documentation completed
kasiabulat Jul 31, 2019
47666ce
missing exceptions added
kasiabulat Jul 31, 2019
d856681
JsonElement changes removed
kasiabulat Jul 31, 2019
22e6558
part of the review comments included
kasiabulat Aug 1, 2019
04441f1
work on review comments
kasiabulat Aug 1, 2019
30c5dd0
code refactor
kasiabulat Aug 5, 2019
8f3e510
more decimal tests added using MemberData
kasiabulat Aug 5, 2019
c6ab148
more decimal tests added using MemberData
kasiabulat Aug 5, 2019
259590b
more test cases added
kasiabulat Aug 5, 2019
c4d6ef2
equals summary adjusted, equals tests added
kasiabulat Aug 5, 2019
01c230f
more Equals tests added, GetHashCode tests added, minor changes
kasiabulat Aug 6, 2019
392142a
scientifing notation support added, rational numbers tests fixes
kasiabulat Aug 6, 2019
310a5a6
rational overflow tests added
kasiabulat Aug 6, 2019
e03b803
ulong maxvalue tests added to rational types
kasiabulat Aug 6, 2019
95a9401
presision problems fixes
kasiabulat Aug 6, 2019
2bd6871
exception strings fixed
kasiabulat Aug 6, 2019
f0c4814
CI failing fixes (hopefully), review comments included
kasiabulat Aug 7, 2019
c22b36d
missing == tests added to achieve 100% branch coverage
kasiabulat Aug 7, 2019
7dfc891
review comments included
kasiabulat Aug 8, 2019
07746d8
Merge remote-tracking branch 'upstream/master' into kasiabulat/json-n…
kasiabulat Aug 8, 2019
d479fa7
Merge remote-tracking branch 'upstream/master' into kasiabulat/json-n…
kasiabulat Aug 8, 2019
ab1401a
trailing whitespaces fixes
kasiabulat Aug 8, 2019
217bade
equals comments added
kasiabulat Aug 8, 2019
40a6649
equals object refactored to call quals json number
kasiabulat Aug 8, 2019
af4aed3
Merge conflicts resolved
kasiabulat Aug 8, 2019
f534415
merge fix
kasiabulat Aug 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
work on review comments
  • Loading branch information
kasiabulat committed Aug 1, 2019
commit 04441f1224cc0cd632e15e9d96cd737a5aeaffb2
32 changes: 29 additions & 3 deletions src/System.Text.Json/src/System/Text/Json/Node/JsonNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class JsonNumber : JsonNode, IEquatable<JsonNumber>
/// <exception cref="ArgumentException">
/// Provided value is not in a legal JSON number format.
/// </exception>
/// <remarks>Provided value is stored in the same format as passed.</remarks>
public JsonNumber(string value) => SetFormattedValue(value);

/// <summary>
Expand Down Expand Up @@ -162,6 +163,12 @@ public class JsonNumber : JsonNode, IEquatable<JsonNumber>
/// <exception cref="FormatException">
/// <see cref="JsonNumber"/> represents a number in a format not compliant with <see cref="float"/>.
/// </exception>
/// <remarks>
/// On .NET Core this method does not return <see langword="false"/> for values larger than
/// <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>),
/// instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or
/// <see cref="float.NegativeInfinity"/>) is emitted.
/// </remarks>
public float GetSingle() => float.Parse(_value);

/// <summary>
Expand All @@ -174,6 +181,12 @@ public class JsonNumber : JsonNode, IEquatable<JsonNumber>
/// <exception cref="FormatException">
/// <see cref="JsonNumber"/> represents a number in a format not compliant with <see cref="double"/>.
/// </exception>
/// <remarks>
/// On .NET Core this method does not return <see langword="false"/> for values larger than
/// <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>),
/// instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or
/// <see cref="float.NegativeInfinity"/>) is emitted.
/// </remarks>
public double GetDouble() => double.Parse(_value);

/// <summary>
Expand Down Expand Up @@ -308,6 +321,12 @@ public class JsonNumber : JsonNode, IEquatable<JsonNumber>
/// <see langword="true"/> if instance was converted successfully;
/// otherwise, <see langword="false"/>
/// </returns>
/// <remarks>
/// On .NET Core this method does not return <see langword="false"/> for values larger than
/// <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>),
/// instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or
/// <see cref="float.NegativeInfinity"/>) is emitted.
/// </remarks>
public bool TryGetSingle(out float value) => float.TryParse(_value, out value);

/// <summary>
Expand All @@ -322,6 +341,12 @@ public class JsonNumber : JsonNode, IEquatable<JsonNumber>
/// <see langword="true"/> if instance was converted successfully;
/// otherwise, <see langword="false"/>
/// </returns>
/// <remarks>
/// On .NET Core this method does not return <see langword="false"/> for values larger than
/// <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>),
/// instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or
/// <see cref="float.NegativeInfinity"/>) is emitted.
/// </remarks>
public bool TryGetDouble(out double value) => double.TryParse(_value, out value);

/// <summary>
Expand Down Expand Up @@ -408,6 +433,7 @@ public class JsonNumber : JsonNode, IEquatable<JsonNumber>
/// <exception cref="ArgumentException">
/// Provided value is not in a legal JSON number format.
/// </exception>
/// <remarks>Provided value is stored in the same format as passed.</remarks>
public void SetFormattedValue(string value)
{
if (string.IsNullOrEmpty(value))
Expand Down Expand Up @@ -587,7 +613,7 @@ public void SetFormattedValue(string value)
/// <see langword="true"/> if the numeric value of this instance matches <paramref name="other"/>,
/// <see langword="false"/> otherwise.
/// </returns>
public bool Equals(JsonNumber other) => _value == other._value;
public bool Equals(JsonNumber other) => !(other is null) && _value == other._value;

/// <summary>
/// Compares numeric values of two JSON numbers.
Expand All @@ -598,7 +624,7 @@ public void SetFormattedValue(string value)
/// <see langword="true"/> if the numeric value of instances matches,
/// <see langword="false"/> otherwise.
/// </returns>
public static bool operator ==(JsonNumber left, JsonNumber right) => left._value == right._value;
public static bool operator ==(JsonNumber left, JsonNumber right) => left?._value == right?._value;

/// <summary>
/// Compares numeric values of two JSON numbers.
Expand All @@ -609,6 +635,6 @@ public void SetFormattedValue(string value)
/// <see langword="true"/> if the numeric value of instances does not match,
/// <see langword="false"/> otherwise.
/// </returns>
public static bool operator !=(JsonNumber left, JsonNumber right) => left._value != right._value;
public static bool operator !=(JsonNumber left, JsonNumber right) => left?._value != right?._value;
}
}
68 changes: 44 additions & 24 deletions src/System.Text.Json/tests/JsonNodeTests.JsonNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ namespace System.Text.Json.Tests
{
public static partial class JsonNodeTests
{
private delegate bool TryGetValue<T>(out T result);

private static void SimpleNumberTests<T>(
T value,
Func<JsonNumber, T> ctor,
Action<JsonNumber, T> setter,
Func<T, JsonNumber> getter, TryGetValue<T> tryGetter,
Func<T, JsonNumber> implicitCaster)
{
JsonNumber number = new JsonNumber();
setter(number, value);
AssertValue(value, number, getter, tryGetter);

number = ctor(value);
AssertValue(value, number, getter, tryGetter);

number = new JsonNumber(value.ToString());
AssertValue(value, number, getter, tryGetter);

number = implicitCaster(value);
AssertValue(value, number, getter, tryGetter);
}

private static void AssertValue<T>(
T value,
JsonNumber number,
Func<T, JsonNumber> getter,
TryGetValue<T> tryGetter)
{
Assert.Equal(value, getter(number));
Assert.True(tryGetter(number, out T result));
Assert.Equal(value, result);
}


[Fact]
public static void TestDefaultCtor()
{
Expand All @@ -31,30 +66,13 @@ public static void TestDefaultCtor()
[InlineData(byte.MaxValue)]
public static void TestByte(byte value)
{
// Default constructor:
var jsonNumber = new JsonNumber();
jsonNumber.SetByte(value);
Assert.Equal(value, jsonNumber.GetByte());
Assert.True(jsonNumber.TryGetByte(out byte result));
Assert.Equal(value, result);

// Numeric type constructor:
jsonNumber = new JsonNumber(value);
Assert.Equal(value, jsonNumber.GetByte());
Assert.True(jsonNumber.TryGetByte(out result));
Assert.Equal(value, result);

// Implicit cast:
jsonNumber = value;
Assert.Equal(value, jsonNumber.GetByte());
Assert.True(jsonNumber.TryGetByte(out result));
Assert.Equal(value, result);

// String constructor:
jsonNumber = new JsonNumber(value.ToString());
Assert.Equal(value, jsonNumber.GetByte());
Assert.True(jsonNumber.TryGetByte(out result));
Assert.Equal(value, result);
SimpleNumberTests(
value,
v => new JsonNumber(v),
(number, v) => number.SetInt16(v),
number => number.GetInt16(),
(number, out v) => number.TryGetValue(out v),
v => v);
}

[Theory]
Expand Down Expand Up @@ -453,6 +471,8 @@ public static void TestNullString()

[Theory]
[InlineData("0")]
[InlineData("1.1e1")]
[InlineData("0.0")]
[InlineData("-17")]
[InlineData("17")]
[InlineData("3.14")]
Expand Down