public string JsonStringify(object?value) { var _json_context = new JsonObject.ConvertToJsonContext(maxDepth: 99, enumsAsStrings: true, compressOutput: true); string json_result = JsonObject.ConvertToJson(value, _json_context); return(json_result); // Json.Converter.ToJson(value); }
private static string ConvertToJson(object fromObj) { var context = new JsonObject.ConvertToJsonContext( maxDepth: 3, enumsAsStrings: false, compressOutput: true); return(JsonObject.ConvertToJson(fromObj, in context)); }
public static void TestConvertToJsonWithoutCompress() { var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: true, compressOutput: false); const string expected = @"{ ""type"": ""Alias"" }"; Hashtable hash = new Hashtable { { "type", CommandTypes.Alias } }; string json = JsonObject.ConvertToJson(hash, in context); Assert.Equal(expected, json); }
public static void TestConvertToJsonWithEnum() { var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: false, compressOutput: true); string expected = "{\"type\":1}"; Hashtable hash = new Hashtable { { "type", CommandTypes.Alias } }; string json = JsonObject.ConvertToJson(hash, in context); Assert.Equal(expected, json); context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: true, compressOutput: true); json = JsonObject.ConvertToJson(hash, in context); expected = "{\"type\":\"Alias\"}"; Assert.Equal(expected, json); }
public static void TestConvertToJsonBasic() { var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: false, compressOutput: true); string expected = "{\"name\":\"req\",\"type\":\"http\"}"; OrderedDictionary hash = new OrderedDictionary { { "name", "req" }, { "type", "http" } }; string json = JsonObject.ConvertToJson(hash, in context); Assert.Equal(expected, json); hash.Add("self", hash); json = JsonObject.ConvertToJson(hash, context); expected = "{\"name\":\"req\",\"type\":\"http\",\"self\":{\"name\":\"req\",\"type\":\"http\",\"self\":\"System.Collections.Specialized.OrderedDictionary\"}}"; Assert.Equal(expected, json); }
static void Main(string[] args) { // Create the Powershell instance, and adds command to be executed PowerShell ps = PowerShell.Create(); ps.AddCommand("Get-Process"); Collection <PSObject> _ps_results = ps.Invoke(); // Create a JSON Context Object JsonObject.ConvertToJsonContext _json_context = new JsonObject.ConvertToJsonContext(maxDepth: 12, enumsAsStrings: false, compressOutput: false); // Converts the PSObject into JSON string json_result = JsonObject.ConvertToJson(_ps_results, _json_context); // Outputs the result to console Console.WriteLine(json_result); }
public static void TestConvertToJsonCancellation() { var source = new CancellationTokenSource(); var context = new JsonObject.ConvertToJsonContext( maxDepth: 1, enumsAsStrings: true, compressOutput: false, Newtonsoft.Json.StringEscapeHandling.Default, targetCmdlet: null, source.Token); source.Cancel(); Hashtable hash = new Hashtable { { "type", CommandTypes.Alias } }; string json = JsonObject.ConvertToJson(hash, in context); Assert.Null(json); }