Skip to content

Commit 80cdb4f

Browse files
authored
Added TryGetValue method to get values without throwing exception (#9)
1 parent 98fc5dd commit 80cdb4f

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

V.Udodov.Json.Tests/EntityTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void WhenGettingEntityAsAStringItShouldSerializeIt()
138138
}
139139

140140
[Fact]
141-
public void WhenTryingToGetWrongFlexibleDataEntityItemItShouldThrow()
141+
public void WhenTryingToGetWrongFlexibleDataEntityItemThroughIndexerItShouldThrow()
142142
{
143143
var entityMock = new EntityMock
144144
{
@@ -152,5 +152,33 @@ public void WhenTryingToGetWrongFlexibleDataEntityItemItShouldThrow()
152152

153153
action.Should().Throw<KeyNotFoundException>();
154154
}
155+
156+
[Fact]
157+
public void WhenTryingToGetWrongFlexibleDataEntityItemItShouldThrow()
158+
{
159+
var entityMock = new EntityMock
160+
{
161+
["shoe_size"] = 12
162+
};
163+
164+
entityMock.TryGetValue("pants_size", out var result)
165+
.Should().BeFalse();
166+
167+
result.Should().BeNull();
168+
}
169+
170+
[Fact]
171+
public void WhenTryingToGetFlexibleDataEntityItemItShouldThrow()
172+
{
173+
var entityMock = new EntityMock
174+
{
175+
["shoe_size"] = 12
176+
};
177+
178+
entityMock.TryGetValue("shoe_size", out var result)
179+
.Should().BeTrue();
180+
181+
result.Should().Be(12);
182+
}
155183
}
156184
}

V.Udodov.Json/Entity.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,7 @@ public object this[string key]
8484

8585
private object Get(string key)
8686
{
87-
object JTokenToObject(JToken source)
88-
{
89-
switch (source.Type)
90-
{
91-
case JTokenType.Object:
92-
return ((JObject) source).Properties()
93-
.ToDictionary(prop => prop.Name, prop => JTokenToObject(prop.Value));
94-
case JTokenType.Array:
95-
return source.Values().Select(JTokenToObject).ToList();
96-
default:
97-
return source.ToObject<object>();
98-
}
99-
}
100-
101-
if (_data.TryGetValue(key, out JToken token))
102-
{
103-
return JTokenToObject(token);
104-
}
87+
if (TryGetValue(key, out object result)) return result;
10588

10689
throw new KeyNotFoundException($"Key {key} was not found.");
10790
}
@@ -123,5 +106,31 @@ private void Set(string key, object value)
123106

124107
_data.Add(key, token);
125108
}
109+
110+
public bool TryGetValue(string key, out object value)
111+
{
112+
object JTokenToObject(JToken source)
113+
{
114+
switch (source.Type)
115+
{
116+
case JTokenType.Object:
117+
return ((JObject) source).Properties()
118+
.ToDictionary(prop => prop.Name, prop => JTokenToObject(prop.Value));
119+
case JTokenType.Array:
120+
return source.Values().Select(JTokenToObject).ToList();
121+
default:
122+
return source.ToObject<object>();
123+
}
124+
}
125+
126+
if (_data.TryGetValue(key, out JToken token))
127+
{
128+
value = JTokenToObject(token);
129+
return true;
130+
}
131+
132+
value = null;
133+
return false;
134+
}
126135
}
127136
}

0 commit comments

Comments
 (0)