The normal option to optimize the serialization in JSON?

I.e. the idea of less code to serialize to JSON.
Such is a self-contained test code:

using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;

namespace JsonSerialize {
 class Program {
 static void Main(string[] args) {

 // Proposed version - 6 Stoke
 Console.WriteLine("Proposed version");
 using (JsonWriter writer = JsonTextWriterNew.GetJsonTextWriterNew(out StringBuilder sb1)) {
writer.WriteStartObject();
 writer.WriteProperty("title", "Our name");
writer.WriteEndObject();
Console.WriteLine(sb1.ToString());
}

 // instead of the option - 9 rows
 Console.WriteLine("instead of option");
 StringBuilder sb2 = new StringBuilder();
 StringWriter sw = new StringWriter(sb2);
 using (JsonWriter writer = new JsonTextWriter(sw)) {
writer.WriteStartObject();
writer.WritePropertyName("title");
 writer.WriteValue("Our title");
writer.WriteEndObject();
}
Console.WriteLine(sb2.ToString());
Console.ReadKey();
}
}

}

namespace Newtonsoft.Json {
 public class JsonTextWriterNew : JsonWriter {

 private StringBuilder _sb;
 private StringWriter _sw;
 private JsonTextWriter _jtw;

 public static GetJsonTextWriterNew JsonTextWriter(out StringBuilder stringBuilder) {
 var jsonTextWriterExt = new JsonTextWriterNew();
 stringBuilder = jsonTextWriterExt._sb;
 return jsonTextWriterExt._jtw;
}

 public JsonTextWriterNew() {
 _sb = new StringBuilder();
 _sw = new StringWriter(_sb);
 _jtw = new JsonTextWriter(_sw);
}

 public override void Flush() {
_jtw.Flush();
}
}

 public static class JsonWriterExtensions {
 public static void WriteProperty(this JsonWriter writer, string name, object value) {
writer.WritePropertyName(name);
writer.WriteValue(value);
}
}
}
March 23rd 20 at 19:14
1 answer
March 23rd 20 at 19:16
Thank you, revered. And here's to this "answer".
What is it? - rebeka commented on March 23rd 20 at 19:19
@rebeka, to the fact that usually, only one line - thea51 commented on March 23rd 20 at 19:22
@thea51, there is no object in its entirety. it is necessary to collect.
the question is asked in the context of the possible closure of unmanaged resources.
well, can someone tell me more from personal experience. - rebeka commented on March 23rd 20 at 19:25
@rebeka, this is a bad practice, in fact, you still create an object yourself. bad, what opens unlimited scope for errors. and closed source hints at the fact that so no one does

for the same reason, waiting for advice "from personal experience" is also a dubious idea - thea51 commented on March 23rd 20 at 19:28
@rebeka, I will explain kament above - C# language multiparadigmatic in which the PLO, we can say the dominant paradigm. "hand picking" of the object - except as an exercise in the size of your example. but in a large project, this is a guaranteed road to HELL - thea51 commented on March 23rd 20 at 19:31
>> it's a bad practice
it's all clear and so myself. the question is that there is no object at compile time.
in C#, no verification occurs where it is. - rebeka commented on March 23rd 20 at 19:34

Find more questions by tags C#.NET