Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17353 was 17353, checked in by dpiringe, 4 years ago

#3026:

  • relocated GetMaxValue and GetMinValue from ValueTypeValueConverter into BaseConverter
  • fixed a bug in ConstrainedValueParameterConverter (from GetType().Name to ToString())
  • printing now PrettyNames for types
  • added comments
  • added StorableConverter.cs (not finished, maybe not a good converter)
  • added ValueRangeConverter.cs for DoubleRange and IntRange
  • added ParameterConverter.cs for default parameter conversion
File size: 1.4 KB
Line 
1using System.IO;
2using Newtonsoft.Json;
3using Newtonsoft.Json.Linq;
4
5namespace HeuristicLab.JsonInterface {
6  /// <summary>
7  /// Custom json writer for own formatting for templates.
8  /// It collapses arrays into a single line.
9  /// </summary>
10  internal class CustomJsonWriter : JsonTextWriter {
11    private bool isRangeArray = false;
12    public override void WriteStartArray() {
13     
14      if (isRangeArray) base.Formatting = Formatting.None;
15      base.WriteStartArray();
16    }
17
18    public override void WritePropertyName(string name) {
19      base.Formatting = Formatting.Indented;
20      base.WritePropertyName(name);
21      isRangeArray = name == nameof(JsonItem.Range) || name == nameof(JsonItem.Value);
22    }
23
24    public override void WriteStartObject() {
25      base.Formatting = Formatting.Indented;
26      base.WriteStartObject();
27    }
28
29    public override void WriteEndObject() {
30      base.Formatting = Formatting.Indented;
31      base.WriteEndObject();
32    }
33
34    public CustomJsonWriter(TextWriter writer) : base(writer) { }
35
36    public static string Serialize(JToken token) {
37      JsonSerializer serializer = new JsonSerializer();
38      StringWriter sw = new StringWriter();
39      CustomJsonWriter writer = new CustomJsonWriter(sw);
40      writer.Formatting = Formatting.Indented;
41      serializer.Serialize(writer, token);
42      return sw.ToString();
43    }
44  }
45}
Note: See TracBrowser for help on using the repository browser.