Skip to content

Commit

Permalink
Merge pull request #50 from GravityWolfNotAmused/dev
Browse files Browse the repository at this point in the history
Fix Issue with json parsing.
  • Loading branch information
GravityWolfNotAmused authored Sep 13, 2022
2 parents b8e8a2c + 5c33720 commit 468bbfe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
69 changes: 69 additions & 0 deletions DiscordPlayerCountBot.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using DiscordPlayerCountBot.Json;

namespace DiscordPlayerCountBot.Tests;

[Collection("Json Serialization Test Suite")]
public class JsonTests
{
[Fact(DisplayName = "Test Primitives Float", Timeout = 30)]
public void SerializePrimitivesFloat()
{
var testFloat = JsonHelper.DeserializeObject<float>("2.23");

Assert.True(testFloat == (float)2.23, $"{testFloat}");
Assert.True(testFloat.GetType() == typeof(float));
}

[Fact(DisplayName = "Test Primitives Double", Timeout = 30)]
public void SerializePrimitivesDouble()
{
var testDouble = JsonHelper.DeserializeObject<double>("200.00");

Assert.True(testDouble == 200.00);
Assert.True(testDouble.GetType() == typeof(double));

}

[Fact(DisplayName = "Test Primitives Char", Timeout = 30)]
public void SerializePrimitivesChar()
{
var testChar = JsonHelper.DeserializeObject<char>("3");

Assert.True(testChar == '3');
Assert.True(testChar.GetType() == typeof(char));
}

[Fact(DisplayName = "Test Primitives Bool", Timeout = 30)]
public void SerializePrimitivesBool()
{
var testBooleanTrue = JsonHelper.DeserializeObject<bool>("true");

Assert.True(testBooleanTrue);
Assert.True(testBooleanTrue.GetType() == typeof(bool));

var testBooleanFalse = JsonHelper.DeserializeObject<bool>("false");

Assert.True(!testBooleanFalse);
Assert.True(testBooleanFalse.GetType() == typeof(bool));
}

[Fact(DisplayName = "Test Primitives Int", Timeout = 30)]
public void SerializePrimitivesInt()
{
var testInt = JsonHelper.DeserializeObject<int>("100");

Assert.True(testInt == 100);
Assert.True(testInt.GetType() == typeof(int));
}

[Fact(DisplayName = "Test Strings", Timeout = 30)]
public void SerializeStrings()
{
var testString = JsonHelper.DeserializeObject<string>("test") ?? "";

Assert.True(!string.IsNullOrEmpty(testString), "Should not be null or empty.");
Assert.True(!string.IsNullOrWhiteSpace(testString), "Should not be null or whitespace.");
Assert.Equal("test", testString, true, true, true);
Assert.True(testString.GetType() == typeof(string));
}
}
3 changes: 3 additions & 0 deletions DiscordPlayerCountBot/Json/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Newtonsoft.Json;
using System;

namespace DiscordPlayerCountBot.Json
{
public static class JsonHelper
{
public static T? DeserializeObject<T>(string content)
{
if (typeof(T).IsPrimitive || typeof(T) == typeof(string)) return (T)Convert.ChangeType(content, typeof(T));

return JsonConvert.DeserializeObject<T>(content, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
Expand Down

0 comments on commit 468bbfe

Please sign in to comment.