Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17399 was 17394, checked in by dpiringe, 5 years ago

#3026:

  • deleted: ConvertableAttribute, DummyConverter, ObjectExtensions
  • renamed: CustomJsonWriter -> SingleLineArrayJsonWriter, JCInstantiator -> JsonTemplateInstantiator
  • added: JsonItemConverterFactory, UnsupportedJsonItem
  • IJsonItemConverter:
    • added two new properties: Priority and ConvertableType -> because converters are automatically collected by plugin infrastructure now
    • Extract, Inject references a root converter now -> typically an instance of JsonItemConverter -> to prevent cycles
  • JsonItemConverter:
    • now implements the interface IJsonItemConverter
    • is now a dynamic class
    • is only instantiable with an factory (JsonItemConverterFactory)
    • still has the old (but now public) static methods Extract and Inject (without ref param IJsonItemConverter root) -> creates instance with factory and calls methods of instance
    • removed register and unregister methods, because the factory collects all converters automatically now (on first call of Create)
    • has cycle detection for Extract and Inject
    • renamed method Get to GetConverter
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 SingleLineArrayJsonWriter : 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 SingleLineArrayJsonWriter(TextWriter writer) : base(writer) { }
35
36    public static string Serialize(JToken token) {
37      JsonSerializer serializer = new JsonSerializer();
38      StringWriter sw = new StringWriter();
39      SingleLineArrayJsonWriter writer = new SingleLineArrayJsonWriter(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.