PWL
PWL

Reputation: 466

Nested list of constant strings in C#

Any way I can store this data in a clean way, and preferably use variable names instead of strings as keys to avoid typos? E.g. UNITED_STATES = "201" instead of "United States" = "201".

{
    "countries": {
         "id": "123",
         "data" {
             "United States": "201"
             "Canada": "202",
         }
    },
    "departments": { ... }
}

I started with KeyValuePairs like this, but nesting data in here seems like a bad idea.

private static readonly List<KeyValuePair<string, string>> CategoryIds = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("Countries", "123"),
    new KeyValuePair<string, string>("Departments", "124")
};

Upvotes: 0

Views: 260

Answers (5)

Siavash Rostami
Siavash Rostami

Reputation: 1933

NJsonSchema is a library that will enable you to generate code in csharp as well as few other languages from a standard json schema. It is very powerful and configurable, and can pave most of the way on your behalf. But as i said it will expect an standard json schema as for the source of generation.

var schema = NJsonSchema.JsonSchema4.FromFileAsync(filename);
var generator = new CSharpGenerator(schema.Result);
var file = generator.GenerateFile();

Above is the minimum amount of code required to generate csharp classes from json schema. you can define settings and pass to the generator function to service your special needs of course.

github page for this library: NJsonSchema github

Nuget page: NJsonSchema Nuget

Upvotes: 0

eocron
eocron

Reputation: 7546

Two approaches to deserialize JSON here.

Strong typing approach (good approach):

public class A
{
     public B Countries {get;set;}
     public C Departments {get;set;}
}

public class B
{
     public int Id {get;set;}
     public D Data {get;set;}
}

...

var result = JsonConvert.DeserializeObject<A>(json);

You create DTO objects manually and just expect them to deserialize successfully.

Dynamic approach (bad but sometimes acceptable approach):

dynamic result = JsonConvert.DeserializeObject(json);

var data = result.countries.data;

You create some "bag of things" (dynamic is basically a bunch of hierarchical Dictionary wrapped into syntax sugar cane), don't really care about all of them, and just want some of its properties.

Upvotes: 1

ElasticCode
ElasticCode

Reputation: 7875

You can create a public class as below and you can then call country value like CountriesConstants.UNITED_STATES in your code and if you need to change the value just update it in CountriesConstants class

public class CountriesConstants
{
    public const string UNITED_STATES = "201";
    public const string Canada = "202";
    //Add More
}

Upvotes: 0

Rahul
Rahul

Reputation: 77896

You can use a dictionary<k,v> for this purpose along with a enum like below probably

enum CountryVal
{
 UnitesStates,
 Canada
}

With a model structure like

public class Countries
{
    public string id { get; set; }
    public Dictionary<CountryVal, int> Data { get; set; }
}

public class Departments
{
    public string id { get; set; }
}

public class RootObject
{
    public Countries countries { get; set; }
    public Departments departments { get; set; }
}

Upvotes: 0

mortb
mortb

Reputation: 9869

Maybe you could use json.net JObject?

It allows you to work with dynamic objects and convert them to and from json strings

Documentation for JObject https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm

Nuget: https://www.nuget.org/packages/Newtonsoft.Json/

Upvotes: 0

Related Questions