2

I have Dictionary, which contains json paths and values:

Logging.Console.IncludeScopes = true
Logging.LogLevel.Microsoft = "Warning"
Logging.LogLevel.System= "Warning"
Sort.TypeOrder = "asc"

I want to generate json from this dictionary.

{
  "Logging": {
    "Console": {
      "IncludeScopes": true
    },
    "LogLevel": {
      "Microsoft": "Warning",
      "System": "Warning"
    }
  },
  "Sort": {
    "TypeOrder": [
      "asc"
    ]
  }
}

How can I achieve this?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
Dadroid
  • 1,444
  • 15
  • 16
  • Not exactly what you want but maybe this gives you an idea anyhow: https://stackoverflow.com/q/4861138/6996150 – johey Nov 28 '19 at 11:34
  • Does this answer your question? [How do I convert a dictionary to a JSON String in C#?](https://stackoverflow.com/questions/5597349/how-do-i-convert-a-dictionary-to-a-json-string-in-c) – Pavel Anikhouski Nov 28 '19 at 11:35
  • Object can be generated out of box by any tool like `NewtonSoft.Json` byt question here how the `TypeOrder` is converted to an array? Can the existing class structure be updated to add the JsonConverter attribute over property or class? – user1672994 Nov 28 '19 at 11:35
  • @johey it's about generating json from simple key value dictionary, I have path in key. – Dadroid Nov 28 '19 at 11:38
  • @PavelAnikhouski I've seen it, but it's not what I want. – Dadroid Nov 28 '19 at 11:40

2 Answers2

0

You can't serialize the whole Logging object. My suggestion is to create a custom class for logging settings, use it for both setting log and output JSON.

public class LoggingSettings
{
    public bool includeScopes;
    public LogLevel logLevel;
    public int day;
}

var settings = new LoggingSettings
        {
            includeScopes = true,
            typeOrder = "asc",
            logLevel = new LogLevel
            {
                microsoft = "Warning",
                system = "Warning"
            }
        };

Serialization:

var json = new JavaScriptSerializer().Serialize(settings);
Console.WriteLine(json);

Usage when setting log:

Logging.Console.IncludeScopes = settings.includeScopes;
Logging.LogLevel.Microsoft = settings.logLevel.Microsoft;
Logging.LogLevel.System= settings.logLevel.System;
Sort.TypeOrder = settings.typeOrder;
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
  • I said, it's Dictionary, not object. Logging.Console.IncludeScopes is a key and true is a value of it in Dictionary. – Dadroid Nov 28 '19 at 11:41
  • @Dadroid He's saying that you shouldn't be using the dictionary for serialization because it's not going to do what you want it to do. – Corey Nov 28 '19 at 11:43
0

As your key contains dynamic path for Jobject so you could use a recursive function to achieve this.

Finally serialize the output dictionary which should produce the desired json.

See here

private static void CreateJSONFromDictionary(string[] keys, int index, string value, IDictionary<string, object> dict)
{
    var key = keys[index];

    if (keys.Length > index + 1)
    {
        object childObj;
        IDictionary<string, object> nestedDict;
        if (dict.TryGetValue(key, out childObj))
        {
            nestedDict = (IDictionary<string, object>)childObj;
        }
        else
        {
            nestedDict = new Dictionary<string, object>();
            dict[key] = nestedDict;
        }

        CreateJSONFromDictionary(keys, index + 1, value, nestedDict);

    }
    else
    {
        dict[key] = value;
    }
}

Use this

var output = new Dictionary<string, object>();

foreach (var kvp in dict)
{
     var keys = kvp.Key.Split('.');
     CreateJSONFromDictionary(keys, 0, kvp.Value, output);
}
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42