Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:59:48+00:00 2026-06-17T09:59:48+00:00

I have added a custom property to some of my objects like this: [JsonCustomRoot(status)]

  • 0

I have added a custom property to some of my objects like this:

[JsonCustomRoot("status")]
public class StatusDTO 
{
    public int StatusId { get; set; }
    public string Name { get; set; }
    public DateTime Created { get; set; }
}

The attribute is very simple:

public class JsonCustomRoot :Attribute
{
    public string rootName { get; set; }

    public JsonCustomRoot(string rootName)
    {
        this.rootName = rootName;
    }
}

The default output from JSON.NET when serializing an instance of an object is this:

{"StatusId":70,"Name":"Closed","Created":"2012-12-12T11:50:56.6207193Z"}

Now the question is: How can I add a root-node to the JSON with the value of the custom attribute like so:

{status:{"StatusId":70,"Name":"Closed","Created":"2012-12-12T11:50:56.6207193Z"}}

I have found several articles mentioning the IContractResolver interface, but I cannot grasp how to do it. My attempts include this unfinished piece of code:

protected override JsonObjectContract CreateObjectContract(Type objectType)
{
    JsonObjectContract contract = base.CreateObjectContract(objectType);

    var info = objectType.GetCustomAttributes()
                   .SingleOrDefault(t => (Type)t.TypeId==typeof(JsonCustomRoot));
    if (info != null)
    {
        var myAttribute = (JsonCustomRoot)info;
        // How can i add myAttribute.rootName to the root from here?
        // Maybe some other method should be overrided instead?
    }

    return contract;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:59:49+00:00Added an answer on June 17, 2026 at 9:59 am

    Here’s a solution specifically for Web API, which I am also using: RootFormatter.cs

    I wrote it based on Creating a JSONP Formatter for ASP.NET Web API.

    Instead of using a custom attribute I am reusing Title field of JsonObjectAttribute. Here’s a usage code:

    using Newtonsoft.Json
    
    [JsonObject(Title = "user")]
    public class User
    {
        public string mail { get; set; }
    }
    

    Then, add RootFormatter to your App_Start and register it as follows in WebApiConfig:

    GlobalConfiguration.Configuration.Formatters.Insert(0, new RootFormatter());
    

    I was able to get a wrapped response similar to WCF’s WebMessageBodyStyle.Wrapped:

    {"user":{
      "mail": "foo@example.com"
    }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a public static property in a class. The class has some custom
I have an NSOperationQueue to which i have added my custom class's objects(inherits from
I have this two objects - Magazine and Author (M-M relationship): public partial class
In Silverlight 3.0 I have added a custom behavior to some UIElement in Code
I have one ViewController, in this i have added one Custom UIView named as
I have a Custom Canvas(DesignerCanvas), user can add some controls to this canvas. Before
I have a custom class MyCustomType. In that class I have a property MyCustomProperty
I have a user control that has some public properties (like Dirty :boolean) and
I have a TextView . I have added custom links like @abc , #android
I have an activity with 2 fragments. I have added custom animations for fragment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.