Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/SingleLineArrayJsonWriter.cs @ 18039

Last change on this file since 18039 was 17473, checked in by dpiringe, 5 years ago

#3026:

  • refactored inheritance structure of json items, now the default JsonItem is an abstract class without properties Value and Range -> splitted up into new interfaces
  • updated view models for new json item structure
  • updated SingleLineArrayJsonWriter
File size: 1.6 KB
RevLine 
[17280]1using System.IO;
2using Newtonsoft.Json;
3using Newtonsoft.Json.Linq;
4
[17284]5namespace HeuristicLab.JsonInterface {
[17353]6  /// <summary>
7  /// Custom json writer for own formatting for templates.
8  /// It collapses arrays into a single line.
9  /// </summary>
[17442]10  public class SingleLineArrayJsonWriter : JsonTextWriter {
[17280]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);
[17473]21      isRangeArray =
22        name == nameof(IConcreteRestrictedJsonItem<int>.ConcreteRestrictedItems) ||
23        name == nameof(IValueJsonItem.Value) ||
24        name == nameof(IMatrixJsonItem.RowNames) ||
25        name == nameof(IMatrixJsonItem.ColumnNames);
[17280]26    }
27
28    public override void WriteStartObject() {
29      base.Formatting = Formatting.Indented;
30      base.WriteStartObject();
31    }
32
33    public override void WriteEndObject() {
34      base.Formatting = Formatting.Indented;
35      base.WriteEndObject();
36    }
37
[17394]38    public SingleLineArrayJsonWriter(TextWriter writer) : base(writer) { }
[17280]39
40    public static string Serialize(JToken token) {
41      JsonSerializer serializer = new JsonSerializer();
42      StringWriter sw = new StringWriter();
[17394]43      SingleLineArrayJsonWriter writer = new SingleLineArrayJsonWriter(sw);
[17280]44      writer.Formatting = Formatting.Indented;
45      serializer.Serialize(writer, token);
46      return sw.ToString();
47    }
48  }
49}
Note: See TracBrowser for help on using the repository browser.