Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit acc7a79

Browse files
committed
update test example
1 parent 60b4b79 commit acc7a79

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

tests/ServiceStack.Text.Tests/JsonTests/CustomSerializerTests.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -315,37 +315,35 @@ public FormatAttribute(string format)
315315
public class DcStatus
316316
{
317317
[Format("{0:0.0} V")]
318-
public Double Voltage { get; set; }
318+
public double Voltage { get; set; }
319+
319320
[Format("{0:0.000} A")]
320-
public Double Current { get; set; }
321+
public double Current { get; set; }
322+
321323
[Format("{0:0} W")]
322-
public Double Power
323-
{
324-
get { return Voltage * Current; }
325-
}
324+
public double Power => Voltage * Current;
326325

327326
public string ToJson()
328327
{
329328
return new Dictionary<string, string>
330329
{
331-
{ "Voltage", "{0:0.0} V".Fmt(Voltage) },
332-
{ "Current", "{0:0.000} A".Fmt(Current) },
333-
{ "Power", "{0:0} W".Fmt(Power) },
330+
{ "Voltage", $"{Voltage:0.0} V"},
331+
{ "Current", $"{Current:0.000} A"},
332+
{ "Power", $"{Power:0} W"},
334333
}.ToJson();
335334
}
336335
}
337336

338-
public class DcStatus2
337+
public class DcStatusRawFn
339338
{
340339
[Format("{0:0.0} V")]
341-
public Double Voltage { get; set; }
340+
public double Voltage { get; set; }
341+
342342
[Format("{0:0.000} A")]
343-
public Double Current { get; set; }
343+
public double Current { get; set; }
344+
344345
[Format("{0:0} W")]
345-
public Double Power
346-
{
347-
get { return Voltage*Current; }
348-
}
346+
public double Power => Voltage * Current;
349347
}
350348

351349
[Test]
@@ -354,13 +352,13 @@ public void Can_deserialize_using_CustomFormat()
354352
var test = new DcStatus { Voltage = 10, Current = 1.2 };
355353
Assert.That(test.ToJson(), Is.EqualTo("{\"Voltage\":\"10.0 V\",\"Current\":\"1.200 A\",\"Power\":\"12 W\"}"));
356354

357-
JsConfig<DcStatus2>.RawSerializeFn = o => new Dictionary<string, string> {
358-
{ "Voltage", "{0:0.0} V".Fmt(o.Voltage) },
359-
{ "Current", "{0:0.000} A".Fmt(o.Current) },
360-
{ "Power", "{0:0} W".Fmt(o.Power) },
355+
JsConfig<DcStatusRawFn>.RawSerializeFn = o => new Dictionary<string, string> {
356+
{ "Voltage", $"{o.Voltage:0.0} V"},
357+
{ "Current", $"{o.Current:0.000} A"},
358+
{ "Power", $"{o.Power:0} W"},
361359
}.ToJson();
362360

363-
var test2 = new DcStatus2 { Voltage = 10, Current = 1.2 };
361+
var test2 = new DcStatusRawFn { Voltage = 10, Current = 1.2 };
364362
Assert.That(test2.ToJson(), Is.EqualTo("{\"Voltage\":\"10.0 V\",\"Current\":\"1.200 A\",\"Power\":\"12 W\"}"));
365363

366364
JsConfig.Reset();

0 commit comments

Comments
 (0)