You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to deserialize this to custom classes called Line and Point. The X, Y and Z of point or of the type Length. The data deserializes just fine except for the 'Length" types. Those end up with a value of zero. Is there a way to use the XML data above an deserialize to a default LengthUnit type like meter?
Line:
public class Line : IEntity, IDebugOutputable, ISplitable, ILength, IRobotElement
{
public string Name { get; set; }
public Point StartPoint { get; set; } = new(new Length(), new Length(), new Length());
public Point EndPoint { get; set; } = new(new Length(), new Length(), new Length());
public double Volume { get; set; } = 0;
public Length Length
{
get
{
LengthUnit unit = Length.Info.UnitType;
double a = Math.Pow(EndPoint.X.Value - StartPoint.X.Value, 2);
double b = Math.Pow(EndPoint.Y.Value - StartPoint.Y.Value, 2);
double c = Math.Pow(EndPoint.Z.Value - StartPoint.Z.Value, 2);
var result = Length.From( Math.Sqrt(a + b + c), unit) ;
return result;
}
}
public string DebugOutput()
{
StringBuilder result = new();
result.AppendLine();
result.AppendLine(Name);
result.AppendLine($"Start point: {StartPoint.DebugOutput()}");
result.AppendLine($"End point: {EndPoint.DebugOutput()}");
result.AppendLine($"Volume: {Math.Round(Volume, 3)}");
return result.ToString();
}
public IEntity Split(double percent)
{
if (percent <= 0 || percent >= 1) throw new ArgumentOutOfRangeException("percent must be between 0 and 1");
Line secondLine = new()
{
//Fix start point
StartPoint = Utils.InterpolatePoint(StartPoint, EndPoint, percent),
EndPoint = EndPoint,
Volume = Volume * (1 - percent),
Name = Name + $" - Remainder"
};
//Fix first line
EndPoint = secondLine.StartPoint;
Volume *= percent;
Name += $" - Base";
return secondLine;
}
}
Point:
public class Point : IDebugOutputable
{
//public Point() { }
public Point(Length x, Length y, Length z)
{
X = x;
Y = y;
Z = z;
}
public Length X { get; set; }
public Length Y { get; set; }
public Length Z { get; set; }
public string DebugOutput()
{
string result = "";
result += $"X: {Math.Round(X.Value, 3)}, ";
result += $"Y: {Math.Round(Y.Value, 3)}, ";
result += $"Z: {Math.Round(Z.Value, 3)}";
return result;
}
}
The text was updated successfully, but these errors were encountered:
You are probably better off having a non-UnitsNet type for serializing/deserializing, then mapping to UnitsNet after deserializing.
Most serializers support some form of plugins to add converters from/to specific types, but the simplest approach is to have separate types for serialization using standard .NET BCL types (double, decimal, string etc), then manually mapping from/to your domain types that use more complex types like UnitsNet quantities.
For example, you can have two classes: SerializablePoint and Point
I have XML that looks like this:
I need to deserialize this to custom classes called Line and Point. The X, Y and Z of point or of the type
Length
. The data deserializes just fine except for the 'Length" types. Those end up with a value of zero. Is there a way to use the XML data above an deserialize to a default LengthUnit type like meter?Line:
Point:
The text was updated successfully, but these errors were encountered: