Skip to content

Commit 56ee19e

Browse files
Merge pull request #78 from GavinPower747/development
Release 1.8.1
2 parents 9b33d5a + 56cba07 commit 56ee19e

4 files changed

Lines changed: 132 additions & 5 deletions

File tree

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.8.0.{build}
1+
version: 1.8.1.{build}
22
image: Visual Studio 2017
33
skip_tags: true
44

src/pubg-dotnet/Infrastructure/JsonConverters/RelationshipIdConverter.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ public class RelationshipIdConverter : JsonConverter
1212

1313
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
1414
{
15-
JObject jo = JObject.Load(reader);
16-
var dataToken = jo.SelectToken("data");
15+
//if the reader is not reading a relationship object just deserialize as normal.
16+
//This allows us to serialize and deserialize multiple times after converting from the Json-API format
17+
if (reader.TokenType != JsonToken.StartObject)
18+
return serializer.Deserialize(reader, objectType);
1719

20+
JToken jt = JToken.Load(reader);
21+
22+
var dataToken = jt.SelectToken("data");
23+
1824
if (objectType == typeof(string))
1925
return dataToken["id"].ToString();
20-
26+
2127
return dataToken.Select(x => (string)x["id"]).ToList();
2228
}
2329

src/pubg-dotnet/pubg-dotnet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Description>Sync and Async client library for communicating with the Pubg Developer API supporting .net standard 2.0</Description>
55
<AssemblyTitle>Pubg.Net</AssemblyTitle>
6-
<Version>1.8.0</Version>
6+
<Version>1.8.1</Version>
77
<Authors>Gavin Power</Authors>
88
<TargetFramework>netstandard2.0;net45</TargetFramework>
99
<AssemblyName>Pubg.Net</AssemblyName>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using FluentAssertions;
2+
using JsonApiSerializer;
3+
using Newtonsoft.Json;
4+
using Pubg.Net.Infrastructure.JsonConverters;
5+
using System;
6+
using System.Collections.Generic;
7+
using Xunit;
8+
9+
namespace Pubg.Net.UnitTests.JsonConverters
10+
{
11+
public class RelationshipIdConverterTests
12+
{
13+
public class TestArticle
14+
{
15+
[JsonProperty]
16+
public int Id { get; set; }
17+
18+
[JsonProperty]
19+
public string Title { get; set; }
20+
21+
[JsonProperty]
22+
public string Body { get; set; }
23+
24+
[JsonProperty]
25+
public DateTime Created { get; set; }
26+
27+
[JsonProperty]
28+
public DateTime Updated { get; set; }
29+
30+
[JsonProperty("author")]
31+
[JsonConverter(typeof(RelationshipIdConverter))]
32+
public IEnumerable<string> AuthorIds { get; set; }
33+
}
34+
35+
string jsonApiLiteral =
36+
@"{
37+
""data"":
38+
{
39+
""type"": ""TestArticle"",
40+
""id"": ""1"",
41+
""attributes"":
42+
{
43+
""title"": ""JSON API paints my bikeshed!"",
44+
""body"": ""The shortest article. Ever."",
45+
""created"": ""2015-05-22T14:56:29.000Z"",
46+
""updated"": ""2015-05-22T14:56:28.000Z""
47+
},
48+
""relationships"":
49+
{
50+
""author"":
51+
{
52+
""data"":
53+
[
54+
{""id"": 42, ""type"": ""people""},
55+
{""id"": 43, ""type"": ""people""},
56+
{""id"": 44, ""type"": ""people""},
57+
{""id"": 45, ""type"": ""people""}
58+
]
59+
}
60+
}
61+
}
62+
}";
63+
64+
[Fact]
65+
public void RelationshipIdConverter_Converts_FromJsonApiFormat()
66+
{
67+
TestArticle testArticle = JsonConvert.DeserializeObject<TestArticle>(jsonApiLiteral, new JsonApiSerializerSettings());
68+
69+
testArticle.Id.Should().Equals(1);
70+
testArticle.Title.Should().Equals("JSON API paints my bikeshed!");
71+
testArticle.Body.Should().Equals("The shortest article. Ever.");
72+
testArticle.Created.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:29.000Z"));
73+
testArticle.Updated.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:28.000Z"));
74+
testArticle.AuthorIds.Should().NotBeNullOrEmpty();
75+
testArticle.AuthorIds.Should().Contain(new [] { "42", "43", "44", "45" });
76+
}
77+
78+
[Fact]
79+
public void RelationshipIdConverter_Converts_FromJsonApiFormat_MultipleTimes()
80+
{
81+
TestArticle testArticle = JsonConvert.DeserializeObject<TestArticle>(jsonApiLiteral, new JsonApiSerializerSettings());
82+
83+
var reserializedArticle = JsonConvert.SerializeObject(testArticle);
84+
var deserializedArticle = JsonConvert.DeserializeObject<TestArticle>(reserializedArticle);
85+
86+
deserializedArticle.Id.Should().Equals(1);
87+
deserializedArticle.Title.Should().Equals("JSON API paints my bikeshed!");
88+
deserializedArticle.Body.Should().Equals("The shortest article. Ever.");
89+
deserializedArticle.Created.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:29.000Z"));
90+
deserializedArticle.Updated.Should().Equals(Convert.ToDateTime("2015-05-22T14:56:28.000Z"));
91+
deserializedArticle.AuthorIds.Should().NotBeNullOrEmpty();
92+
deserializedArticle.AuthorIds.Should().Contain(new[] { "42", "43", "44", "45" });
93+
}
94+
95+
[Fact]
96+
public void RelationshipIdConverter_Converts_FromApplicationJson()
97+
{
98+
var baseArticle = new TestArticle
99+
{
100+
Id = 1,
101+
Title = "adsd",
102+
Body = "asda",
103+
Created = Convert.ToDateTime("2015-05-22T14:56:29.000Z"),
104+
Updated = Convert.ToDateTime("2015-05-22T14:56:29.000Z"),
105+
AuthorIds = new[] { "11", "12" }
106+
};
107+
108+
var serializedArticle = JsonConvert.SerializeObject(baseArticle);
109+
110+
var deSerialzedArticle = JsonConvert.DeserializeObject<TestArticle>(serializedArticle);
111+
112+
deSerialzedArticle.Id.Should().Equals(baseArticle.Id);
113+
deSerialzedArticle.Title.Should().Equals(baseArticle.Title);
114+
deSerialzedArticle.Body.Should().Equals(baseArticle.Body);
115+
deSerialzedArticle.Created.Should().Equals(baseArticle.Created);
116+
deSerialzedArticle.Updated.Should().Equals(baseArticle.Updated);
117+
deSerialzedArticle.AuthorIds.Should().NotBeNullOrEmpty();
118+
deSerialzedArticle.AuthorIds.Should().Contain(baseArticle.AuthorIds);
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)