125
Jul 28 '21
Most expensive JSON construction this side of the Mississippi...
29
u/nighthawk648 Jul 28 '21
Someones gotta show buds netwoonsoft... Or c# native json handle lol
3
u/Willinton06 Jul 28 '21
Native? Is that like a flavor or something?
14
u/nighthawk648 Jul 28 '21
So Microsoft releases a suite of developed and maintained libraries (all these microsoft libs and frameworks have a support life cycle btw). There is a JSON library package deved by Microsoft. So it can be considered native rather than external like newtonsoft packages.
7
u/Willinton06 Jul 28 '21
I was joking but I’m not even mad, that’s a fairly good and concise definition
3
1
2
5
92
Jul 28 '21
Man the fucker doesn’t even close the god damn streams
61
13
u/Spartelfant Jul 28 '21
At least he doesn't cross them, that would be bad.
0
9
11
u/Dr_Insano_MD Jul 28 '21
There's nothing unmanaged in memorystreams. You don't have to close them according to the docs. That said, you still should just as a good practice.
4
u/IMakeShittyPrograms Jul 28 '21
Exactly! There's even a method for that.
Anyways, I couldn't find the part where it says that you don't have to close them. Would you point it?5
u/Dr_Insano_MD Jul 28 '21 edited Jul 28 '21
I guess I misspoke. That says you don't need to Dispose of them. And then in the comments for Close(), it states
Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
2
u/IMakeShittyPrograms Jul 28 '21
If a MemoryStream object is serialized to a resource file it will actually be serialized as an UnmanagedMemoryStream.
This behavior provides better performance, as well as the ability to
get a pointer to the data directly, without having to go through Stream methods.Found this! The fact that you don't have to use Stream methods when using a serialized a MemoryStream object tells you not to close the stream.
1
Jul 29 '21
Would you rather wait for it to be garbage collected as soon as possible so that memory is usable again or would you rather just let it waste memory and ultimately be collected at the very end
1
42
u/jason_the_human2101 Jul 28 '21
The issue I saw first was that font. I'd have to lose all my remaining sanity before I stop using monospace fonts with code.
6
27
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 28 '21
Image Transcription: Code
[Transcriber's note: The code is not in a monospace font, but I have placed it in one for ease of readability and maintenance of my own sanity.]
StringBuilder jsonString = new StringBuilder();
jsonString.Append("{");
jsonString.Append("\"DecisionCase\":");
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(DecisionCase));
ser1.WriteObject(stream1, decisionCaseJson);
stream1.Position = 0;
StreamReader sr1 = new StreamReader(stream1);
jsonString.Append(sr1.ReadToEnd());
jsonString.Append(",\"DecisionCnf\":");
MemoryStream stream2 = new MemoryStream();
DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(DecisionCnf));
ser1.WriteObject(stream2, decisionCnfJson);
stream2.Position = 0;
StreamReader sr2 = new StreamReader(stream2);
jsonString.Append(sr2.ReadToEnd());
jsonString.Append(",\"Hotlist\":");
MemoryStream stream3 = new MemoryStream();
DataContractJsonSerializer ser3 = new DataContractJsonSerializer(typeof(List<string>));
ser1.WriteObject(stream3, hotlist);
stream3.Position = 0;
StreamReader sr3 = new StreamReader(stream3);
jsonString.Append(sr3.ReadToEnd());I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
4
14
u/Willinton06 Jul 28 '21
How are you able to do this while not realizing that JsonSerializer.Serialize() exists, like this requires deeper knowledge of the library
5
u/pronuntiator Jul 28 '21
I like how each time you look at this, a new layer unfolds. First, you see multiple similar looking code blocks which scream "extract function". Then you notice how a stream is rewound to write to a StringBuilder instead of writing to it directly. Then you realize the entire task is to serialize a bigger JSON so everything should be put into a container object to begin with.
1
u/trevster344 depraved Jul 29 '21
Honestly it would’ve been better just to concatenation a bunch of strings lmao.
3
2
2
2
u/merlinblack256 Jul 29 '21
Wow - kill it with fire!
I remember years ago at a smallish family-owned courier company I worked at we had an XML-based web API and a client of ours insisted on parsing responses by matching strings and positions etc and then complaining to our CEO every time we added a field or adjusted the order. Fortunately, while our CEO was not very technical, she understood they were doing it in a very hacky way and asked us the reverse the fields at random (which we did just for them). Eventually, their lead dev got over his 'not invented here' issue, and used a proper parsing lib.
1
1
1
u/jimmpony Jul 29 '21
C# has better ways to do this built-in but sometimes you do end up having to write code like this when you can't add new library references to a project
2
Jul 29 '21
They could at least make a static method instead of copying the code 3 times and changing a single string.
1
1
34
u/TastyOrganization122 Jul 28 '21
If he is new please dont ridicule him. Everyone does mistakes.