-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New unit: TorquePerLength and fix for array serialization & deserialization #712
Conversation
Added support for serializing/deserializing array of units
… failing on array serialization and deserialization functionality
Codecov Report
@@ Coverage Diff @@
## master #712 +/- ##
=========================================
Coverage ? 58.44%
=========================================
Files ? 168
Lines ? 38304
Branches ? 0
=========================================
Hits ? 22386
Misses ? 15918
Partials ? 0
Continue to review full report at Codecov.
|
@angularsen ping |
Okay, I added the TorquePerLength on a copy from old/v3, bumped minor & suffix (3.112.0-2) and uploaded the nuget package to our nuget feed. |
I'm unavailable all this week but glad you found a way.
…On Mon, Oct 21, 2019, 12:34 Dirk Schuermans ***@***.***> wrote:
Okay, I added the TorquePerLength on a copy from old/v3, bumped minor &
suffix (3.112.0-2) and uploaded the nuget package to our nuget feed.
So i'm all set for now 👍
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#712?email_source=notifications&email_token=AAGAK2EV4HVR5LE2ADESBOTQPWAURA5CNFSM4JB3CXXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBZ3UNI#issuecomment-544455221>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGAK2AQAML6B25DIW5MBH3QPWAURANCNFSM4JB3CXXA>
.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Good job fixing the compatibility tests 👍
For next time, it would be better to create two separate pull requests for adding a quantity and fixing JSON serialization. I'll look at addressing the syntax suggestions after merging this PR.
@@ -41,9 +42,36 @@ public class UnitsNetJsonConverter : JsonConverter | |||
|
|||
object obj = TryDeserializeIComparable(reader, serializer); | |||
// A null System.Nullable value or a comparable type was deserialized so return this | |||
if (!(obj is ValueUnit vu)) | |||
if (!(obj is ValueUnit) && !(obj is Array)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but this could benefit from being rewritten to if (obj is ValueUnit valueUnit) {} else if (obj is Array array) { }
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I think about it, "obj is Array" might be cutting it a bit short too?
What if it's an array of strings? or any other type?
Not entirely sure if we can even end up in that scenario though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is Array
should cover string[]
I believe. It's the base type all arrays implement if I understand it right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, but shouldn't we only be expecting an array of Units there?
When it's an array of e.g. strings, we should return that array as is and not try to parse them as units?
As per the comment there: '// A null System.Nullable value or a comparable type was deserialized so return this'
I'd imagine it applies to a "array of system.nullable values or an array of comparable types"
Just thinking out loud here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good questions, I don't really recall how this all works anymore and I didn't write the implementation. I would have to read some more unit tests and dig a bit more into how this is actually used.
For instance, does this JSON deserialize properly or does it trip up on the other stuff?
{
"somestring": "foo",
"somenumber": 5,
"some_quantity": { "Value": 5, "Unit": "LengthUnit.Meter" },
"some_quantity_array": [ { "Value": 5, "Unit": "LengthUnit.Meter" } ],
"some_other_array": [ 5, "abc", ["def" ] ]
}
|
||
List<object> results = new List<object>(); | ||
|
||
foreach (var value in values) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This foreach can be changed to LINQ.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, i copied it from my initial attempt and it was laid out like this for easier debugging while developing.
} | ||
|
||
Type unitType = objectType.GetElementType(); | ||
Array typedArray = Array.CreateInstance(unitType, results.Count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this all actually necessary? I think if you replace the foreach and this below with a LINQ like this, I believe it should work:
var results = values.Select(value => ParseValueUnit(value as ValueUnit)).ToArray();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you'll run into runtime exceptions, saying it can't convert object to array or whatever...
This is how I've always converted an "object" into an type specific array (eg string[] or double[] or whatever)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were absolutely right, it wasn't quite as simple as I first imagined. I did make some simplifications in #715 if you want to take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, like how you handled it there :)
protected override double PoundForceInchesPerFootInOneNewtonMeterPerMeter => 2.69770722235; | ||
protected override double TonneForceCentimetersPerMeterInOneNewtonMeterPerMeter => 1.01971621298e-2; | ||
protected override double TonneForceMetersPerMeterInOneNewtonMeterPerMeter => 1.01971621298e-4; | ||
protected override double TonneForceMillimetersPerMeterInOneNewtonMeterPerMeter => 1.01971621298e-1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified these.
Found during PR review #712. Addressed separately.
Found during PR review #712. Addressed separately.
Cheers. Yea, having to do the merge from v3 to v4 it was easier to also incorporate my fixes for the JSON stuff... |
I seem to have forgot to release the nuget. Here it is. |
Added a new unit: TorquePerLength
This unit is used in the structural analysis field when expressing the rotational stiffness resistance around an axis
Also added support for serialization & deserialization of an array of units in JSON