[C#] Array of components

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [x] Documentation issue or request

Current behavior

Given that the current deserialization happens with Newtonsoft.Json inside IContentsClient.GetAsync(...) of a certain mapped C# class(of a schema) I was wondering what an array of different components whould need to look like in order to be mapped correctly to a C# array. Is there any way to accomplish this?

Environment

  • [X] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Version: [8.4.2]

Hi,

it depends. If all your components have the same type. It is relatively easy.

e.g.

class Article : Content<ArticleData> {}

class ArticleData
{
   [JsonConverter(typeof(InvariantConverter))]
   public Section[] Sections { get; set; }
}

class Section 
{
  public string Title { get; set; }

  public string Text { get; set; }
}

or for localized articles

class ArticleData
{
   public Dictionary<string, Section> Sections { get; set; }
}

If you can assign multiple components you have several options:

  1. Write a custom json converter such as this one: https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Converters/JsonInheritanceConverter.cs
  2. Use a dynamic type such as JToken or JObject
  3. Use a super class that contains properties for all possible components.
1 Like

I tried with 3rd option given that I structured with increasing fields number per section type and it did work :slight_smile:
Meanwhile if I needed for the 1st or 2nd option, how would that work with Squidex.ClientLibrary? I can’t find any function that does return JToken or JObject or a way to pass in my custom converter.