public SciResponseV2_1 ParseResultsJson(string jsonResult)
        {
            SciResponseV2_1 response = new SciResponseV2_1();

            var options = new JsonDocumentOptions
            {
                AllowTrailingCommas = true
            };

            using (JsonDocument document = JsonDocument.Parse(jsonResult, options))
            {
                string name = document.RootElement.GetProperty("metainfo").GetProperty("name").GetString();

                string[] nameComponents = name.Split("__");
                response.Latitude     = Convert.ToDouble(nameComponents[0]);
                response.Longitude    = Convert.ToDouble(nameComponents[1]);
                response.RotationName = nameComponents[2];

                response.ErosionWater = document.RootElement.GetProperty("parameter")
                                        .EnumerateArray()
                                        .First(x => x.GetProperty("name").GetString() == "erosion_water")
                                        .GetProperty("value").GetDouble();

                response.WaterOM = document.RootElement.GetProperty("parameter")
                                   .EnumerateArray()
                                   .First(x => x.GetProperty("name").GetString() == "water_om")
                                   .GetProperty("value").GetDouble();

                response.WaterFO = document.RootElement.GetProperty("parameter")
                                   .EnumerateArray()
                                   .First(x => x.GetProperty("name").GetString() == "water_fo")
                                   .GetProperty("value").GetDouble();

                response.WindOM = document.RootElement.GetProperty("parameter")
                                  .EnumerateArray()
                                  .First(x => x.GetProperty("name").GetString() == "wind_om")
                                  .GetProperty("value").GetDouble();

                response.WindFO = document.RootElement.GetProperty("parameter")
                                  .EnumerateArray()
                                  .First(x => x.GetProperty("name").GetString() == "wind_fo")
                                  .GetProperty("value").GetDouble();

                response.ErosionWind = document.RootElement.GetProperty("parameter")
                                       .EnumerateArray()
                                       .First(x => x.GetProperty("name").GetString() == "erosion_wind")
                                       .GetProperty("value").GetDouble();

                response.SciTotal = document.RootElement.GetProperty("result")
                                    .EnumerateArray()
                                    .First(x => x.GetProperty("name").GetString() == "sci_total")
                                    .GetProperty("value").GetDouble();
            }

            return(response);
        }
        public List <SciResponseV2_1> ReadSciResponseV2_1Files(
            string filePath,
            SciV2_1 service)
        {
            string[] files = Directory.GetFiles(filePath, "*.json");

            List <SciResponseV2_1> responses = new List <SciResponseV2_1>();

            foreach (var file in files)
            {
                string          json     = File.ReadAllText(file);
                SciResponseV2_1 response = service.ParseResultsJson(json);

                responses.Add(response);
            }

            return(responses);
        }
Example #3
0
        public void ParseResults_ValidJson_ExpectedResults()
        {
            // Arrange
            var    sut  = new SciV2_1();
            string json = File.ReadAllText(
                @"Assets\exampleSciResponseV2_1.json");

            // Act
            SciResponseV2_1 actual = sut.ParseResultsJson(json);

            // Assert
            Assert.Equal(46.311713123, actual.Latitude);
            Assert.Equal(-116.896475566, actual.Longitude);
            Assert.Equal("Transition_NoTill", actual.RotationName);
            Assert.Equal(0.6309946026996589, actual.SciTotal);
            Assert.Equal(0.970992957076145, actual.ErosionWater);
            Assert.Equal(0.717950513657798, actual.WaterOM);
            Assert.Equal(0.734653465346535, actual.WaterFO);
            Assert.Equal(0.717950513657798, actual.WindOM);
            Assert.Equal(0.7604, actual.WindFO);
            Assert.Equal(1, actual.ErosionWind);
        }