using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public string CompanyName { get; set; }
public double IPO { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public string StartDate { get; set; }
public List<Product> Products = new List<Product>();
public List<NewsArticle> CompanySpecificNewsArticles = new List<NewsArticle>();
public List<string> EavesDropperList = new List<string>();
[JsonProperty("ProductName")]
public string ProductName { get; set; }
public string Type { get; set; }
[JsonProperty("Content")]
public string Content { get; set; }
public class JsonExtensions
public static T LoadFromFileOrCreateDefault<T>(string path, JsonSerializerSettings settings = null) where T : new()
var serializer = JsonSerializer.CreateDefault(settings);
using (var file = File.OpenText(path))
return (T)JsonSerializer.CreateDefault(settings).Deserialize(file, typeof(T));
catch (FileNotFoundException)
public static void SaveToFile<T>(T root, string path, Formatting formatting = Formatting.None, JsonSerializerSettings settings = null)
using (var file = File.CreateText(path))
using (var writer = new JsonTextWriter(file) { Formatting = formatting })
JsonSerializer.CreateDefault(settings).Serialize(writer, root);
readonly string directory = "";
readonly string fileName = "comp57939301.json";
string Path { get { return System.IO.Path.Combine(directory, fileName); } }
Console.WriteLine(File.ReadAllText(Path));
File.WriteAllText(Path, GetJson());
Console.WriteLine(File.ReadAllText(Path));
public void CreateNewCompany()
Company company = new Company
CompanyName = "test company name",
Category = "test category name",
Description = "test description",
AddProductListItemsToFinishedJSON(company);
AddNewsArticlesListItemsToFinishedJSON(company);
var root = JsonExtensions.LoadFromFileOrCreateDefault<JObject>(Path);
var companiesArray = (JArray)root["Companies"] ?? (JArray)(root["Companies"] = new JArray());
companiesArray.Add(JObject.FromObject(company));
JsonExtensions.SaveToFile(root, Path, Formatting.Indented);
void AddProductListItemsToFinishedJSON(Company localcompany) { }
void AddNewsArticlesListItemsToFinishedJSON(Company localcompany) { }
""Description"":""A video game company"",
""StartDate"":""1-1-2000"",
""CompanySpecificNewsArticles"":[
""Content"":""This company has had a very good year!""
""Content"":""This company has had a very bad year!""
""Content"":""This company is doing okay, I guess""
""CompanySpecificEavesdropper"":[
""Content"":""This company has had a very good year!""
""Content"":""This company has had a very bad year!""
""Content"":""This company is doing okay, I guess!""
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];