Skip to content

Commit

Permalink
Upgrade to json.net 13 for #248
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Apr 8, 2021
1 parent a3589cc commit 34dcf27
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/Q42.HueApi.Streaming/Q42.HueApi.Streaming.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
<RepositoryType>GitHub</RepositoryType>
<PackageTags>philips hue lights entertainment</PackageTags>
<PackageId>Q42.HueApi.Entertainment</PackageId>
<Version>3.15.0</Version>
<Version>3.18.0-beta1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.9" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Q42.HueApi.Tests/Q42.HueApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand Down
2 changes: 1 addition & 1 deletion src/Q42.HueApi.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net471" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net471" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
<Version>13.0.1</Version>
</PackageReference>
<PackageReference Include="Q42.WinRT">
<Version>1.5.2.42</Version>
Expand Down
14 changes: 9 additions & 5 deletions src/Q42.HueApi/HttpBridgeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ public override async Task<IEnumerable<LocatedBridge>> LocateBridgesAsync(Cancel
{
string content = await response.Content.ReadAsStringAsync();

NuPnPResponse[] responseModel = JsonConvert.DeserializeObject<NuPnPResponse[]>(content);

var locatedBridges = responseModel.Select(x => new LocatedBridge() { BridgeId = x.Id, IpAddress = x.InternalIpAddress }).ToList();
locatedBridges.ForEach(OnBridgeFound);
return locatedBridges;
NuPnPResponse[]? responseModel = JsonConvert.DeserializeObject<NuPnPResponse[]>(content);
if (responseModel != null)
{
var locatedBridges = responseModel.Select(x => new LocatedBridge() { BridgeId = x.Id, IpAddress = x.InternalIpAddress }).ToList();
locatedBridges.ForEach(OnBridgeFound);
return locatedBridges;
}
else
return Enumerable.Empty<LocatedBridge>();
}
else
{
Expand Down
35 changes: 19 additions & 16 deletions src/Q42.HueApi/HueClient-Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task<bool> DeleteWhiteListEntryAsync(string entry)
BridgeConfig? config = await GetConfigAsync().ConfigureAwait(false);
if (config == null)
return null;

return config.WhiteList.Select(l => l.Value).ToList();
}

Expand All @@ -80,30 +80,33 @@ public async Task<bool> DeleteWhiteListEntryAsync(string entry)

return new Bridge(jsonResult);
}


/// <summary>
/// Get bridge config
/// </summary>
/// <returns>BridgeConfig object</returns>
public async Task<BridgeConfig?> GetConfigAsync()
{
//Not needed to check if initialized, can be used without API key
//Not needed to check if initialized, can be used without API key

HttpClient client = await GetHttpClient().ConfigureAwait(false);
string stringResult = await client.GetStringAsync(new Uri(String.Format("{0}config", ApiBase))).ConfigureAwait(false);
JToken token = JToken.Parse(stringResult);
BridgeConfig? config = null;
if (token.Type == JTokenType.Object)
{
var jsonResult = (JObject)token;
config = JsonConvert.DeserializeObject<BridgeConfig>(jsonResult.ToString());
HttpClient client = await GetHttpClient().ConfigureAwait(false);
string stringResult = await client.GetStringAsync(new Uri(String.Format("{0}config", ApiBase))).ConfigureAwait(false);
JToken token = JToken.Parse(stringResult);
BridgeConfig? config = null;
if (token.Type == JTokenType.Object)
{
var jsonResult = (JObject)token;
config = JsonConvert.DeserializeObject<BridgeConfig>(jsonResult.ToString());

//Fix whitelist IDs
foreach (var whitelist in config.WhiteList)
whitelist.Value.Id = whitelist.Key;
if (config != null)
{
//Fix whitelist IDs
foreach (var whitelist in config.WhiteList)
whitelist.Value.Id = whitelist.Key;
}
return config;
}
return config;
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions src/Q42.HueApi/HueClient-Groups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ public async Task<IReadOnlyCollection<Group>> GetGroupsAsync()

foreach (var prop in jsonResult.Properties())
{
Group newGroup = JsonConvert.DeserializeObject<Group>(prop.Value.ToString());
newGroup.Id = prop.Name;
Group? newGroup = JsonConvert.DeserializeObject<Group>(prop.Value.ToString());
if (newGroup != null)
{
newGroup.Id = prop.Name;

results.Add(newGroup);
results.Add(newGroup);
}
}

}
Expand Down
18 changes: 12 additions & 6 deletions src/Q42.HueApi/HueClient-Lights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ public async Task<IEnumerable<Light>> GetLightsAsync()

foreach (var prop in jsonResult.Properties())
{
Light newLight = JsonConvert.DeserializeObject<Light>(prop.Value.ToString());
newLight.Id = prop.Name;
results.Add(newLight);
Light? newLight = JsonConvert.DeserializeObject<Light>(prop.Value.ToString());
if (newLight != null)
{
newLight.Id = prop.Name;
results.Add(newLight);
}
}
}
return results;
Expand Down Expand Up @@ -268,10 +271,13 @@ public async Task<IReadOnlyCollection<Light>> GetNewLightsAsync()
{
if (prop.Name != "lastscan")
{
Light newLight = JsonConvert.DeserializeObject<Light>(prop.Value.ToString());
newLight.Id = prop.Name;
Light? newLight = JsonConvert.DeserializeObject<Light>(prop.Value.ToString());
if (newLight != null)
{
newLight.Id = prop.Name;

results.Add(newLight);
results.Add(newLight);
}

}
}
Expand Down
13 changes: 8 additions & 5 deletions src/Q42.HueApi/HueClient-ResourceLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public async Task<IReadOnlyCollection<ResourceLink>> GetResourceLinksAsync()

foreach (var prop in jsonResult.Properties())
{
ResourceLink newResourceLink = JsonConvert.DeserializeObject<ResourceLink>(prop.Value.ToString());
newResourceLink.Id = prop.Name;
ResourceLink? newResourceLink = JsonConvert.DeserializeObject<ResourceLink>(prop.Value.ToString());
if (newResourceLink != null)
{
newResourceLink.Id = prop.Name;

results.Add(newResourceLink);
results.Add(newResourceLink);
}
}

}
Expand Down Expand Up @@ -111,9 +114,9 @@ public async Task<IReadOnlyCollection<ResourceLink>> GetResourceLinksAsync()

var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

DefaultHueResult[] resourceLinkResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult);
DefaultHueResult[]? resourceLinkResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult);

if (resourceLinkResult.Length > 0 && resourceLinkResult[0].Success != null && !string.IsNullOrEmpty(resourceLinkResult[0].Success.Id))
if (resourceLinkResult != null && resourceLinkResult.Length > 0 && resourceLinkResult[0].Success != null && !string.IsNullOrEmpty(resourceLinkResult[0].Success.Id))
{
return resourceLinkResult[0].Success.Id;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Q42.HueApi/HueClient-Rules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ public async Task<IReadOnlyCollection<Rule>> GetRulesAsync()

foreach (var prop in jsonResult.Properties())
{
Rule rule = JsonConvert.DeserializeObject<Rule>(prop.Value.ToString());
rule.Id = prop.Name;
Rule? rule = JsonConvert.DeserializeObject<Rule>(prop.Value.ToString());
if (rule != null)
{
rule.Id = prop.Name;

results.Add(rule);
results.Add(rule);
}
}

}
Expand Down
9 changes: 6 additions & 3 deletions src/Q42.HueApi/HueClient-Scenes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ public async Task<IReadOnlyCollection<Scene>> GetScenesAsync()

foreach (var prop in jsonResult.Properties())
{
Scene scene = JsonConvert.DeserializeObject<Scene>(prop.Value.ToString());
scene.Id = prop.Name;
Scene? scene = JsonConvert.DeserializeObject<Scene>(prop.Value.ToString());
if (scene != null)
{
scene.Id = prop.Name;

results.Add(scene);
results.Add(scene);
}
}

}
Expand Down
20 changes: 11 additions & 9 deletions src/Q42.HueApi/HueClient-Schedules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ public async Task<IReadOnlyCollection<Schedule>> GetSchedulesAsync()
//Each property is a light
var jsonResult = (JObject)token;

foreach (var prop in jsonResult.Properties())
{
Schedule newSchedule = JsonConvert.DeserializeObject<Schedule>(prop.Value.ToString());
newSchedule.Id = prop.Name;

results.Add(newSchedule);
}
foreach (var prop in jsonResult.Properties())
{
Schedule? newSchedule = JsonConvert.DeserializeObject<Schedule>(prop.Value.ToString());
if (newSchedule != null)
{
newSchedule.Id = prop.Name;
results.Add(newSchedule);
}
}

}

Expand Down Expand Up @@ -101,9 +103,9 @@ public async Task<IReadOnlyCollection<Schedule>> GetSchedulesAsync()

var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

DefaultHueResult[] scheduleResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult);
DefaultHueResult[]? scheduleResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult);

if (scheduleResult.Length > 0 && scheduleResult[0].Success != null && !string.IsNullOrEmpty(scheduleResult[0].Success.Id))
if (scheduleResult != null && scheduleResult.Length > 0 && scheduleResult[0].Success != null && !string.IsNullOrEmpty(scheduleResult[0].Success.Id))
{
return scheduleResult[0].Success.Id;
}
Expand Down
22 changes: 14 additions & 8 deletions src/Q42.HueApi/HueClient-Sensors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ public async Task<IReadOnlyCollection<Sensor>> GetSensorsAsync()

foreach (var prop in jsonResult.Properties())
{
Sensor scene = JsonConvert.DeserializeObject<Sensor>(prop.Value.ToString());
scene.Id = prop.Name;
Sensor? scene = JsonConvert.DeserializeObject<Sensor>(prop.Value.ToString());
if (scene != null)
{
scene.Id = prop.Name;

results.Add(scene);
results.Add(scene);
}
}

}
Expand Down Expand Up @@ -76,9 +79,9 @@ public async Task<IReadOnlyCollection<Sensor>> GetSensorsAsync()

var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

DefaultHueResult[] sensorResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult);
DefaultHueResult[]? sensorResult = JsonConvert.DeserializeObject<DefaultHueResult[]>(jsonResult);

if (sensorResult.Length > 0 && sensorResult[0].Success != null && !string.IsNullOrEmpty(sensorResult[0].Success.Id))
if (sensorResult != null && sensorResult.Length > 0 && sensorResult[0].Success != null && !string.IsNullOrEmpty(sensorResult[0].Success.Id))
{
var id = sensorResult[0].Success.Id;
sensor.Id = id;
Expand Down Expand Up @@ -133,10 +136,13 @@ public async Task<IReadOnlyCollection<Sensor>> GetNewSensorsAsync()
{
if (prop.Name != "lastscan")
{
Sensor newSensor = JsonConvert.DeserializeObject<Sensor>(prop.Value.ToString());
newSensor.Id = prop.Name;
Sensor? newSensor = JsonConvert.DeserializeObject<Sensor>(prop.Value.ToString());
if (newSensor != null)
{
newSensor.Id = prop.Name;

results.Add(newSensor);
results.Add(newSensor);
}

}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Q42.HueApi/HueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void CheckInitialized()
{
try
{
T objResult = JsonConvert.DeserializeObject<T>(json);
T? objResult = JsonConvert.DeserializeObject<T>(json);

return objResult;

Expand Down Expand Up @@ -118,7 +118,9 @@ protected static HueResults DeserializeDefaultHueResult(string json)

try
{
result = JsonConvert.DeserializeObject<HueResults>(json);
var jsonResult = JsonConvert.DeserializeObject<HueResults>(json);
if (jsonResult != null)
return jsonResult;
}
catch (JsonSerializationException)
{
Expand All @@ -139,7 +141,9 @@ private static IReadOnlyCollection<T> DeserializeDefaultHueResult<T>(string json

try
{
result = JsonConvert.DeserializeObject<List<T>>(json);
var jsonResult = JsonConvert.DeserializeObject<List<T>>(json);
if (jsonResult != null)
return jsonResult;
}
catch (JsonSerializationException)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Q42.HueApi/Models/Schedule/GenericScheduleCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Q42.HueApi.Interfaces;
using System;
Expand Down Expand Up @@ -37,17 +37,17 @@ public bool IsSceneCommand()

public SceneCommand AsSceneCommand()
{
return JsonConvert.DeserializeObject<SceneCommand>(this.JsonString);
return JsonConvert.DeserializeObject<SceneCommand>(this.JsonString)!;
}

public LightCommand AsLightCommand()
{
return JsonConvert.DeserializeObject<LightCommand>(this.JsonString);
return JsonConvert.DeserializeObject<LightCommand>(this.JsonString)!;
}

public SensorState AsSensorCommand()
{
return JsonConvert.DeserializeObject<SensorState>(this.JsonString);
return JsonConvert.DeserializeObject<SensorState>(this.JsonString)!;
}
}
}
4 changes: 2 additions & 2 deletions src/Q42.HueApi/Q42.HueApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8601;CS8602;CS8603;CS8625;CS8613</WarningsAsErrors>
<Version>3.17.0</Version>
<Version>3.18.0-beta1</Version>
<Company>Q42</Company>
<Authors>Michiel Post, Q42</Authors>
<Description>Open source library for interaction with the Philips Hue Bridge. Allows you to control your lights from C#. Supports .Net Standard 2.0 and .net45</Description>
Expand All @@ -20,7 +20,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" Condition="'$(TargetFramework)' == 'net45'" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 34dcf27

Please sign in to comment.