public void TestEnumerable() { var array = (IEnumerable <dynamic>)DJson.Parse(@"[ { ""value"": 1 }, { ""value"": 2 }, { ""value"": 3 } ]"); Assert.Equal(3, array.Count()); Assert.Equal(2, array.First(x => x.Value == 2).Value); }
private static void AssertDynamicJson(DJson dynJson) { Assert.Equal(3, dynJson.CountMembers); Assert.Null(dynJson[3]); var gradeObj = (DJson)dynJson[2]; Assert.NotNull(gradeObj); Assert.Null(gradeObj["Dontknow"]); var grades = (DJson)gradeObj["grades"]; Assert.NotNull(grades); var obj90L = grades[0]; Assert.NotEqual(90, obj90L); Assert.Equal(90, (int)(long)obj90L); Assert.Equal(90, Convert.ToInt32(obj90L)); Assert.Equal(90, Convert.ToInt64(obj90L)); Assert.NotStrictEqual(90, grades[0]); Assert.NotStrictEqual(80, grades[1]); Assert.NotStrictEqual(100, grades[2]); Assert.NotStrictEqual(75, grades[3]); Assert.Equal("[{\"name\":\"John\"},[\"425-000-1212\",15],{\"grades\":[90,80,100,75]}]", JsonSerializer.Serialize(dynJson)); Assert.Equal("{\"name\":\"John\"}", JsonSerializer.Serialize(dynJson[0])); Assert.Equal("[\"425-000-1212\",15]", JsonSerializer.Serialize(dynJson[1])); Assert.Equal("{\"grades\":[90,80,100,75]}", JsonSerializer.Serialize(dynJson[2])); Assert.Equal("null", JsonSerializer.Serialize(dynJson[3])); }
private static void AssertDynamic(DJson dynJson) { dynamic dyn = dynJson; Assert.Equal(3, dyn.CountMembers); Assert.Null(dyn[3]); var gradeObj = dyn[2]; Assert.NotNull(gradeObj); Assert.Null(gradeObj.Dontknow); var grades = gradeObj.grades; Assert.NotNull(grades); Assert.Equal(90, grades[0]); Assert.Equal(80, grades[1]); Assert.Equal(100, grades[2]); Assert.Equal(75, grades[3]); Assert.Equal("[{\"name\":\"John\"},[\"425-000-1212\",15],{\"grades\":[90,80,100,75]}]", JsonSerializer.Serialize(dyn)); Assert.Equal("{\"name\":\"John\"}", JsonSerializer.Serialize(dyn[0])); Assert.Equal("[\"425-000-1212\",15]", JsonSerializer.Serialize(dyn[1])); Assert.Equal("{\"grades\":[90,80,100,75]}", JsonSerializer.Serialize(dyn[2])); Assert.Equal("null", JsonSerializer.Serialize((object)dyn[3])); }
public void TestReservedWords() { var json = DJson.Parse(@"{ ""int"": 123, ""string"": ""qwerty"" }"); Assert.Equal(123, json.@int); Assert.Equal("qwerty", json.@string); }
public void TestBool() { Assert.True((bool)DJson.Parse(@"{ ""value"": true }").value); Assert.False((bool)DJson.Parse(@"{ ""value"": false }").value); Assert.True((bool)DJson.Parse(@"{ ""value"": ""true"" }").value); Assert.False((bool)DJson.Parse(@"{ ""value"": ""false"" }").value); }
public void TestConvert() { var list = (List <int>)DJson.Parse(@"{ ""value"": [ 1, 2, 3, 4 ] }").Value; Assert.Equal(new List <int> { 1, 2, 3, 4 }, list); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { return; } filterContext.Result = DJson.Json(DResult.Error("该方法只允许Ajax调用!")); }
public void TestParse() { var dynJson = DJson.Parse(json); AssertDynamic(dynJson); AssertDynamicJson(dynJson); }
public void TestEnumerable() { var kvs = (IEnumerable <dynamic>)DJson.Parse(@"{ ""a"": { ""value"": 1 }, ""b"": { ""value"": 2 }, ""c"": { ""value"": 3 } }"); Assert.Equal(3, kvs.Count()); Assert.Equal(3, kvs.First(x => x.Name == "c").Value.Value); Assert.Equal(2, kvs.First(x => x.Value.Value == 2).Value.Value); }
public void TestEnumerator() { int value = 1; foreach (var item in DJson.Parse(@"[ 1, 2, 3 ]")) { Assert.Equal(value++, item); } }
public void Kukai() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\kukai.json")) { var hdKey = HDKey.FromMnemonic(Mnemonic.Parse((string)sample.mnemonic)) .Derive((string)sample.path); Assert.Equal(sample.address, hdKey.Address); } }
public void TestSByteNull() { var json1 = DJson.Parse(@"{ ""value"": -128 }"); Assert.Equal((sbyte?)-128, (sbyte?)json1.value); var json2 = DJson.Parse(@"{ ""value"": null }"); Assert.Null((sbyte?)json2.value); }
public async Task <dynamic> PostJson(string path, string content, CancellationToken cancellationToken = default) { var response = await HttpClient.PostAsync(path, new JsonContent(content), cancellationToken); await EnsureResponceSuccessfull(response); using (var stream = await response.Content.ReadAsStreamAsync()) { return(await DJson.ParseAsync(stream, DefaultOptions, cancellationToken)); } }
public void Atomex() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\atomex.json")) { var hdKey = HDKey.FromMnemonic(Mnemonic.Parse((string)sample.mnemonic)) .Derive((string)sample.path); Assert.Equal(sample.privateKey, hdKey.Key.GetBase58()); Assert.Equal(sample.address, hdKey.Address); } }
void test2() { string root = Application.StartupPath; string fp = Path.Combine(root, "test1.json"); string txt = File.ReadAllText(fp); DJson djson = DJson.From(txt); DJsonItem dJsonItem = djson["dynamicObj"]; txt += ""; }
public void Kukai() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\kukai.json")) { var password = string.IsNullOrEmpty(sample.password) ? "" : sample.password; var hdKey = HDKey.FromMnemonic(Mnemonic.Parse((string)sample.mnemonic), password) .Derive((string)sample.path); Assert.Equal(sample.address, hdKey.Address); } }
/// <summary> /// Send a GET request to the specified Uri and return the response body as a dynamic JSON. /// </summary> /// <param name="client">Http client.</param> /// <param name="requestUri">The Uri the request is sent to.</param> /// <param name="options">Options to control the behavior during reading.</param> /// <param name="cancellationToken">A token that may be used to cansel the read operation.</param> /// <returns></returns> public static async Task <dynamic> GetJsonAsync( this HttpClient client, Uri requestUri, JsonSerializerOptions options = null, CancellationToken cancellationToken = default) { using (var stream = await client.GetStreamAsync(requestUri)) { return(await DJson.ParseAsync(stream, options, cancellationToken)); } }
public SettingsFixture() { lock (Crit) { var settings = DJson.Read("../../../Rpc/settings.json"); Rpc = new TezosRpc(settings.node, 60); TestContract = settings.TestContract; TestDelegate = settings.TestDelegate; TestInactive = settings.TestInactive; } }
public void TestEd25519() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\ed25519.json")) { var hdKey = HDKey.FromSeed(Hex.Parse((string)sample.seed)) .Derive((string)sample.path); Assert.Equal(sample.privateKey, hdKey.Key.GetHex()); Assert.Equal(sample.chainCode, Hex.Convert(hdKey.ChainCode)); Assert.Equal(sample.pubKey, hdKey.PubKey.GetHex()); } }
public void TestEnumerator() { var i = 0; var keys = new List <string> { "a", "b", "c" }; foreach (var kv in DJson.Parse(@"{ ""a"": 0, ""b"": 1, ""c"": 2 }")) { Assert.Equal(keys[i], kv.Name); Assert.Equal(i++, kv.Value); } }
public void TestNistp256() { foreach (var sample in DJson.Read(@"..\..\..\Keys\HDKeys\Samples\nistp256.json")) { var hdKey = HDKey.FromSeed(Hex.Parse((string)sample.seed), ECKind.NistP256) .Derive((string)sample.path); Assert.Equal(sample.privateKey, hdKey.Key.GetHex()); Assert.Equal(sample.chainCode, Hex.Convert(hdKey.ChainCode)); Assert.Equal(sample.pubKey, hdKey.PubKey.GetHex()); TestPublicKeyDerivation(sample.path, sample.seed, ECKind.NistP256); } }
public void TestBoolNull() { var json1 = DJson.Parse(@"{ ""value"": false }"); Assert.False((bool?)json1.value); var json2 = DJson.Parse(@"{ ""value"": null }"); Assert.Null((bool?)json2.value); var json3 = DJson.Parse(@"{ }"); Assert.Null((bool?)json3.value); }
public SettingsFixture() { lock (Crit) { var settings = DJson.Read("../../../Rpc/settings.json"); Rpc = new TezosRpc(settings.node, 60); TestContract = settings.TestContract; TestEntrypoint = settings.TestEntrypoint; TestDelegate = settings.TestDelegate; TestInactive = settings.TestInactive; KeyHash = settings.KeyHash; BigMapId = settings.BigMapId; } }
public void TestSnakeCase() { var json = DJson.Parse(@"{ ""snake"": true, ""snake_case"": true, ""ip_endpoint"": true, ""user_id"": true }"); Assert.True(json.snake); Assert.True(json.Snake); Assert.True(json.snake_case); Assert.True(json.SnakeCase); Assert.True(json.ip_endpoint); Assert.True(json.IpEndpoint); Assert.True(json.IPEndpoint); Assert.True(json.user_id); Assert.True(json.UserId); Assert.True(json.UserID); }
public void TestCamelCase() { var json = DJson.Parse(@"{ ""camel"": true, ""camelCase"": true, ""ipEndpoint"": true, ""userId"": true }"); Assert.True(json.camel); Assert.True(json.Camel); Assert.True(json.camelCase); Assert.True(json.CamelCase); Assert.True(json.ipEndpoint); Assert.True(json.IpEndpoint); Assert.True(json.IPEndpoint); Assert.True(json.userId); Assert.True(json.UserId); Assert.True(json.UserID); }
public async Task CompareUnforgedFromFile() { var forge = new LocalForge(); var options = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, MaxDepth = 100_000, }; options.Converters.Add(new DateTimUtcTimezoneAppender()); var jsonComparer = new JsonElementComparer(true, true); var directories = Directory.GetDirectories("../../../Forging/operations"); JsonElement toJsonElement(object o) { // JSON serialize the unforged manager operation for the purpose of deserializing as JsonElement for comparison var serMop = JsonSerializer.Serialize(o, o.GetType(), options); // Deserialize the unforged manager operation to JsonElement for comparison return((JsonElement)DJson.Parse(serMop)); } foreach (var directory in directories) { var json = DJson.Read($"{directory}/unsigned.json", options); // Parse the op bytes var opBytes = Hex.Parse(File.ReadAllText($"{directory}/forged.hex")); // Unforge the op bytes var op = await forge.UnforgeOperationAsync(opBytes); // Serialize/deserialize each operation for the purpose of conversion to JsonElement for comparison var deserOps = op.Item2.Select(toJsonElement); // Assert branch Assert.Equal(json.branch, op.Item1); // Assert equivalent JSON operations Assert.Equal(json.contents.count, op.Item2.Count()); Assert.True(((JsonElement)json.contents).EnumerateArray().SequenceEqual(deserOps, jsonComparer)); } }
void test1() { string root = Application.StartupPath; string fp = Path.Combine(root, "BA-01.json"); string txt = File.ReadAllText(fp); RootNode rootNode = new RootNode(); rootNode.fromJsonUnit(txt); string s = rootNode.message; foreach (var item in DJson.From(txt)) { if (item.isJsonOfValue || item.isArrayItemOfValue) { foreach (var item1 in item.children) { if (item1.isJsonOfValue || item1.isArrayItemOfValue) { s = "\t+++++++++++++++++"; Trace.WriteLine(s); foreach (var item2 in item1.children) { if (item2.isJsonOfValue || item2.isArrayItemOfValue) { continue; } s = "\t" + item2.key + ": " + item2.value; Trace.WriteLine(s); } continue; } s = "\t" + item1.key + ": " + item1.value; Trace.WriteLine(s); } continue; } s = item.key + ": " + item.value; Trace.WriteLine(s); } }
public async Task CompareForgedToFile() { var forge = new LocalForge(); var options = new JsonSerializerOptions { MaxDepth = 100_000 }; var directories = Directory.GetDirectories("../../../Forging/operations"); foreach (var directory in directories) { var op = (Operation)DJson.Read($"{directory}/unsigned.json", options); var opBytes = File.ReadAllText($"{directory}/forged.hex"); var localBytes = op.Contents.Count == 1 ? await forge.ForgeOperationAsync(op.Branch, op.Contents[0]) : await forge.ForgeOperationGroupAsync(op.Branch, op.Contents.Select(x => x as ManagerOperationContent)); Assert.True(opBytes == Hex.Convert(localBytes), directory); } }
public async void TestJsonSchemaGeneration() { foreach (var address in Directory.GetFiles($@"../../../Contracts/Parameters").Select(x => x.Substring(x.Length - 41, 36))) { var script = DJson.Read($@"../../../Contracts/Scripts/{address}.json"); var contract = new ContractScript(Micheline.FromJson((string)script.code)); foreach (var sample in DJson.Read($@"../../../Contracts/Parameters/{address}.json")) { var jsonSchemaSrc = contract.Parameter.GetJsonSchema((string)sample.human.entrypoint); var jsonSchema = await JsonSchema.FromJsonAsync(jsonSchemaSrc); if (jsonSchema.Type == JsonObjectType.String) { continue; // Newtonsoft does not handle that } var errors = jsonSchema.Validate((string)sample.human.value); Assert.Empty(errors); } } }
public HeaderObject DJsonExtract() { var json = DJson.Parse(Text); return(new HeaderObject { level = json.header.level, proto = json.header.proto, predecessor = json.header.predecessor, timestamp = json.header.timestamp, validation_pass = json.header.validation_pass, operations_hash = json.header.operations_hash, fitness = new List <string> { json.header.fitness[0], json.header.fitness[1] }, context = json.header.context, priority = json.header.priority, proof_of_work_nonce = json.header.proof_of_work_nonce, signature = json.header.signature }); }