Last active
May 5, 2022 16:38
-
-
Save pmatthews05/034ca0920e589f57ea6f4b8280bc7220 to your computer and use it in GitHub Desktop.
Uploading Json String to Cosmos DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.Cosmos; | |
public class Program | |
{ | |
public static async Task Main() | |
{ | |
IDictionary<string, object> resultRow = new Dictionary<string, object>(); | |
resultRow.Add("AStringValue", "A value to save"); | |
resultRow.Add("ALongValue", 1237396038044506331); | |
resultRow.Add("ABooleanValue", false); | |
resultRow.Add("AIntValue", 68); | |
ExtensionResult er = new ExtensionResult(); | |
Type T = er.GetType(); | |
var jsonString = ManipulateJsonString(resultRow, T.AssemblyQualifiedName, "AnExampleContentType"); | |
await AddToCosmos(jsonString); | |
} | |
public static string ManipulateJsonString(IDictionary<string,object> resultRow, string assemblyQualifiedName, string contentTypeName){ | |
string json = JsonConvert.SerializeObject(resultRow); | |
Type objectType = Type.GetType(assemblyQualifiedName); | |
dynamic item = JsonConvert.DeserializeObject(json, objectType); | |
item.ResultContentType = contentTypeName; | |
string jsonString = JsonConvert.SerializeObject(item); | |
Console.WriteLine(jsonString); | |
return jsonString; | |
} | |
public static async Task AddToCosmos(string jsonString){ | |
//Cosmos Emulator | |
string primaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; | |
string endpointUri = "https://localhost:8081"; | |
CosmosClient cc = new CosmosClient(endpointUri, primaryKey); | |
string databaseName = "ExampleDatabase"; | |
Container container = cc.GetContainer(databaseName, "ExampleContainer"); | |
await container.CreateItemAsync(jsonString); | |
} | |
} | |
public class BaseResult | |
{ | |
private string _Id; | |
[JsonProperty(PropertyName = "id")] | |
public string Id | |
{ | |
get | |
{ | |
if (string.IsNullOrEmpty(_Id)) | |
{ | |
_Id = Guid.NewGuid().ToString(); | |
} | |
return _Id; | |
} | |
set { _Id = value; } | |
} | |
[JsonProperty(PropertyName ="AStringValue")] | |
public string AStringValue { get; set; } | |
[JsonProperty(PropertyName ="ResultContentType")] | |
public string ResultContentType { get; set; } | |
[JsonProperty(PropertyName ="ALongValue")] | |
public string ALongValue { get; set; } | |
public override string ToString() | |
{ | |
return JsonConvert.SerializeObject(this); | |
} | |
} | |
public class ExtensionResult : BaseResult | |
{ | |
[JsonProperty(PropertyName ="AIntValue")] | |
public string AIntValue { get; set; } | |
public override string ToString() | |
{ | |
return JsonConvert.SerializeObject(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.Cosmos; | |
public class Program | |
{ | |
public static async Task Main() | |
{ | |
IDictionary<string, object> resultRow = new Dictionary<string, object>(); | |
resultRow.Add("AStringValue", "A value to save"); | |
resultRow.Add("ALongValue", 1237396038044506331); | |
resultRow.Add("ABooleanValue", false); | |
resultRow.Add("AIntValue", 68); | |
ExtensionResult er = new ExtensionResult(); | |
Type T = er.GetType(); | |
var extensionResult = ManipulateObject(resultRow, "AnExampleContentType"); | |
await AddToCosmos(extensionResult); | |
} | |
public static ExtensionResult ManipulateObject(IDictionary<string,object> resultRow, string contentTypeName){ | |
string json = JsonConvert.SerializeObject(resultRow); | |
ExtensionResult item = JsonConvert.DeserializeObject<ExtensionResult>(json); | |
item.ResultContentType = contentTypeName; | |
return item; | |
} | |
public static async Task AddToCosmos(ExtensionResult extensionResult){ | |
//Cosmos Emulator | |
string primaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; | |
string endpointUri = "https://localhost:8081"; | |
CosmosClient cc = new CosmosClient(endpointUri, primaryKey); | |
string databaseName = "ExampleDatabase"; | |
Container container = cc.GetContainer(databaseName, "ExampleContainer"); | |
await container.CreateItemAsync(extensionResult); | |
} | |
} | |
public class BaseResult | |
{ | |
private string _Id; | |
[JsonProperty(PropertyName = "id")] | |
public string Id | |
{ | |
get | |
{ | |
if (string.IsNullOrEmpty(_Id)) | |
{ | |
_Id = Guid.NewGuid().ToString(); | |
} | |
return _Id; | |
} | |
set { _Id = value; } | |
} | |
[JsonProperty(PropertyName ="AStringValue")] | |
public string AStringValue { get; set; } | |
[JsonProperty(PropertyName ="ResultContentType")] | |
public string ResultContentType { get; set; } | |
[JsonProperty(PropertyName ="ALongValue")] | |
public string ALongValue { get; set; } | |
public override string ToString() | |
{ | |
return JsonConvert.SerializeObject(this); | |
} | |
} | |
public class ExtensionResult : BaseResult | |
{ | |
[JsonProperty(PropertyName ="AIntValue")] | |
public string AIntValue { get; set; } | |
public override string ToString() | |
{ | |
return JsonConvert.SerializeObject(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment