Skip to content

Commit

Permalink
GH-33: Implement missing json readers for authentication objects
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Aug 11, 2018
1 parent 9c5bb5a commit e371e98
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace TraktNet.Objects.Authentication.Json.Reader
{
using Newtonsoft.Json;
using Objects.Json;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

internal class AuthorizationArrayJsonReader : AArrayJsonReader<ITraktAuthorization>
{
public override async Task<IEnumerable<ITraktAuthorization>> ReadArrayAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
if (jsonReader == null)
return await Task.FromResult(default(IEnumerable<ITraktAuthorization>));

if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartArray)
{
var authorizationReader = new AuthorizationObjectJsonReader();
var authorizations = new List<ITraktAuthorization>();
ITraktAuthorization authorization = await authorizationReader.ReadObjectAsync(jsonReader, cancellationToken);

while (authorization != null)
{
authorizations.Add(authorization);
authorization = await authorizationReader.ReadObjectAsync(jsonReader, cancellationToken);
}

return authorizations;
}

return await Task.FromResult(default(IEnumerable<ITraktAuthorization>));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace TraktNet.Objects.Authentication.Json.Reader
{
using Newtonsoft.Json;
using Objects.Json;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

internal class DeviceArrayJsonReader : AArrayJsonReader<ITraktDevice>
{
public override async Task<IEnumerable<ITraktDevice>> ReadArrayAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
if (jsonReader == null)
return await Task.FromResult(default(IEnumerable<ITraktDevice>));

if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartArray)
{
var deviceReader = new DeviceObjectJsonReader();
var devices = new List<ITraktDevice>();
ITraktDevice device = await deviceReader.ReadObjectAsync(jsonReader, cancellationToken);

while (device != null)
{
devices.Add(device);
device = await deviceReader.ReadObjectAsync(jsonReader, cancellationToken);
}

return devices;
}

return await Task.FromResult(default(IEnumerable<ITraktDevice>));
}
}
}

0 comments on commit e371e98

Please sign in to comment.