How to use JsonPathEngine class of NBi.Core.DataSerialization.Flattening.Json package

Best NBi code snippet using NBi.Core.DataSerialization.Flattening.Json.JsonPathEngine

JsonPathEngineTest.cs

Source:JsonPathEngineTest.cs Github

copy

Full Screen

...11using System.Threading.Tasks;12using System.Xml.Linq;13namespace NBi.Testing.Core.DataSerialization.Flattenizer14{15 public class JsonPathEngineTest16 {17 protected StreamReader GetResourceReader(string filename)18 {19 // A Stream is needed to read the XML document.20 var stream = Assembly.GetExecutingAssembly()21 .GetManifestResourceStream($"{GetType().Namespace}.Resources.{filename}.json");22 var reader = new StreamReader(stream);23 return reader;24 }25 [Test]26 [TestCase("$.PurchaseOrders[*].Items[*]", 5)]27 [TestCase("$.PurchaseOrders[*]", 4)]28 public void Execute_Example_RowCount(string from, int rowCount)29 {30 var selects = new List<ElementSelect>()31 {32 new ElementSelect(new LiteralScalarResolver<string>("$"))33 };34 using (var reader = GetResourceReader("PurchaseOrders"))35 {36 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);37 var result = engine.Execute(reader);38 Assert.That(result.Count, Is.EqualTo(rowCount));39 }40 }41 [Test]42 public void Execute_Example_FirstColumnIsCorrect()43 {44 var from = "$.PurchaseOrders[*].Items[*]";45 var selects = new List<ElementSelect>()46 {47 new ElementSelect(new LiteralScalarResolver<string>("PartNumber"))48 };49 using (var reader = GetResourceReader("PurchaseOrders"))50 {51 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);52 var result = engine.Execute(reader);53 Assert.That(result.Count, Is.EqualTo(5));54 Assert.That(result.Select(x => ((x as IEnumerable<object>).ElementAt(0) as string).Length), Is.All.EqualTo(6)); //Format is 123-XY55 }56 }57 [Test]58 public void Execute_Example_AllColumnsAreCorrect()59 {60 var from = "$.PurchaseOrders[*].Items[*]";61 var selects = new List<ElementSelect>()62 {63 new ElementSelect(new LiteralScalarResolver<string>("PartNumber")),64 new ElementSelect(new LiteralScalarResolver<string>("Quantity"))65 };66 using (var reader = GetResourceReader("PurchaseOrders"))67 {68 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);69 var result = engine.Execute(reader);70 Assert.That(result.Count, Is.EqualTo(5));71 Assert.That(result.Count, Is.EqualTo(5));72 Assert.That(result.Select(x => ((x as IEnumerable<object>).ElementAt(0) as string).Length), Is.All.EqualTo(6)); //Format is 123-XY73 Assert.That(result.Select(x => (x as IEnumerable<object>).ElementAt(1)), Is.All.EqualTo(1).Or.EqualTo(2)); //All quantity are between 1 and 274 }75 }76 [Test]77 public void Execute_FromElement_ValueCorrect()78 {79 var from = "$.PurchaseOrders[*].Items[*].ProductName";80 var selects = new List<ElementSelect>()81 {82 new ElementSelect(new LiteralScalarResolver<string>("$"))83 };84 using (var reader = GetResourceReader("PurchaseOrders"))85 {86 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);87 var result = engine.Execute(reader);88 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("Lawnmower"));89 }90 }91 [Test]92 public void Execute_FromAttribute_ValueCorrect()93 {94 var from = "$.PurchaseOrders[*].Items[*]";95 var selects = new List<ElementSelect>()96 {97 new ElementSelect(new LiteralScalarResolver<string>("$.PartNumber"))98 };99 using (var reader = GetResourceReader("PurchaseOrders"))100 {101 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);102 var result = engine.Execute(reader);103 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Is.EqualTo("872-AA"));104 }105 }106 [Test]107 public void Execute_MissingElement_Null()108 {109 var from = "$.PurchaseOrders[*]";110 var selects = new List<ElementSelect>()111 {112 new ElementSelect(new LiteralScalarResolver<string>("$.PurchaseOrderNumber"))113 };114 using (var reader = GetResourceReader("PurchaseOrders"))115 {116 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);117 var result = engine.Execute(reader);118 Assert.That((result.ElementAt(3) as IEnumerable<object>).ElementAt(0), Is.EqualTo("(null)"));119 }120 }121 [Test]122 public void Execute_ParentElement_ValueCorrect()123 {124 var from = "$.PurchaseOrders[*].Items[*]";125 var selects = new List<ElementSelect>()126 {127 new ElementSelect(new LiteralScalarResolver<string>("!!.PurchaseOrderNumber")),128 new ElementSelect(new LiteralScalarResolver<string>("$.PartNumber"))129 };130 using (var reader = GetResourceReader("PurchaseOrders"))131 {132 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);133 var result = engine.Execute(reader);134 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Does.Contain("99503"));135 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(1), Does.Contain("872-AA"));136 Assert.That((result.ElementAt(1) as IEnumerable<object>).ElementAt(0), Does.Contain("99503"));137 Assert.That((result.ElementAt(1) as IEnumerable<object>).ElementAt(1), Does.Contain("926-AA"));138 Assert.That((result.ElementAt(2) as IEnumerable<object>).ElementAt(0), Does.Contain("99505"));139 Assert.That((result.ElementAt(2) as IEnumerable<object>).ElementAt(1), Does.Contain("456-NM"));140 }141 }142 [Test]143 public void Execute_ParentElementGoingAboveRoot_ValueCorrect()144 {145 var from = "$.PurchaseOrders[*].Items[*]";146 var selects = new List<ElementSelect>()147 {148 new ElementSelect(new LiteralScalarResolver<string>("!!!!!!.PurchaseOrderNumber")),149 new ElementSelect(new LiteralScalarResolver<string>("$.PartNumber"))150 };151 using (var reader = GetResourceReader("PurchaseOrders"))152 {153 var engine = new JsonPathEngine(new LiteralScalarResolver<string>(from), selects);154 var result = engine.Execute(reader);155 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(0), Does.Contain("(null)"));156 Assert.That((result.ElementAt(0) as IEnumerable<object>).ElementAt(1), Does.Contain("872-AA"));157 Assert.That((result.ElementAt(1) as IEnumerable<object>).ElementAt(0), Does.Contain("(null)"));158 Assert.That((result.ElementAt(1) as IEnumerable<object>).ElementAt(1), Does.Contain("926-AA"));159 Assert.That((result.ElementAt(2) as IEnumerable<object>).ElementAt(0), Does.Contain("(null)"));160 Assert.That((result.ElementAt(2) as IEnumerable<object>).ElementAt(1), Does.Contain("456-NM"));161 }162 }163 }164}...

Full Screen

Full Screen

JsonPathEngine.cs

Source:JsonPathEngine.cs Github

copy

Full Screen

...10using System.Text.RegularExpressions;11using System.Threading.Tasks;12namespace NBi.Core.DataSerialization.Flattening.Json13{14 class JsonPathEngine : PathFlattenizer, IDataSerializationFlattenizer15 {16 public JsonPathEngine(IScalarResolver<string> from, IEnumerable<IPathSelect> selects)17 : base(from, selects) { }18 public override IEnumerable<object> Execute(TextReader textReader)19 {20 var json = JToken.ReadFrom(new JsonTextReader(textReader));21 var result = from item in json.SelectTokens(From.Execute())22 select GetObj(item);23 return result;24 }25 private object GetObj(JToken item)26 {27 var obj = new List<object>();28 obj.AddRange(BuildPaths(item, Selects).ToArray());29 return obj;30 }...

Full Screen

Full Screen

DataSerializationFlattenizerFactory.cs

Source:DataSerializationFlattenizerFactory.cs Github

copy

Full Screen

...13 {14 switch (args)15 {16 case XPathArgs xpathArgs: return new XPathEngine(xpathArgs.From, xpathArgs.Selects, xpathArgs.DefaultNamespacePrefix, xpathArgs.IsIgnoreNamespace);17 case JsonPathArgs jsonPathArgs: return new JsonPathEngine(jsonPathArgs.From, jsonPathArgs.Selects);18 default: throw new ArgumentOutOfRangeException();19 }20 }21 }22}...

Full Screen

Full Screen

JsonPathEngine

Using AI Code Generation

copy

Full Screen

1var engine = new JsonPathEngine();2var result = engine.Execute("1.json", "$.store.book[*].author");3foreach (var item in result)4{5 Console.WriteLine(item);6}7var engine = new JsonPathEngine();8var result = engine.Execute("2.json", "$.store.book[*].author");9foreach (var item in result)10{11 Console.WriteLine(item);12}13var engine = new JsonPathEngine();14var result = engine.Execute("3.json", "$.store.book[*].author");15foreach (var item in result)16{17 Console.WriteLine(item);18}19var engine = new JsonPathEngine();20var result = engine.Execute("4.json", "$.store.book[*].author");21foreach (var item in result)22{23 Console.WriteLine(item);24}25var engine = new JsonPathEngine();26var result = engine.Execute("5.json", "$.store.book[*].author");27foreach (var item in result)28{29 Console.WriteLine(item);30}31var engine = new JsonPathEngine();32var result = engine.Execute("6.json", "$.store.book[*].author");33foreach (var item in result)34{35 Console.WriteLine(item);36}37var engine = new JsonPathEngine();38var result = engine.Execute("7.json", "$.store.book[*].author");39foreach (var item in result)40{41 Console.WriteLine(item);42}43var engine = new JsonPathEngine();44var result = engine.Execute("8.json", "$.store.book[*].author");45foreach (var item in result)46{

Full Screen

Full Screen

JsonPathEngine

Using AI Code Generation

copy

Full Screen

1var engine = new JsonPathEngine();2var result = engine.GetPath(new System.IO.FileInfo("1.json"), "$.store.book[0].author");3Console.WriteLine(result);4var engine = new JsonPathEngine();5var result = engine.GetPath(new System.IO.FileInfo("2.json"), "$.store.book[0].author");6Console.WriteLine(result);7var engine = new JsonPathEngine();8var result = engine.GetPath(new System.IO.FileInfo("3.json"), "$.store.book[0].author");9Console.WriteLine(result);10var engine = new JsonPathEngine();11var result = engine.GetPath(new System.IO.FileInfo("4.json"), "$.store.book[0].author");12Console.WriteLine(result);13var engine = new JsonPathEngine();14var result = engine.GetPath(new System.IO.FileInfo("5.json"), "$.store.book[0].author");15Console.WriteLine(result);16var engine = new JsonPathEngine();17var result = engine.GetPath(new System.IO.FileInfo("6.json"), "$.store.book[0].author");18Console.WriteLine(result);19var engine = new JsonPathEngine();20var result = engine.GetPath(new System.IO.FileInfo("7.json"), "$.store.book[0].author");21Console.WriteLine(result);22var engine = new JsonPathEngine();23var result = engine.GetPath(new System.IO.FileInfo("8.json"), "$.store.book[0].author");24Console.WriteLine(result);

Full Screen

Full Screen

JsonPathEngine

Using AI Code Generation

copy

Full Screen

1string json = @"{""Name"":""John"", ""Age"":25, ""Address"":{""Street"":""Main Street"", ""Number"": 123}}";2var engine = new JsonPathEngine();3var result = engine.Evaluate(json, "$.Name");4string json = @"{""Name"":""John"", ""Age"":25, ""Address"":{""Street"":""Main Street"", ""Number"": 123}}";5var engine = new JsonPathEngine();6var result = engine.Evaluate(json, "$.Address.Street");7string json = @"{""Name"":""John"", ""Age"":25, ""Address"":{""Street"":""Main Street"", ""Number"": 123}}";8var engine = new JsonPathEngine();9var result = engine.Evaluate(json, "$.Address.Number");10string json = @"{""Name"":""John"", ""Age"":25, ""Address"":{""Street"":""Main Street"", ""Number"": 123}}";11var engine = new JsonPathEngine();12var result = engine.Evaluate(json, "$.Age");13string json = @"{""Name"":""John"", ""Age"":25, ""Address"":{""Street"":""Main Street"", ""Number"": 123}}";14var engine = new JsonPathEngine();15var result = engine.Evaluate(json, "$.Address");16string json = @"{""Name"":""John"", ""Age"":25, ""Address"":{""Street"":""Main Street"", ""Number"": 123}}";17var engine = new JsonPathEngine();18var result = engine.Evaluate(json, "$.Address.Number");19string json = @"{""Name"":""John"", ""Age"":

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run NBi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in JsonPathEngine

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful