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

  • Home
  • SEARCH
  • 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 9207515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:24:17+00:00 2026-06-18T00:24:17+00:00

Given a set of static, nested classes such as this: public static class LocalSiteMap

  • 0

Given a set of static, nested classes such as this:

public static class LocalSiteMap
{
    public static class Navigation
    {
        public static readonly string Home = "homePage";

        public static class PageIds
        {
            public static class ShowManagement
            {
                public static readonly string Index = "showManagement";
            }

            public static class Shows
            { 
                public static readonly string Create = "addShows";
            }
        }

        public static class Actors
        {
            public static readonly string Details = "actorDetailsForm";
            public static readonly string History= "historyDetailsForm";
        }
    }
}

I would like to build an equivalent JSON string, such as this:

{ 'localSiteMap' : {
    { 'navigation': {
        'home': 'homePage',
        'pageIds': {
            'showManagement': {
                'index': 'showManagement'
            },
            'shows': {
                'create': 'addShows'
            }
        },
        'actors': {
            'details': 'actorDetailsForm',
            'history': 'historyDetailsForm'
        }
    }
}

Each nested class results in a nested object in the JSON. Each string property results in a string Key/Value pair in the JSON.

I know I can reflect over the root Static Class and build a JSON string pretty easily (and that’s the approach I’m currently taking), but I wondered if there was a more elegant way to do it. For example, if this were an instance anonymous type then I could serialise it pretty easily.

Some background: this is a set of constants to be passed via a WebAPI Controller to a Single Page Application running in a browser. Having the same set of page identifiers available in both C# (Server) and JS (Client) worlds is very useful for browser automation tests which use the Page Object pattern.

The ‘LocalSiteMap’ static class is already baked in to a fairly mature project, so changing it to a instance classes or an anonymous type instead isn’t really an option for me.

  • 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-18T00:24:18+00:00Added an answer on June 18, 2026 at 12:24 am

    The issue with building a string here is you have to ensure that the syntax is 100% correct, opening and closing braces, adding commas etc. All possible but quite a hassle.

    A solution to avoid magic strings altogether is to use LINQ-to-XML as an intermediary translation step. Like this:

    public static class NestedStaticClassWithStringPropertiesJsoniser
    {
        public static string GetJson(this Type type)
        {
            XElement rootXml = new XElement(type.Name);
            XElement xmlContent = CreateXmlTree(rootXml, type);
    
            string jsonString = JsonConvert.SerializeXNode(xmlContent);
    
            return jsonString;
        }
    
        public static XElement CreateXmlTree(XElement parent, Type type)
        {
            AddStringProperties(parent, type);
    
            AddNestedClasses(parent, type);
    
            return parent;
        }
    
        private static void AddNestedClasses(XElement parent, Type type)
        {
            var subTypes = type.GetNestedTypes();
    
            foreach (var subType in subTypes)
            {
                var newElement = new XElement(subType.Name);
                var subTree = CreateXmlTree(newElement, subType);
                parent.Add(subTree);
            }
        }
    
        private static void AddStringProperties(XElement parent, Type type)
        {
            var properties = type.GetFields();
            foreach (var property in properties)
            {
                var propertyElement = new XElement(property.Name);
                propertyElement.SetValue(property.GetValue(null));
                parent.Add(propertyElement);
            }
        }
    }
    

    It can easily be used like this:

    string json = typeof(ATypeWithNestedStaticClasses).GetJson();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set some public static constants on a class conditionally by
Given the following code... class Program { static void Main(string[] args) { Foo foo
Given the following method: public static void SetPropertyValue(object target, string propName, object value) {
Given a class like below: public class LoginInfo { public int UserId; public string
Given the following Java code: public class Test { static private class MyThread extends
Given the following code: public class Product { public Guid Id {get;set;} } public
Given the following code: public static class Super { public static class Inner {
Is it possible in MySQL to select from a static set of integers? Given
This is one of the solution of getting true or false from given set
Given the following code, I try to implement the public static Point operator +(int

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.