-
Notifications
You must be signed in to change notification settings - Fork 15
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
Proposal/serialization #55
Conversation
Great, more things to benchmark 🤣 Btw, earlier, I was in the process of implementing the TODOs in the
|
Oh man, I really hate serialization... Look at the result of the The only way to get a "correct" JSON output, is to use the (obsolete) using System;
using System.Globalization;
using System.Numerics;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace Fractions;
[Serializable]
public partial struct Fraction : ISerializable {
private Fraction(SerializationInfo info, StreamingContext context) {
if (info == null) {
throw new ArgumentNullException(nameof(info));
}
var numerator = BigInteger.Parse(info.GetString(nameof(Numerator)) ?? "0", CultureInfo.InvariantCulture);
var denominator = BigInteger.Parse(info.GetString(nameof(Denominator)) ?? "0", CultureInfo.InvariantCulture);
var normalizationNotApplied = info.GetBoolean("NormalizationNotApplied");
this = new Fraction(normalizationNotApplied, numerator, denominator);
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
if (info == null) {
throw new ArgumentNullException(nameof(info));
}
info.AddValue(nameof(Numerator), Numerator.ToString(CultureInfo.InvariantCulture));
info.AddValue(nameof(Denominator), Denominator.ToString(CultureInfo.InvariantCulture));
info.AddValue("NormalizationNotApplied", _normalizationNotApplied);
}
} |
Nah, I don't care about the json at all- we have json-converters for that. It's just the WCF proxies that I wanted to handle. Is ISerializable still usable- last I remember there were tons of security alerts.. |
It is not recommended to use ISerializable (I do not know of any security alerts). I'll try something else tomorrow. I think that both XML and JSON should simply contain the fraction as text. Without XmlElements, attributes and so on. Just “2/3”. [EDIT 2024-05-04] |
…zationNotApplied (instead of attributes)
The data type implements IXmlSerializable + basic support for System.Text.Json Example output: XmlSerializer <?xml version="1.0" encoding="utf-16"?>
<Fraction>
<Numerator>2</Numerator>
<Denominator>4</Denominator>
<NormalizationNotApplied>True</NormalizationNotApplied>
</Fraction> DataContractSerializer <Fraction xmlns="http://schemas.datacontract.org/2004/07/Fractions">
<Numerator>2</Numerator>
<Denominator>4</Denominator>
<NormalizationNotApplied>True</NormalizationNotApplied>
</Fraction> DataContractJsonSerializer 😞 "<Fraction xmlns=\"http:\/\/schemas.datacontract.org\/2004\/07\/Fractions\"><Numerator>2<\/Numerator><Denominator>4<\/Denominator><NormalizationNotApplied>True<\/NormalizationNotApplied><\/Fraction>" System.Text.Json.JsonSerializer {
"Numerator":"2",
"Denominator":"4",
"NormalizationNotApplied":true
} @lipchev |
I was ready to live with no out-of-the-box support as well (and this looks great). PS Haven't checked the System.Text converter, but if you don't mind the hassle, it could probably find it's use somewhere for somebody as a separate nuget.. |
I have no idea 😬. The general suggestion is: make struct readonly if possible. [...] Readonly modifier on a struct declaration clearly expresses a design intend (emphasizing that the struct is immutable) and helps the compiler to avoid defensive copies in many contexts mentioned above. [...] Unfortunately, the
I don't understand what you mean by that. Should the serialized output be as small as possible? So that as little data as possible needs to be sent over the network? If that was your intention, then "1/3" would certainly be more efficient than the whole XML bullshit <Numerator>1<....
In fact, System.Text.Json is quite lightweight. I didn't have to adjust the |
Yes, certainly - in UnitsNet we only have the
I would not call NewtonSoft obsolete.. PS
I meant to say |
Actually, I would rather not implement the |
@lipchev If you agree, I would rather close this suggestion/pull request without MERGE. |
Yes, I've already prepared the IXmlSerializable implementation on our side. Thanks anyway! |
Implemented IXmlSerializable
readonly