From d755754198bedf4684f7e99b4bd04d22142f0fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Thu, 11 May 2023 18:12:45 +0200 Subject: [PATCH] Fixing Json.GetValue() --- AMWD.Common/Extensions/JsonExtensions.cs | 17 +++++++++++++++-- CHANGELOG.md | 10 +++++++++- .../Common/Extensions/JsonExtensionsTests.cs | 4 ++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/AMWD.Common/Extensions/JsonExtensions.cs b/AMWD.Common/Extensions/JsonExtensions.cs index 95dfaa1..7d91a91 100644 --- a/AMWD.Common/Extensions/JsonExtensions.cs +++ b/AMWD.Common/Extensions/JsonExtensions.cs @@ -146,13 +146,26 @@ namespace Newtonsoft.Json if (lvlObj == null) return defaultValue; - lvlObj = lvlObj[level]; + string lvl = level; + if (lvlObj.Type == JTokenType.Object) + { + foreach (var prop in ((JObject)lvlObj).Properties()) + { + if (prop.Name.Equals(lvl, System.StringComparison.OrdinalIgnoreCase)) + { + lvl = prop.Name; + break; + } + } + } + + lvlObj = lvlObj[lvl]; } if (lvlObj == null) return defaultValue; - return DeepConvert.ChangeType(lvlObj is JValue ? ((JValue)lvlObj).Value : lvlObj.Value()); + return DeepConvert.ChangeType(lvlObj is JValue lvlValue ? lvlValue.Value : lvlObj.Value()); } /// diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a6d56e..1bae954 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Upcoming](https://git.am-wd.de/AM.WD/common/compare/v1.11.0...main) - 0000-00-00 +## [Upcoming](https://git.am-wd.de/AM.WD/common/compare/v1.11.1...main) - 0000-00-00 _no changes yet_ + +## [v1.11.1](https://git.am-wd.de/AM.WD/common/compare/v1.11.0...v1.11.1) - 2023-05-11 + +### Fixed + +- `JsonExtensions.GetValue()` now is case insensitive and detects the correct property name of a `JObject`. + + ## [v1.11.0](https://git.am-wd.de/AM.WD/common/compare/v1.10.0...v1.11.0) - 2023-03-29 ### Added diff --git a/UnitTests/Common/Extensions/JsonExtensionsTests.cs b/UnitTests/Common/Extensions/JsonExtensionsTests.cs index 76faa5d..39bb0c3 100644 --- a/UnitTests/Common/Extensions/JsonExtensionsTests.cs +++ b/UnitTests/Common/Extensions/JsonExtensionsTests.cs @@ -260,7 +260,7 @@ namespace UnitTests.Common.Extensions // act string topLevelString = jObj.GetValue("stringValue"); decimal topLevelDecimal = jObj.GetValue("decimalValue"); - int subLevelInteger = jObj.GetValue("object:integerValue"); + int subLevelInteger = jObj.GetValue("object:IntegerValue"); string subLevelString = jObj.GetValue("object:stringValue"); string notExistingOnTopLevel = jObj.GetValue("fancyValue"); @@ -288,7 +288,7 @@ namespace UnitTests.Common.Extensions // act string topLevelString = jObj.GetValue("stringValue", "Test String"); decimal topLevelDecimal = jObj.GetValue("decimalValue", 13.24m); - int subLevelInteger = jObj.GetValue("object:integerValue", 55); + int subLevelInteger = jObj.GetValue("object:IntegerValue", 55); string subLevelString = jObj.GetValue("object:stringValue", "Yeah!"); string notExistingOnTopLevel = jObj.GetValue("fancyValue", "Party!");