How to encapsulate the code that has been processed using the Newtonsoft Json library?
I used the Newtonsoft Json library to write data to a JSON file, but I don't know how to encapsulate this part of the code. How to encapsulate it?
#######
JObject ArchiveBaseInfo = new JObject(
new JProperty("Version", ArchiveVersion),
new JProperty("Name", EncodeName),
new JProperty("ID", PlayerID)
);
File.WriteAllText(ArchiveFile, ArchiveBaseInfo.ToString());
// write JSON directly to a file
using (StreamWriter file = File.CreateText(ArchiveFile))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
ArchiveBaseInfo.WriteTo(writer);
}
5 Replies
I have a few questions:
1. Why
JObject
shenanigans and not objects proper?
2. Why Newtonsoft?
3. Why are you writing to the file twice?
4. What do you mean by "encapsulate" here?1. I don't know if he can
2. There is no need to pursue NLog for this project, but I don't know of any other well-known libraries besides NLog
3. o. WriteTo (writer); Can it be deleted?
4. It can be directly called where needed, such as. a (cv, fg); Such or similar method calls
2. he is not talking about NLog, Newtonsoft is old and slow used only for edge cases and faulty json these days. .NET comes with System.Text.Json out of the box which is the suggested.
leowest
REPL Result: Success
Console Output
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 492.813ms | Execution: 43.449ms | React with ❌ to remove this embed.
Understood
If it was "o. WriteTo (writer);" that caused writing twice, I think I know how to do it
thank you for you help. I think I know how to improve it