0

I'm trying to output a JSON file. I am using netcoreapp3.1 along with the Newtonsoft.Json NuGet. Here is my code where I enter all the information:

    private BlockWrapper GetBlock()
    {
        var blockWrapper = new BlockWrapper
        {
            blocks = new List<IntBlock>
            {
                new SectionBlockBlock
                {
                    text = new TextBlock
                    {
                        type = "mrkdwn",
                        text = "Hello! I am Multiuse Kat. How can I help you ?"
                    }
                },
                new ActionsBlockBlock
                {
                    elements = new ElementBlock[]
                    {
                        new ElementBlock
                        {
                            type = "button",
                            text = new TextBlock
                            {
                                type = "plain_text",
                                text = "Help"
                            },
                            style = "primary",
                            value = "click_me_123"
                        }
                    }
                },
            }
        };
        return blockWrapper;
    }

My models look like this. Notice I am using an interface with IntBlock:

public class BlockWrapper
{
    public List<IntBlock> blocks { get; set; }
}
public class SectionBlockBlock : IntBlock
{
    public string type { get; } = "section";
    public string blockId { get; set; }
    public TextBlock text { get; set; }
}
public class ActionsBlockBlock : IntBlock
{
    public string type { get; } = "actions";
    public string blockId { get; set; }
    public ElementBlock[] elements { get; set; }
}
public class TextBlock
{
    public string type { get; set; }
    public string text { get; set; }
    public bool? emoji { get; set; }
}
public class ElementBlock
{
    public string type { get; set; }
    public string action_id { get; set; }
    public TextBlock text { get; set; }
    public string value { get; set; }
    public string style { get; set; }
}


public interface IntBlock { }

When I return new JsonResult(GetBlock()); I output this:

enter image description here

I'm assuming this has something to do with the interface of IntBlock. Can someone tell me how to fix this? Thanks!

  • 1
    What MVC framework and version are you using? – dbc Jan 21 '20 at 00:53
  • 1
    Is it [tag:asp.net-core-3.0] or [tag:asp.net-core-3.1]? You have nearly provided a [mcve], details about the framework would make your question answerable. We basically need to be able to figure out what JSON serializer you are using. – dbc Jan 21 '20 at 01:08
  • Thank you. I have edited with these details: I am using netcoreapp3.1 along with the Newtonsoft.Json NuGet – goodkidbadmAArky Jan 21 '20 at 03:02
  • 1
    *along with the Newtonsoft.Json NuGet* - OK but did you also add the `Microsoft.AspNetCore.Mvc.NewtonsoftJson` nuget and call `.AddNewtonsoftJson();` in `ConfigureServices()`? If not, you'll be using `System.Text.Json` instead, which I believe will serialize property values according to their declared type, not their actual type. See: [Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?](https://stackoverflow.com/a/55666898/3744182). – dbc Jan 21 '20 at 03:04
  • That worked! Thank you. – goodkidbadmAArky Jan 21 '20 at 03:10
  • Close as a duplicate then? – dbc Jan 21 '20 at 03:55

1 Answers1

1

Try use this

return JsonConvert.SerializeObject(GetBlock());

i do always use above approach and all work fine.

And also, Your interface properties are null

public interface IntBlock { }

Therefore, your collection type is empty interface and serializer wouldn't be able to find any property to serialize it.

i think below code should work

public class ActionsBlockBlock : IntBlock
{
    IntBlock.type { get; } = "actions";
    IntBlock.blockId { get; set; }
    IntBlock.ElementBlock[] elements { get; set; }
}
public interface IntBlock 
{ 
    public string type { get; set; }
    public string blockId { get; set; }
    public ElementBlock[] elements { get; set; }
}

do above schema for all of your class and try again . i think it works .

A Farmanbar
  • 4,381
  • 5
  • 24
  • 42
  • `return JsonConvert.SerializeObject(GetBlock());` will result in double-serialized JSON. See: [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179/3744182). – dbc Jan 21 '20 at 03:55
  • @abc It does not double serialize it the response type json format doing that and if you don't you have to have stream format. – A Farmanbar Jan 21 '20 at 04:12