0

I have pretty much the same question as this one: Get Raw json string in Newtonsoft.Json Library but now using the Text.Json.Serialization in .NET Core 3.1, I struggle to find the equivalent of JRaw.

I want to have an extensible object like:

{
    "id": "myId",
    "name": "name",
    "description": "my description",
    "extensions": {
        "unit": "C°",
        "minVal": "0",
        "maxVal": "100",
        "precision": "1",
        "enum": ["str1", "str2"],
         ...
    }
}

I want to get the Id, Name and Description set in an object, but the extensions is a bunch of properties which can be whatever. So I want to keep the RAW Json for this extensions.

public class MyData
{
    public string id { get; set; }
    public string name { get; set; }
    public string description { get; set; }

    [JsonConverter(typeof(RawJsonStringConverter))]
    public string extensions { get; set; }
}

I am fighting with a custom converter as documented here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to

I tried with string, JsonElement, object. No success so far. Any idea?

EricBDev
  • 1,279
  • 13
  • 21
  • They have examples on how to translate Newtonsoft.Json to System.Text.Json here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to – Andy Sep 04 '20 at 14:59
  • yeah, I read that before, but what I am asking is not there. However, I just figured out that having JsonElement as type does the job – EricBDev Sep 04 '20 at 15:06
  • https://stackoverflow.com/a/60402592/1204153 – Andy Sep 04 '20 at 15:20
  • [`GetRawText()`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonelement.getrawtext?view=netcore-3.1) – Pavel Anikhouski Sep 04 '20 at 15:25
  • thanks for the comments, I already used GetRawText() in some other services. The combinaison of it with the custom converter as mentioned in Andy's link is a good mix with the JsonElement. – EricBDev Sep 08 '20 at 18:49

1 Answers1

0

Using JsonElement seems to be enough, without any custom converter:

public class MyData
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public JsonElement? Extensions { get; set; }
}

edit: JsonElement? as nullable is better: it allows JsonElement to be fully optional, if you have the corresponding settings in JsonSerializerOptions.

I am working with the following JsonSerializerOptions:

public static JsonSerializerOptions UpdateJsonSerializerOptions(this JsonSerializerOptions options, bool enumAsString = true)
{
    if (options == null)
        options = new JsonSerializerOptions();

    options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    options.IgnoreNullValues = true;
    options.WriteIndented = true;

    if (enumAsString)
    {
        // convert enums as strings
        options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
    }

    return options;
}

IgnoreNullValues is the important part to have the proper conversion when no extensions at all in the input. And CamelCase allows to stay with .NET property names (e.g 'Name') while the JSON will be with 'name'

EricBDev
  • 1,279
  • 13
  • 21