Exemple #1
0
        static void Sample7()
        {
            using (var jr = new ChoJSONReader("sample7.json").WithJSONPath("$.fathers")
                            .WithField("id")
                            .WithField("married", fieldType: typeof(bool))
                            .WithField("name")
                            .WithField("sons")
                            .WithField("daughters", fieldType: typeof(Dictionary <string, object>[]))
                   )
            {
                using (var w = new ChoJSONWriter("sample7out.json"))
                {
                    w.Write(jr);
                }

                /*
                 * foreach (var item in jr)
                 * {
                 *  var x = item.id;
                 *  Console.WriteLine(x.GetType());
                 *
                 *  Console.WriteLine(item.id);
                 *  Console.WriteLine(item.married);
                 *  Console.WriteLine(item.name);
                 *  foreach (dynamic son in item.sons)
                 *  {
                 *      var x1 = son.address;
                 *      //Console.WriteLine(ChoUtility.ToStringEx(son.address.street));
                 *  }
                 *  foreach (var daughter in item.daughters)
                 *      Console.WriteLine(ChoUtility.ToStringEx(daughter));
                 * }
                 */
            }
        }
        public async Task <Stream> GetLogs(JObject query, string indexPattern = BASIC_INDEX_PATTERN,
                                           List <string> tokensToIgnore       = null)
        {
            tokensToIgnore = tokensToIgnore ?? JsonPropertyEditor.BASIC_TOKENS_TO_IGNORE;
            try
            {
                var data = await client.SearchAsync <StringResponse>(indexPattern,
                                                                     JsonPropertyEditor.ExcludeGrayLogMeta(query, tokensToIgnore).ToString());

                var ms   = new MemoryStream();
                var json = JObject.Parse(data.Body);

                //elastic specification and graylog
                using (var r = new ChoJSONReader(json["hits"]["hits"].Select(x => x["_source"])))
                {
                    using (var w = new ChoCSVWriter(ms, new ChoCSVRecordConfiguration {
                        HasExcelSeparator = true
                    })
                                   .WithFirstLineHeader())
                    {
                        w.Write(r);
                    }
                }

                ms.Position = 0;
                return(ms);
            }
            catch (Exception e)
            {
                throw new Exception("ElasticSearch error!", e);
            }
        }
Exemple #3
0
        static void Json2Yaml1()
        {
            string json = @"{
   ""swagger"":""2.0"",
   ""info"":{
      ""title"":""UberAPI"",
      ""description"":""MoveyourappforwardwiththeUberAPI"",
      ""version"":""1.0.0""
   },
   ""host"":""api.uber.com"",
   ""schemes"":[
      ""https""
   ],
   ""basePath"":""/v1"",
   ""produces"":[
      ""application/json""
   ]
}";

            using (var r = ChoJSONReader.LoadText(json))
            {
                using (var w = new ChoYamlWriter(Console.Out)
                               .ErrorMode(ChoErrorMode.IgnoreAndContinue)
                       )
                {
                    w.Write(r);
                }
            }
        }
Exemple #4
0
        static void Sample9()
        {
            using (var jr = new ChoJSONReader <Book>("sample9.json").WithJSONPath("$..book")
                   )
            {
                foreach (var x in jr)
                {
                    Console.WriteLine($"Category: {x.Category}");
                    Console.WriteLine($"Title: {x.Title}");
                    Console.WriteLine($"Author: {x.Author}");
                    Console.WriteLine($"Price: {x.Price}");
                }
            }
            return;

            using (var jr = new ChoJSONReader("sample9.json").WithJSONPath("$..book")
                   )
            {
                foreach (var x in jr)
                {
                    Console.WriteLine($"Category: {x.category}");
                    Console.WriteLine($"Title: {x.title}");
                    Console.WriteLine($"Author: {x.author}");
                    Console.WriteLine($"Price: {x.price}");
                }
            }
        }
Exemple #5
0
        public static void JSON2XML()
        {
            string json = @"{
  ""header"": ""myheader"",
  ""transaction"": {
    ""date"": ""2019-09-24"",
    ""items"": [
      {
        ""number"": ""123"",
        ""unit"": ""EA"",
        ""qty"": 6
      },
      {
        ""number"": ""456"",
        ""unit"": ""CS"",
        ""qty"": 4
      }
    ]
  }
}";

            using (var r = ChoJSONReader.LoadText(json))
            {
                var x = r.FirstOrDefault();
                //Console.WriteLine(x.Dump());
                Console.WriteLine(ChoXmlWriter.ToText(x, new ChoXmlRecordConfiguration().Configure(c =>
                {
                    c.RootName = "Root1";
                    //c.DoNotEmitXmlNamespace = true;
                    //c.TurnOffXmlFormatting = true;
                })));
            }
        }
Exemple #6
0
        static void Json2Parquet()
        {
            string json = @"
[{
	""Health"": {
		""Id"": 99,
		""Status"": false
	},
	""Safety"": {
		""Id"": 3,
		""Fire"": 1
	},
	""Climate"": [{
		""Id"": 0,
		""State"": 2
	}]
}]";

            using (var r = ChoJSONReader <MyData> .LoadText(json)
                           .UseJsonSerialization())
            {
                //using (var w = new ChoParquetWriter("MyData.parquet")
                //    //.UseNestedKeyFormat()
                //    )
                //{
                //    w.Write(r.Select(rec1 => rec1.ToDictionary().Flatten().ToDictionary()));
                //}

                var x = ChoParquetWriter.SerializeAll(r); //.Select(rec1 => rec1.ToDictionary().Flatten().ToDictionary()));
                File.WriteAllBytes("MyData1.parquet", x);
            }
        }
        public void ConvertJsonToCsv(FileInfo inputfile)
        {
            _inputFile  = inputfile;
            _outputFile = new FileInfo(Path.Combine(inputfile.Directory.FullName, Constant.CsvOutputFile));

            ConsolePresentation.ShowConvertingMessage();

            try
            {
                using (var reader = new ChoJSONReader(_inputFile.FullName))
                {
                    using (var writer = new ChoCSVWriter(_outputFile.FullName).WithFirstLineHeader())
                    {
                        writer.Write(reader);
                    }
                }
            }
            catch (Exception exception)
            {
                ConsolePresentation.ShowConsoleExceptionAndReadKey(exception);
            }

            ConsolePresentation.ShowConsoleEndMessageAndReadKey(_outputFile.FullName);

            Environment.Exit(0);
        }
Exemple #8
0
        static void StorePOCONodeLoadTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var jparser = new JsonTextReader(reader))
                        {
                            writer.WriteLine(Stores);

                            writer.Flush();
                            stream.Position = 0;

                            var config = new ChoJSONRecordConfiguration()
                            {
                                UseJSONSerialization = true
                            };
                            object rec;
                            using (var parser = new ChoJSONReader <StoreRec>(JObject.Load(jparser).SelectTokens("$.Manufacturers"), config))
                            {
                                while ((rec = parser.Read()) != null)
                                {
                                    Console.WriteLine(rec.ToStringEx());
                                }
                            }
                        }
        }
Exemple #9
0
        public static void JSON2SoapXML()
        {
            string json = @"{
  ""Name"": ""00141169"",
  ""CurrencyCode"": ""EUR"",
  ""Date"": ""2020-04-03"",
}";

            StringBuilder xml = new StringBuilder();

            using (var r = ChoJSONReader.LoadText(json))
            {
                var x = r.FirstOrDefault();
                //Console.WriteLine(x.Dump());

                using (var w = new ChoXmlWriter(xml)
                               .Configure(c => c.NamespaceManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"))
                               .Configure(c => c.NamespaceManager.AddNamespace("tmp", "http://tempuri.org/"))
                               .Configure(c => c.RootName = "soap:Envelope")
                               .Configure(c => c.NodeName = "Body")
                               .Configure(c => c.DefaultNamespacePrefix = "tmp")
                               //.Configure(c => c.Formatting = System.Xml.Formatting.None)
                       )
                {
                    w.Write(new { listdata = x });
                }
            }

            Console.WriteLine(xml.ToString());
        }
Exemple #10
0
        public static void JSON2XmlDateTimeTest()
        {
            string actual;
            string json = @"{
 ""start"": ""2019-10-24T10:37:27.590Z"",
 ""end"": ""2019-10-24T11:00:00.000Z"",
 ""requests/duration"": {
   ""avg"": 3819.55
 }
}";

            //using (var r = new ChoJSONReader(new StringBuilder(json))
            //    )
            //{
            //    Console.WriteLine(r.First().Dump());
            //}

            actual = ChoJSONWriter.ToText(ChoJSONReader.LoadText(json,
                                                                 new ChoJSONRecordConfiguration().Configure(c => c.JsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
            {
                DateParseHandling = Newtonsoft.Json.DateParseHandling.None
            })).FirstOrDefault()
                                          );

            Assert.AreEqual(json, actual);
        }
Exemple #11
0
        public static void Json2Xml2()
        {
            string json = @"
[
    {
        ""firstName"": ""John"",
        ""lastName"": ""Smith"",
        ""age"": 25,
        ""address"": {
            ""streetAddress"": ""21 2nd Street"",
            ""city"": ""New York"",
            ""state"": ""NY"",
            ""postalCode"": ""10021""
        },
        ""phoneNumber"": [
            {
                ""type"": ""home"",
                ""number"": ""212 555-1234""
            },
            {
                ""type"": ""fax"",
                ""number"": ""646 555-4567""
            }
        ]
    },
    {
        ""firstName"": ""Tom"",
        ""lastName"": ""Mark"",
        ""age"": 50,
        ""address"": {
            ""streetAddress"": ""10 Main Street"",
            ""city"": ""Edison"",
            ""state"": ""NJ"",
            ""postalCode"": ""08837""
        },
        ""phoneNumber"": [
            {
                ""type"": ""home"",
                ""number"": ""732 555-1234""
            },
            {
                ""type"": ""fax"",
                ""number"": ""609 555-4567""
            }
        ]
    }
]
";

            using (var r = ChoJSONReader <Employee> .LoadText(json))
            {
                using (var w = new ChoXmlWriter <Employee>(Console.Out)
                               //.WithNodeName("C")
                       )
                {
                    w.Write(r);
                }
            }
        }
Exemple #12
0
 public static void KVPTest()
 {
     using (var jr = new ChoJSONReader <Dictionary <string, string> >("sample5.json").Configure(c => c.UseJSONSerialization = true))
     {
         foreach (var dict1 in jr.Select(dict => dict.Select(kvp => new { kvp.Key, kvp.Value })).SelectMany(x => x))
         {
             Console.WriteLine(dict1.Key);
         }
     }
 }
Exemple #13
0
 static void JSONToXmlSample4()
 {
     using (var parser = new ChoJSONReader <ProductionOrderFile>("sample3.json").Configure(c => c.UseJSONSerialization = true)
            )
     {
         using (var writer = new ChoXmlWriter <ProductionOrderFile>("sample31.xml").Configure(c => c.UseXmlSerialization = true))
             writer.Write(parser);
         return;
     }
 }
Exemple #14
0
 private static void UsingPOCO()
 {
     using (var csv = new ChoCSVWriter <Employee>("emp.csv").WithFirstLineHeader())
     {
         using (var json = new ChoJSONReader <Employee>("emp.json"))
         {
             csv.Write(json);
         }
     }
 }
Exemple #15
0
 private static void QuickConversion()
 {
     using (var csv = new ChoCSVWriter("emp.csv").WithFirstLineHeader())
     {
         using (var json = new ChoJSONReader("emp.json"))
         {
             csv.Write(json);
         }
     }
 }
Exemple #16
0
 static void Sample8()
 {
     using (var jr = new ChoJSONReader <DataMapper>("sample8.json"))
     {
         foreach (var x in jr)
         {
             Console.WriteLine(ChoUtility.DumpAsJson(x));
         }
     }
 }
Exemple #17
0
 static void JsonToParquet52()
 {
     using (var r = new ChoJSONReader("sample52.json")
                    .WithJSONPath("$..data")
            )
     {
         using (var w = new ChoParquetWriter("myData52.parquet"))
             w.Write(r);
     }
 }
Exemple #18
0
 static void Sample3()
 {
     using (var jr = new ChoJSONReader <MyObjectType>("sample3.json").WithJSONPath("$.menu")
            )
     {
         jr.AfterRecordFieldLoad += (o, e) =>
         {
         };
         using (var xw = new ChoXmlWriter <MyObjectType>("sample3.xml").Configure(c => c.UseXmlSerialization = true))
             xw.Write(jr);
     }
 }
Exemple #19
0
        static void JSON2Parquet1()
        {
            string json = @"{
    ""facilities"": [
        {
            ""id"": 39205,
            ""name"": ""Sample1"",
            ""uuid"": ""ac2f3464-c425-4063-86ad-163521b1d610"",
            ""createdAt"": ""2019-03-06T14:25:32Z"",
            ""updatedAt"": ""2019-03-06T14:29:31Z"",
            ""active"": true
        },
        {
            ""id"": 35907,
            ""name"": ""Sample2"",
            ""uuid"": ""d371debb-f030-4c1e-b198-5eb562ceac0f"",
            ""createdAt"": ""2019-02-21T09:33:25Z"",
            ""updatedAt"": ""2019-02-21T09:33:25Z"",
            ""active"": true
        }
    ]
}
";

            using (var r = ChoJSONReader.LoadText(json)
                           .WithJSONPath("$..facilities[*]", true)
                           .WithField("id")
                           .WithField("createdAt", fieldType: typeof(DateTimeOffset), valueConverter: o => DateTimeOffset.Now)
                           .ErrorMode(ChoErrorMode.IgnoreAndContinue)
                   )
            {
                using (var w = new ChoParquetWriter("JSON2Parquet1.parquet")
                               .ErrorMode(ChoErrorMode.IgnoreAndContinue)
                       )
                {
                    w.Write(r);
                }
                return;

                foreach (var rec in r)
                {
                    Console.WriteLine(rec.Dump());
                }
                return;
                //var x = r.Select(r1 => { r1.Location = new Point(100); return r1; }).ToArray();
                //using (var w = new ChoParquetWriter<Facility>("JSON2Parquet1.parquet")
                //    .IgnoreField("Location.IsEmpty")
                //    )
                //{
                //    w.Write(x);
                //}
            }
        }
Exemple #20
0
 static void Sample13()
 {
     using (var p = new ChoJSONReader("sample3.json")
                    //.WithField("details_attributes", jsonPath: "$..details_attributes", fieldType: typeof(ChoDynamicObject))
            )
     {
         foreach (dynamic rec in p)
         {
             Console.WriteLine(rec.menu.popup.menuitem[1].value);
         }
     }
 }
Exemple #21
0
 static void IgnoreItems()
 {
     using (var jr = new ChoJSONReader("sample6.json")
                     .WithField("ProductId", jsonPath: "$.productId")
                     .WithField("User", jsonPath: "$.returnPolicies.user")
            )
     {
         foreach (var item in jr)
         {
             Console.WriteLine(item.ProductId + " " + item.User);
         }
     }
 }
Exemple #22
0
        private string ConvertJsonToCsv(string vmSource)
        {
            using (var r = new ChoJSONReader(new JsonTextReader(new StringReader(vmSource))))
            {
                using var stream = new MemoryStream();

                using (var w = new ChoCSVWriter(stream).WithFirstLineHeader())
                {
                    w.Write(r);
                }

                return(Encoding.UTF8.GetString(stream.ToArray()));
            }
        }
Exemple #23
0
        static void Sample15()
        {
            using (var p = new ChoJSONReader("sample15.json")
                           .WithField("header", jsonPath: "$..header[*]", fieldType: typeof(string[]))
                           .WithField("results", jsonPath: "$..results[*]", fieldType: typeof(List <string[]>))
                   )
            {
                var             rec     = p.FirstOrDefault();
                string[]        header  = rec.header;
                List <string[]> results = rec.results;

                var z = results.Select(a => header.Zip(a, (k, v) => new { Key = k, Value = v }).ToDictionary(kvp => kvp.Key, kvp => kvp.Value)).ToArray();
            }
        }
        static async Task RunAsync()
        {
            // Update web service API endpoint in the following line.
            client.BaseAddress = new Uri("https://api.transport.nsw.gov.au/v1/live/hazards/incident/open");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/octet-stream"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("apikey", "j1zHbAwDsYNo4sR9FpInRVzIX8698p9JxomI");

            TransportAPI_client transportClient = new TransportAPI_client();


            try
            {
                // Get the current traffic incidents
                var incidents = await transportClient.GetCurrentTrafficIncidentsAsync(client.BaseAddress, client);

                var currentTrafficIncidents = JsonConvert.DeserializeObject <CurrentTrafficIncidentsModel>(incidents);


                StringBuilder stringBuilder = new StringBuilder();

                using (var incidentData = ChoJSONReader.LoadText(incidents)
                                          .WithJSONPath("$..features[*]")
                       )
                {
                    using (var writer = new ChoCSVWriter(stringBuilder)
                                        .WithFirstLineHeader()
                                        .Configure(c => c.MaxScanRows = 1)
                                        .Configure(c => c.ThrowAndStopOnMissingField = false)
                           )
                    {
                        writer.Write(incidentData);
                    }
                }


                File.AppendAllText("./output.csv", stringBuilder.ToString());

                Console.WriteLine("\nThe output from NSW Open Data Transport API is now in CSV flat file format. Filename: output.csv");
                //Console.WriteLine(currentTrafficIncidents.features);
                //Console.WriteLine(incidents.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("\nPress any key to exit.");
        }
Exemple #25
0
        static void Main1(string[] args)
        {
            String path = @"C:\Users\kibandi\Desktop\700KExchangeOutput.txt";

            using (StreamReader sr = File.OpenText(path))
            {
                using (var p = new ChoJSONReader(sr))
                {
                    foreach (var rec in p)
                    {
                        Console.WriteLine($"Name: {rec.name}, Id: {rec.id}");
                    }
                }
            }
        }
Exemple #26
0
        static void LoadTest()
        {
            using (var p = new ChoJSONReader <Company>("companies.json")
            {
                TraceSwitch = ChoETLFramework.TraceSwitchOff
            }.NotifyAfter(10000))
            {
                p.Configuration.ColumnCountStrict = true;
                foreach (var e in p)
                {
                    Console.WriteLine("overview: " + e.name);
                }
            }

            //Console.WriteLine("Id: " + e.name);
        }
Exemple #27
0
 static void Sample16()
 {
     using (var p = new ChoJSONReader("sample16.json")
                    .WithField("Ref", jsonPath: "$..ref", fieldType: typeof(string))
                    .WithField("pickcompname", jsonPath: "$..pickcompname", fieldType: typeof(string))
                    .WithField("gw", jsonPath: "$..gw", fieldType: typeof(double))
                    .WithField("qty1", jsonPath: "$..packaing[0].qty", fieldType: typeof(int))
                    .WithField("unit1", jsonPath: "$..packaing[0].unit", fieldType: typeof(string))
                    .WithField("qty2", jsonPath: "$..packaing[1].qty", fieldType: typeof(int))
                    .WithField("unit2", jsonPath: "$..packaing[1].unit", fieldType: typeof(string))
            )
     {
         using (var c = new ChoCSVWriter("sample16.csv").WithFirstLineHeader())
             c.Write(p);
     }
 }
Exemple #28
0
        public static string Jsontocsvlasttry(string jsonContent)
        {
            StringBuilder csv = new StringBuilder();

            using (var p = new ChoJSONReader(new StringReader(jsonContent)))
            {
                using (var w = new ChoCSVWriter(new StringWriter(csv))
                               .WithFirstLineHeader()
                               .WithDelimiter(";")
                       )
                {
                    w.Write(p);
                }
            }
            return(csv.ToString());
        }
Exemple #29
0
        static void EnumLoadTest()
        {
            string json = @"[
 {
  ""Age"": 1,
  ""Gender"": ""M""
 }
]";

            using (var p = ChoJSONReader <Person> .LoadText(json))
            {
                foreach (var rec in p)
                {
                    Console.WriteLine(rec.Dump());
                }
            }
        }
Exemple #30
0
        public static void Sample7Test()
        {
            using (var jr = new ChoJSONReader(FileNameSample7JSON).WithJSONPath("$.fathers")
                            .WithField("id")
                            .WithField("married", fieldType: typeof(bool))
                            .WithField("name")
                            .WithField("sons")
                   )
            {
                using (var w = new ChoXmlWriter(FileNameSample7ActualXML))
                {
                    w.Write(jr);
                }
            }

            FileAssert.AreEqual(FileNameSample7ExpectedXML, FileNameSample7ActualXML);
        }