using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public int Height { get; set; }
public int Width { get; set; }
public int BlockScale { get; set; }
public int CenterTop { get; set; }
public int FieldTop { get; set; }
public class DefaultConfig
public FieldSize FieldSize { get; set; }
public FieldBlocks FieldBlocks { get; set; }
public static class JsonHelper
private static JObject jsonRoot;
public static void InitializeJsonHelper(string json)
jsonRoot = JObject.Parse(json);
public static async Task<T> GetObjectFromJson<T>(string section)
JProperty token = await FindTokenWithSectionName(section);
return token.Value.ToObject<T>();
public static async Task<List<T>> GetCollection<T>(string section)
JProperty token = await FindTokenWithSectionName(section);
return token.Value.ToObject<List<T>>();
public static async Task<string> AddCollectionToSection<T>(ICollection<T> objects, string section)
JProperty currentProperty = await FindTokenWithSectionName(section);
JProperty newProperty = new JProperty(currentProperty);
currentProperty.Replace(newProperty);
await SetReplaceTokenToSection(currentProperty, section);
return jsonRoot.ToString();
catch (Exception exception)
return exception.StackTrace;
public static async Task<string>AddObjectToSection<T>(T newObject, string section)
JToken token = await FindTokenWithSectionName(section);
JValue valueOfToken = new JValue(newObject);
token.Replace(valueOfToken);
await SetReplaceTokenToSection(token, section);
return jsonRoot.ToString();
private static async Task<JProperty> FindTokenWithSectionName(string section)
return await Task.Run(() =>
JProperty property = jsonRoot.Property(section);
private static Task SetReplaceTokenToSection(JToken newToken, string section)
JProperty property = jsonRoot.Property(section);
property.Value = newToken;
return Task.CompletedTask;
public static void Test()
JsonHelper.InitializeJsonHelper(GetJson());
var list = JsonHelper.GetCollection<Level>("Levels").Result;
var config = JsonHelper.GetObjectFromJson<DefaultConfig>("DefaultConfig").Result;
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];