Skip to content

Commit 9224f70

Browse files
authored
Merge pull request #772 from jetersen/fix/systemtextjson
add JsonInclude for Polynomial and DescriptiveStatistics
2 parents e657d18 + 382a2ac commit 9224f70

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

src/Numerics.Tests/PolynomialTests.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public void EvaluateTest(double[] c, double z, double expected)
444444
}
445445

446446
// static Complex Evaluate(Complex, double[])
447-
{
447+
{
448448
var actual = Polynomial.Evaluate(zComplex, c);
449449
Assert.AreEqual(expectedComplex, actual);
450450
}
@@ -466,8 +466,21 @@ public void EvaluateTest(double[] c, double z, double expected)
466466
var actual = p.Evaluate(zComplex);
467467
Assert.AreEqual(expectedComplex, actual);
468468
}
469-
469+
470+
}
471+
472+
#if NET5_0_OR_GREATER
473+
[Test]
474+
public void JsonDeserializationTest()
475+
{
476+
var polynomial = new Polynomial(0, 1, 2);
477+
var json = System.Text.Json.JsonSerializer.Serialize(polynomial);
478+
var deserialize = System.Text.Json.JsonSerializer.Deserialize<Polynomial>(json);
479+
Assert.NotNull(deserialize);
480+
Assert.AreEqual(polynomial.Coefficients.Length, deserialize.Coefficients.Length);
481+
Assert.IsTrue(polynomial.Coefficients.SequenceEqual(deserialize.Coefficients));
470482
}
483+
#endif
471484

472485
}
473486

src/Numerics.Tests/StatisticsTests/DescriptiveStatisticsTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
using System;
3131
using System.Collections.Generic;
32+
#if NET5_0_OR_GREATER
33+
using System.Text.Json;
34+
using System.Text.Json.Serialization;
35+
#endif
3236
using NUnit.Framework;
3337
using MathNet.Numerics.Statistics;
3438

@@ -301,5 +305,47 @@ public void ZeroVarianceSequence()
301305
Assert.That(stats.Skewness, Is.NaN);
302306
Assert.That(stats.Kurtosis, Is.NaN);
303307
}
308+
309+
#if NET5_0_OR_GREATER
310+
/// <summary>
311+
/// <c>IEnumerable</c> Double.
312+
/// </summary>
313+
/// <param name="dataSet">Dataset name.</param>
314+
/// <param name="digits">Digits count.</param>
315+
/// <param name="skewness">Skewness value.</param>
316+
/// <param name="kurtosis">Kurtosis value.</param>
317+
/// <param name="median">Median value.</param>
318+
/// <param name="min">Min value.</param>
319+
/// <param name="max">Max value.</param>
320+
/// <param name="count">Count value.</param>
321+
[TestCase("lottery", 12, -0.09333165310779, -1.19256091074856, 522.5, 4, 999, 218)]
322+
[TestCase("lew", 12, -0.050606638756334, -1.49604979214447, -162, -579, 300, 200)]
323+
[TestCase("mavro", 11, 0.64492948110824, -0.82052379677456, 2.0018, 2.0013, 2.0027, 50)]
324+
[TestCase("michelso", 11, -0.0185388637725746, 0.33968459842539, 299.85, 299.62, 300.07, 100)]
325+
[TestCase("numacc1", 15, 0, double.NaN, 10000002, 10000001, 10000003, 3)]
326+
[TestCase("numacc2", 13, 0, -2.003003003003, 1.2, 1.1, 1.3, 1001)]
327+
[TestCase("numacc3", 9, 0, -2.003003003003, 1000000.2, 1000000.1, 1000000.3, 1001)]
328+
[TestCase("numacc4", 7, 0, -2.00300300299913, 10000000.2, 10000000.1, 10000000.3, 1001)]
329+
[TestCase("meixner", 8, -0.016649617280859657, 0.8171318629552635, -0.002042931016531602, -4.825626912281697, 5.3018298664184913, 10000)]
330+
public void JsonDeserializationTest(string dataSet, int digits, double skewness, double kurtosis, double median, double min, double max, int count)
331+
{
332+
var data = _data[dataSet];
333+
var serialize = new DescriptiveStatistics(data.Data, false);
334+
var jsonSerializerOptions = new JsonSerializerOptions
335+
{
336+
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
337+
};
338+
var json = JsonSerializer.Serialize(serialize, jsonSerializerOptions);
339+
var stats = JsonSerializer.Deserialize<DescriptiveStatistics>(json, jsonSerializerOptions);
340+
Assert.NotNull(stats);
341+
AssertHelpers.AlmostEqualRelative(data.Mean, stats.Mean, 10);
342+
AssertHelpers.AlmostEqualRelative(data.StandardDeviation, stats.StandardDeviation, digits);
343+
AssertHelpers.AlmostEqualRelative(skewness, stats.Skewness, 8);
344+
AssertHelpers.AlmostEqualRelative(kurtosis, stats.Kurtosis, 8);
345+
Assert.AreEqual(stats.Minimum, min);
346+
Assert.AreEqual(stats.Maximum, max);
347+
Assert.AreEqual(stats.Count, count);
348+
}
349+
#endif
304350
}
305351
}

src/Numerics/Polynomial.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using System.Linq;
66
using Complex = System.Numerics.Complex;
77
using System.Text;
8+
#if NET5_0_OR_GREATER
9+
using System.Text.Json.Serialization;
10+
#endif
811
using MathNet.Numerics.LinearAlgebra;
912
using MathNet.Numerics.LinearAlgebra.Double;
1013
using MathNet.Numerics.LinearRegression;
@@ -24,6 +27,9 @@ public class Polynomial : IFormattable, IEquatable<Polynomial>, ICloneable
2427
/// The coefficients of the polynomial in a
2528
/// </summary>
2629
[DataMember(Order = 1)]
30+
#if NET5_0_OR_GREATER
31+
[JsonInclude]
32+
#endif
2733
public double[] Coefficients { get; private set; }
2834

2935
/// <summary>

src/Numerics/Statistics/DescriptiveStatistics.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
using System;
3131
using System.Collections.Generic;
3232
using System.Runtime.Serialization;
33+
#if NET5_0_OR_GREATER
34+
using System.Text.Json.Serialization;
35+
#endif
3336

3437
namespace MathNet.Numerics.Statistics
3538
{
@@ -107,32 +110,56 @@ public DescriptiveStatistics(IEnumerable<double?> data, bool increasedAccuracy =
107110
}
108111
}
109112

113+
#if NET5_0_OR_GREATER
114+
/// <summary>
115+
/// Initializes a new instance of the <see cref="DescriptiveStatistics"/> class.
116+
/// </summary>
117+
/// <remarks>
118+
/// Used for Json serialization
119+
/// </remarks>
120+
public DescriptiveStatistics()
121+
{
122+
}
123+
#endif
124+
110125
/// <summary>
111126
/// Gets the size of the sample.
112127
/// </summary>
113128
/// <value>The size of the sample.</value>
114129
[DataMember(Order = 1)]
130+
#if NET5_0_OR_GREATER
131+
[JsonInclude]
132+
#endif
115133
public long Count { get; private set; }
116134

117135
/// <summary>
118136
/// Gets the sample mean.
119137
/// </summary>
120138
/// <value>The sample mean.</value>
121139
[DataMember(Order = 2)]
140+
#if NET5_0_OR_GREATER
141+
[JsonInclude]
142+
#endif
122143
public double Mean { get; private set; }
123144

124145
/// <summary>
125146
/// Gets the unbiased population variance estimator (on a dataset of size N will use an N-1 normalizer).
126147
/// </summary>
127148
/// <value>The sample variance.</value>
128149
[DataMember(Order = 3)]
150+
#if NET5_0_OR_GREATER
151+
[JsonInclude]
152+
#endif
129153
public double Variance { get; private set; }
130154

131155
/// <summary>
132156
/// Gets the unbiased population standard deviation (on a dataset of size N will use an N-1 normalizer).
133157
/// </summary>
134158
/// <value>The sample standard deviation.</value>
135159
[DataMember(Order = 4)]
160+
#if NET5_0_OR_GREATER
161+
[JsonInclude]
162+
#endif
136163
public double StandardDeviation { get; private set; }
137164

138165
/// <summary>
@@ -141,6 +168,9 @@ public DescriptiveStatistics(IEnumerable<double?> data, bool increasedAccuracy =
141168
/// <value>The sample skewness.</value>
142169
/// <remarks>Returns zero if <see cref="Count"/> is less than three. </remarks>
143170
[DataMember(Order = 5)]
171+
#if NET5_0_OR_GREATER
172+
[JsonInclude]
173+
#endif
144174
public double Skewness { get; private set; }
145175

146176
/// <summary>
@@ -149,20 +179,29 @@ public DescriptiveStatistics(IEnumerable<double?> data, bool increasedAccuracy =
149179
/// <value>The sample kurtosis.</value>
150180
/// <remarks>Returns zero if <see cref="Count"/> is less than four. </remarks>
151181
[DataMember(Order = 6)]
182+
#if NET5_0_OR_GREATER
183+
[JsonInclude]
184+
#endif
152185
public double Kurtosis { get; private set; }
153186

154187
/// <summary>
155188
/// Gets the maximum sample value.
156189
/// </summary>
157190
/// <value>The maximum sample value.</value>
158191
[DataMember(Order = 7)]
192+
#if NET5_0_OR_GREATER
193+
[JsonInclude]
194+
#endif
159195
public double Maximum { get; private set; }
160196

161197
/// <summary>
162198
/// Gets the minimum sample value.
163199
/// </summary>
164200
/// <value>The minimum sample value.</value>
165201
[DataMember(Order = 8)]
202+
#if NET5_0_OR_GREATER
203+
[JsonInclude]
204+
#endif
166205
public double Minimum { get; private set; }
167206

168207
/// <summary>

0 commit comments

Comments
 (0)