14

I am passing a JSON payload to an API Controller, and one of the fields is dynamic because the field needs to be passed again as a JSON string to another API. The dotnet core 3.1 middle layer shouldn't be concerned with typing, as the payload will change.

This is the object that is passed into the API Controller:

    public class GitHubAction
    {
        [JsonProperty("Title")]
        public string Title { get; set; }

        [JsonProperty("Enabled")]
        [JsonConverter(typeof(BooleanParseStringConverter))]
        public bool Enabled { get; set; }

        [JsonProperty("Action")]
        [JsonConverter(typeof(ExpandoObjectConverter))]
        public dynamic Action { get; set; }
    }

Here is a picture of that dynamic object looks like in VSCode:

enter image description here

When I use JsonConvert.SerializeObject(x.Action); the string result isn't being properly converted, but instead serializes to ValueKind: "{\"ValueKind\":1}".

What I want to get is the action object value as a JSON string, which should look like "{"createRepository":{"onboarding":{"service":{"organization":"foo","repositoryName":"foo-2-service","description":"A test service."}}}}"

Is there a simple solution for serializing a dynamic object?

Todd Worden
  • 193
  • 1
  • 3
  • 9
  • 1
    Is this using `Newtonsoft.Json`, or `System.Text.Json`? – yaakov Feb 29 '20 at 12:45
  • 5
    `ValueKind` is from System.Text.Json, but `JsonConvert` is from Newtonsoft. It's quite possible you can't mix the 2 here. Try using `JsonSerializer.Serialize` instead of `JsonConvert.SerializeObject`. – Todd Menier Feb 29 '20 at 15:11
  • There is a property named `ValueKind` of `JsonElement`: [`JsonElement.ValueKind`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonelement.valuekind?view=netcore-3.1). So *probably* your `dynamic Action` is actually a `JsonElement`, can you confirm please? A [mcve] showing the JSON and deserialization code would be ideal. – dbc Feb 29 '20 at 20:09
  • The payload coming into the controller is being serialized using `System.Text.Json` pretty sure. I am not wiring up Newtonsoft in Startup, but I am doing some internal stuff using Newtonsoft else where in the code. And yes, it is a JsonElement. The POCO object I have in the OP might be misleadning. – Todd Worden Mar 01 '20 at 13:23

5 Answers5

26

I had the same problem and I fixed it this way

using System.Text.Json;

string serializedObject = JsonSerializer.Serialize(x.Action); //Instead of use JsonConvert.SerializeObject(x.Action);

Instead of Newtonsoft.Json you have to use System.Text.Json.

JPocoata
  • 634
  • 1
  • 11
  • 24
5

Use Newtonsoft.Json instead of default System.Text.Json

Add Microsoft.AspNetCore.Mvc.NewtonsoftJson package from NUGET and services.AddControllers().AddNewtonsoftJson() in Startup.cs => ConfigureServices()

Arsman Ahmad
  • 2,000
  • 1
  • 26
  • 34
Evgeniy Gubin
  • 51
  • 1
  • 2
2

You can use Newtonsoft.Json.Converters.ExpandoObjectConverter to help you with this.

dynamic action = new ExpandoObject();
action.YourProperty = new ...;
var converter = new ExpandoObjectConverter();
var jsonString = JsonConvert.SerializeObject(action, converter);
0

Try this

var jsonString=s.Action.ToString();

This will give you the json string

Kiran B
  • 683
  • 10
  • 21
-1

If you want to use Newtonsoft.Json then follow this instructions :

in using section use this :

using Newtonsoft.Json;

Then

string serializedObject = JsonConvert.SerializeObject(value);

and for Deserializing use :

var desrializedObject = JsonConvert.DeserializeObject<T>(serializedObject);
Ali Mahmoodi
  • 858
  • 10
  • 14