Skip to content

Commit 4ea05aa

Browse files
Merge pull request #36 from ipdata/claude/new-session-SYpup
Claude/new session s ypup
2 parents 10eb511 + f76c8ab commit 4ea05aa

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace IPData.Http.Serializer
6+
{
7+
internal class IntJsonConverter : JsonConverter<int>
8+
{
9+
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
if (reader.TokenType == JsonTokenType.String)
12+
{
13+
var stringValue = reader.GetString();
14+
if (int.TryParse(stringValue, out var value))
15+
{
16+
return value;
17+
}
18+
19+
throw new JsonException($"Unable to convert \"{stringValue}\" to System.Int32.");
20+
}
21+
22+
return reader.GetInt32();
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
26+
{
27+
writer.WriteNumberValue(value);
28+
}
29+
}
30+
}

src/IPData/Models/IPLookupResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq.Expressions;
55
using System.Text.Json.Serialization;
66
using IPData.Helpers.Extensions;
7+
using IPData.Http.Serializer;
78

89
namespace IPData.Models
910
{
@@ -88,6 +89,7 @@ public class IPLookupResult
8889
public int? Status { get; set; }
8990

9091
[JsonPropertyName("count")]
92+
[JsonConverter(typeof(IntJsonConverter))]
9193
public int Count { get; set; }
9294

9395
internal static string FieldName(Expression<Func<IPLookupResult, object>> expression)

test/Unit/IPData.Tests/DataSources/TestData/IPLookupResult.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@
6767
"is_bogon": false,
6868
"blocklists": []
6969
},
70-
"status": 200
70+
"status": 200,
71+
"count": "213586"
7172
}

test/Unit/IPData.Tests/Http/Serializer/JsonSerializerTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ public void Deserialize_WhenCalled_ReturnedIPLookupResult(string json)
2121
actual.Should().NotBeNull();
2222
}
2323

24+
[Fact]
25+
public void Deserialize_CountAsString_ParsedCorrectly()
26+
{
27+
// Arrange - API returns count as a string (issue #35)
28+
var json = "{\"count\": \"213586\"}";
29+
30+
// Act
31+
var actual = _sut.Deserialize<IPLookupResult>(json);
32+
33+
// Assert
34+
actual.Count.Should().Be(213586);
35+
}
36+
37+
[Fact]
38+
public void Deserialize_CountAsNumber_ParsedCorrectly()
39+
{
40+
// Arrange
41+
var json = "{\"count\": 213586}";
42+
43+
// Act
44+
var actual = _sut.Deserialize<IPLookupResult>(json);
45+
46+
// Assert
47+
actual.Count.Should().Be(213586);
48+
}
49+
2450
[Theory, AutoMoqData]
2551
public void SerializeIPLookupResult_WhenCalled_ReturnedString(IPLookupResult ipInfo)
2652
{

0 commit comments

Comments
 (0)