0

I have an object: Link : Entity that sometimes should be serialized to, or from, just a plain string of text.

Other times its should just serialize like any other entity, using the attributes and class/property data under Link and Entity.

Below is example psudo-code for what I'd want to do in a JsonConverter class for the write function, but I can't figure out how... is this possible?

I need to do this for read and write btw.

protected override void Write(Utf8JsonWriter writer, Type type, Link value) 
{
    if (value.Types.SequenceEqual(DefaultTypeNames)
      && value.MediaType == DefaultMediaType
      && value.Rel == null
      && value.Name == null
      && value.HrefLang == null
      && value.Context == null
      && value.Id == null
      && value.Height == null
      && value.Width == null
      && value.Preview == null) 
    {
        writer.WriteStringValue(value.Href);
    }
    else 
    {
        JsonSerializer.SerializeDefault(writer, value, type);
    }
}      

SerializeDefault would be the logic I'm trying to find, afaik this function doesn't exist.

I have also tried using source generation, but I need to use init only setters, and source generation doesn't support those as of yet.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
SuperMeip
  • 65
  • 3
  • 9
  • I don't understand what are you doing. If you are using JsonSerializer is because you want get something to put inside something – Leandro Bardelli Dec 09 '21 at 14:44
  • Sorry @LeonardoBardelli but I don't understand your confusion. Sometimes I want a Link to serialize to just a text json element, like "/hello" sometimes I want it to be a json object element like { href:"/hello", otherData...} – SuperMeip Dec 09 '21 at 15:43
  • there is no json object. A json is the string representation of an object. Even more, is a javascript object, a class. – Leandro Bardelli Dec 09 '21 at 15:51
  • Does [How to use default serialization in a custom System.Text.Json JsonConverter?](https://stackoverflow.com/q/65430420/3744182) answer your question? – dbc Dec 09 '21 at 23:59
  • @dbc no sorry, I did try that and it has lead to this error https://stackoverflow.com/questions/70252828/use-system-text-json-to-serialize-an-object-as-a-single-string-conditionally – SuperMeip Dec 10 '21 at 00:01
  • 1
    @SuperMeip - The answer from [How to use default serialization in a custom System.Text.Json JsonConverter?](https://stackoverflow.com/q/65430420/3744182) cached a converter for performance reasons as recommended -- but this seems to conflict with the new [source generation](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-source-generation?pivots=dotnet-6-0) functionality. I removed the caching and now the question and answer would seem to apply here also -- though since your question lacks a [mcve] it's hard to say for sure. – dbc Dec 10 '21 at 17:53
  • 1
    With that having been done, is this how a duplicate of [How to use default serialization in a custom System.Text.Json JsonConverter?](https://stackoverflow.com/q/65430420/3744182)? – dbc Dec 14 '21 at 17:03

0 Answers0