source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/CustomJsonWriter.cs @ 17284

Last change on this file since 17284 was 17284, checked in by dpiringe, 3 years ago

#3026: renamed namespace and project from HeuristicLab.Manufacture to HeuristicLab.JsonInterface

File size: 1.2 KB
Line 
1using System.IO;
2using Newtonsoft.Json;
3using Newtonsoft.Json.Linq;
4
5namespace HeuristicLab.JsonInterface {
6  internal class CustomJsonWriter : JsonTextWriter {
7    private bool isRangeArray = false;
8    public override void WriteStartArray() {
9     
10      if (isRangeArray) base.Formatting = Formatting.None;
11      base.WriteStartArray();
12    }
13
14    public override void WritePropertyName(string name) {
15      base.Formatting = Formatting.Indented;
16      base.WritePropertyName(name);
17      isRangeArray = name == "Range" || name == "Default";
18    }
19
20    public override void WriteStartObject() {
21      base.Formatting = Formatting.Indented;
22      base.WriteStartObject();
23    }
24
25    public override void WriteEndObject() {
26      base.Formatting = Formatting.Indented;
27      base.WriteEndObject();
28    }
29
30    public CustomJsonWriter(TextWriter writer) : base(writer) { }
31
32    public static string Serialize(JToken token) {
33      JsonSerializer serializer = new JsonSerializer();
34      StringWriter sw = new StringWriter();
35      CustomJsonWriter writer = new CustomJsonWriter(sw);
36      writer.Formatting = Formatting.Indented;
37      serializer.Serialize(writer, token);
38      return sw.ToString();
39    }
40  }
41}
Note: See TracBrowser for help on using the repository browser.