Exemple #1
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 #2
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);
            }
        }
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
        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 #5
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 #6
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 #7
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);
                //}
            }
        }
        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 #9
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 #10
0
        static void Nested2NestedObjectTest()
        {
            string json = @"{ 
""id"": 123,   ""userName"": ""fflintstone"",   ""Address"": {
""address"": ""345 Cave Stone Road"",
""address2"": """",
""city"": ""Bedrock"",
""state"": ""AZ"",
""zip"": """"   } }";

            using (var p = ChoJSONReader <Customer> .LoadText(json).WithFlatToNestedObjectSupport(false)
                           //.WithField(r => r.Address.Address, fieldName: "Address")
                   )
            {
                var x = p.First();
                Console.WriteLine(x.Dump());
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            XmlToCSVSample();
            return;

            //dynamic p = new ChoPropertyBag();
            //p.Name = "Raj";
            //p.Zip = "10020";

            //foreach (var kvp in ChoExpandoObjectEx.ToExpandoObject(p))
            //    Console.WriteLine(kvp);
            //return;
            //XmlToJSONSample4();
            //    JSONToXmlSample4();

            string json = @"
    {
        ""Row1"":{
            ""x"":123,
            ""y"":21,
            ""z"":22
        },
    }";

            using (var r = ChoJSONReader.LoadText(json)
                           .WithField("x", jsonPath: "$.Row1", fieldType: typeof(object))
                           .WithField("y", jsonPath: "$.Row1.y", fieldType: typeof(object))
                           .WithField("z", jsonPath: "$.Row1", fieldType: typeof(object))
                   )
            {
                r.BeforeRecordFieldLoad += (o, e) =>
                {
                    if (e.PropertyName == "x")
                    {
                        //dynamic x = e.PropertyName;
                        //e.Record.x = x.x;
                        e.Skip = true;
                    }
                };
                using (var writer = new ChoXmlWriter("sample.xml"))
                    writer.Write(r);
                return;
            }
        }
Exemple #12
0
        public static void CSVWithJSON()
        {
            using (var parser = new ChoCSVReader <EmpWithJSON>("emp1.csv"))
            {
                parser.BeforeRecordFieldLoad += (o, e) =>
                {
                    if (e.PropertyName == "JsonValue")
                    {
                        EmpWithJSON rec     = e.Record as EmpWithJSON;
                        dynamic     jobject = ChoJSONReader.LoadText((string)e.Source).FirstOrDefault();
                        rec.product_version_id   = jobject.product_version_id;
                        rec.product_version_name = jobject.product_version_name;
                        e.Skip = true;
                    }
                };
                using (var jp = new ChoJSONWriter("emp1.json"))
                    jp.Write(parser.Select(i => new { i.Id, i.Name, i.product_version_id, i.product_version_name }));

                //foreach (var rec in parser)
                //    Console.WriteLine(rec.product_version_id);
            }
        }
Exemple #13
0
        static void StageJSONFile()
        {
            string json = @"
    [
        {
            ""Id"": 1,
            ""Name"": ""Polo"",
            ""City"": ""New York""
        },
        {
            ""Id"": 2,
            ""Name"": ""328"",
            ""City"": ""Edison""
        }
    ]";

            ChoETLFrxBootstrap.TraceLevel = TraceLevel.Error;
            using (var r = ChoJSONReader <Emp> .LoadText(json)
                   )
            {
                r.StageOnSQLite().Where(e => e.Id == 2).Print();
            }
        }
Exemple #14
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 <Facility> .LoadText(json)
                           .WithJSONPath("$..facilities", false)
                   )
            {
                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 #15
0
        static void Sample11()
        {
            string j1 = @"{
    ""images"":{
         ""totalCount"":4,
         ""0"":{
                ""url"":""file1.jpg""
         },
         ""1"":{
                ""url"":""file2.jpg""
         },
         ""2"":{
                ""url"":""file3.jpg""
        },
        ""3"":{
                ""url"":""file4.jpg""
        }
        }
    }";

            using (var jr = ChoJSONReader.LoadText(j1)
                            .WithField("TotalCount", jsonPath: "$..totalCount", fieldType: typeof(int))
                            .WithField("Url", jsonPath: "$..url", fieldType: typeof(string[]))
                   )
            {
                foreach (var x in jr)
                {
                    Console.WriteLine($"TotalCount: {x.TotalCount}");

                    foreach (var url in x.Url)
                    {
                        Console.WriteLine($"Url: {url}");
                    }
                }
            }
        }
Exemple #16
0
        static void CombineJSONTest()
        {
            string json = @"[
  {
    ""deliveryDay"": ""2018-06-19T15:00:00.000+0300"",
    ""currencyCode"": ""TRY"",
    ""offerType"": ""HOURLY"",
    ""regionCode"": ""TR1"",
    ""offerDetails"": [
         {
           ""startPeriod"": ""1"",
            ""duration"": ""1"",
            ""offerPrices"": [
                {
                  ""price"": ""0"",
                   ""amount"": ""5""
                 }
               ]
             }
           ]
          },
  {
   ""deliveryDay"": ""2018-06-19T15:00:00.000+0300"",
   ""currencyCode"": ""TRY"",
   ""offerType"": ""HOURLY"",
   ""regionCode"": ""TR1"",
   ""offerDetails"": [
         {
           ""startPeriod"": ""1"",
           ""duration"": ""1"",
           ""offerPrices"": [
                {
                  ""price"": ""2000"",
                   ""amount"": ""5""
               }
              ]
            }
          ]
        }
       ]";


            StringBuilder sb = new StringBuilder();

            using (var p = ChoJSONReader.LoadText(json))
            {
                var list = p.GroupBy(r => r.deliveryDay).Select(r => new
                {
                    deliveryDay = r.Key,
                    r.First().currencyCode,
                    r.First().offerType,
                    r.First().regionCode,
                    offerDetails = new
                    {
                        ((Array)r.First().offerDetails).OfType <dynamic>().First().startPeriod,
                        ((Array)r.First().offerDetails).OfType <dynamic>().First().duration,
                        offerPrices = r.Select(r1 => ((Array)r1.offerDetails[0].offerPrices).OfType <object>().First()).ToArray()
                    }
                }).ToArray();

                Console.WriteLine(ChoJSONWriter.ToTextAll(list));
                //foreach (var rec in )
                //	Console.WriteLine(rec.Dump());
            }
        }
Exemple #17
0
        static void Sample50()
        {
            string json = @"
{
    ""home"": {
        ""0_15"": {
            ""goals"": 7,
            ""percentage"": 14
        },
        ""15_30"": {
            ""goals"": 6,
            ""percentage"": 12
        },
        ""30_45"": {
            ""goals"": 11,
            ""percentage"": 22
        },
        ""45_60"": {
            ""goals"": 4,
            ""percentage"": 8
        },
        ""60_75"": {
            ""goals"": 8,
            ""percentage"": 16
        },
        ""75_90"": {
            ""goals"": 14,
            ""percentage"": 28
        }
    },
    ""away"": {
        ""0_15"": {
            ""goals"": 7,
            ""percentage"": 15.56
        },
        ""15_30"": {
            ""goals"": 7,
            ""percentage"": 15.56
        },
        ""30_45"": {
            ""goals"": 5,
            ""percentage"": 11.11
        },
        ""45_60"": {
            ""goals"": 6,
            ""percentage"": 13.33
        },
        ""60_75"": {
            ""goals"": 13,
            ""percentage"": 28.89
        },
        ""75_90"": {
            ""goals"": 7,
            ""percentage"": 15.56
        }
    }
}";

            using (var p = ChoJSONReader <Rootobject> .LoadText(json).Configure(c => c.SupportMultipleContent = true))
            {
                //foreach (var rec in p)
                //	Console.WriteLine(rec.Dump());
                Console.WriteLine(ChoJSONWriter <Rootobject> .ToText(p.First(), new ChoJSONRecordConfiguration().Configure(c => c.SupportMultipleContent = true)));
            }
        }
Exemple #18
0
        public static void Json2Xml1()
        {
            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.LoadText(json))
            {
                using (var w = new ChoXmlWriter(Console.Out)
                               .UseXmlSerialization()
                               .Configure(c => c.OmitXsiNamespace = true)
                               .ErrorMode(ChoErrorMode.IgnoreAndContinue)
                       )
                {
                    w.Write(r);
                }
            }
        }
Exemple #19
0
        static void Issue167()
        {
            var completeFile = new CompleteFile
            {
                DataSource = "DataSource",
                DataType   = 1,
                Samples    = new Samples
                {
                    Samples1 = new List <Data>
                    {
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                    },
                    Samples2 = new List <Data>
                    {
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                    },
                    Samples3 = new List <Data>
                    {
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                        new Data
                        {
                            Prop00     = 0,
                            Prop01     = "Prop01",
                            Properties = new Properties
                            {
                                Prop10 = 1,
                                Prop11 = 2,
                            }
                        },
                    },
                    Sample4 = new Data
                    {
                        Prop00     = 0,
                        Prop01     = "Prop01",
                        Properties = new Properties
                        {
                            Prop10 = 1,
                            Prop11 = 2,
                        }
                    },
                }
            };

            StringBuilder json = new StringBuilder();

            using (var w = new ChoJSONWriter <CompleteFile>(json)
                   )
            {
                w.Write(completeFile);
            }

            json.Print();

            using (var r = ChoJSONReader <CompleteFile> .LoadText(json.ToString())
                           .UseJsonSerialization()
                   )
            {
                using (var w = new ChoParquetWriter("CompleteFile.parquet")
                       )
                {
                    w.Write(r.Select(rec1 => rec1.FlattenToDictionary()));
                }

                //using (var w = new ChoParquetWriter<CompleteFile>("CompleteFile.parquet")
                //    .WithField(f => f.Samples, valueConverter: o => "x", fieldType: typeof(string))
                //    )
                //{
                //    w.Write(r); //.Select(rec1 => rec1.Flatten().ToDictionary()));
                //}
            }
        }