Skip to content

Instantly share code, notes, and snippets.

@0x414c49
Created December 19, 2019 11:22
Show Gist options
  • Save 0x414c49/febddc2d767adc0dac48da16649edcb5 to your computer and use it in GitHub Desktop.
Save 0x414c49/febddc2d767adc0dac48da16649edcb5 to your computer and use it in GitHub Desktop.
CSharp scripting with nuget referencing.
// Make sure you define the version of Nuget package for caching!
#r "nuget: Newtonsoft.Json, 12.0.3"
using System;
using Newtonsoft.Json;
class Product
{
public Guid Id{get;set;}
public string Name{get;set;}
public bool Enabled{get;set;}
}
var productObj = new Product {
Id = Guid.NewGuid(),
Name = "FooBar",
Enabled = true
};
Console.WriteLine(JsonConvert.SerializeObject(productObj));
@omidkrad
Copy link

You can also deserialize JSON to a dynamic object:

using Newtonsoft.Json.Linq;

dynamic json = JObject.Parse("{'Id':1,'Name':'FooBar','Enabled':true}");
Console.WriteLine($"Id: {json.Id}");
Console.WriteLine($"Name: {json.Name}");
Console.WriteLine($"Enabled: {json.Enabled}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment