Skip to content

Commit

Permalink
Add JsonObject support to decimal (#14)
Browse files Browse the repository at this point in the history
* Add JsonObject support to decimal

* Update unit tests to handle decimal numbers
  • Loading branch information
gonzalozawa authored Nov 2, 2017
1 parent 5150577 commit df887b6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 30 deletions.
8 changes: 6 additions & 2 deletions LearnositySDK.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnositySDK", "LearnositySDK\LearnositySDK.csproj", "{4A1F8BE2-02A2-424D-AE33-11F742E18EED}"
EndProject
Expand All @@ -17,6 +17,10 @@ Global
{4A1F8BE2-02A2-424D-AE33-11F742E18EED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A1F8BE2-02A2-424D-AE33-11F742E18EED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A1F8BE2-02A2-424D-AE33-11F742E18EED}.Release|Any CPU.Build.0 = Release|Any CPU
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 4 additions & 2 deletions LearnositySDK/LearnositySDK.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LearnositySDK</RootNamespace>
<AssemblyName>LearnositySDK</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -30,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand Down
36 changes: 18 additions & 18 deletions LearnositySDK/Utils/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JsonObject : ICloneable
private bool array;
private int arrayIndex;
private Dictionary<string, int> di;
private Dictionary<string, float> df;
private Dictionary<string, decimal> dd;
private Dictionary<string, string> ds;
private Dictionary<string, JsonObject> dj;
private Dictionary<string, bool> db;
Expand All @@ -27,13 +27,13 @@ public JsonObject(bool isArray = false)
this.array = isArray;
this.arrayIndex = 0; // lastInsertedIndex
this.di = new Dictionary<string, int>();
this.df = new Dictionary<string, float>();
this.dd = new Dictionary<string, decimal>();
this.ds = new Dictionary<string, string>();
this.dj = new Dictionary<string, JsonObject>();
this.db = new Dictionary<string, bool>();
this.da = new Dictionary<string, JsonObject>();
this.dt = new Dictionary<string, JToken>();
this.types = new string[7] { "int", "string", "JsonObject", "bool", "JsonArray", "float", "NULL" };
this.types = new string[7] { "int", "string", "JsonObject", "bool", "JsonArray", "decimal", "NULL" };
}

public bool isArray()
Expand All @@ -55,7 +55,7 @@ public void set(int value)
/// Sets/adds the value
/// </summary>
/// <param name="value"></param>
public void set(float value)
public void set(decimal value)
{
this.set(this.arrayIndex, value);
this.arrayIndex++;
Expand Down Expand Up @@ -138,7 +138,7 @@ public void set(int key, int value)
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void set(int key, float value)
public void set(int key, decimal value)
{
this.set(key.ToString(), value);
}
Expand Down Expand Up @@ -208,9 +208,9 @@ public void set(string key, int value)
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void set(string key, float value)
public void set(string key, decimal value)
{
this.set("float", key, value);
this.set("decimal", key, value);
}

/// <summary>
Expand Down Expand Up @@ -268,7 +268,7 @@ public void remove(string key)
{
this.db.Remove(key);
this.di.Remove(key);
this.df.Remove(key);
this.dd.Remove(key);
this.ds.Remove(key);
this.dj.Remove(key);
this.da.Remove(key);
Expand Down Expand Up @@ -325,7 +325,7 @@ public void set(string type, string key, Object value)
break;
case 4: this.da.Add(key, (JsonObject)value);
break;
case 5: this.df.Add(key, (float)value);
case 5: this.dd.Add(key, (decimal)value);
break;
case 6: this.dt.Add(key, (JToken)value);
break;
Expand Down Expand Up @@ -453,10 +453,10 @@ public Object get(string key, ref string type)
return this.da[key];
}

if (this.df.ContainsKey(key))
if (this.dd.ContainsKey(key))
{
type = "float";
return this.df[key];
type = "decimal";
return this.dd[key];
}

if (this.dt.ContainsKey(key))
Expand Down Expand Up @@ -530,7 +530,7 @@ public string[] getKeys()
l.Add(item.Key);
}

foreach (KeyValuePair<string, float> item in this.df)
foreach (KeyValuePair<string, decimal> item in this.dd)
{
l.Add(item.Key);
}
Expand Down Expand Up @@ -578,7 +578,7 @@ public int count()
count += this.db.Count;
count += this.dj.Count;
count += this.da.Count;
count += this.df.Count;
count += this.dd.Count;
count += this.dt.Count;

return count;
Expand Down Expand Up @@ -625,9 +625,9 @@ public string toJson()
{
sb.Append(this.di[key].ToString());
}
else if (this.df.ContainsKey(key))
else if (this.dd.ContainsKey(key))
{
sb.Append(this.df[key].ToString("R", CultureInfo.InvariantCulture));
sb.Append(this.dd[key].ToString(CultureInfo.InvariantCulture));
}
else if (this.ds.ContainsKey(key))
{
Expand Down Expand Up @@ -671,14 +671,14 @@ public string toJson()
index++;
}

foreach (KeyValuePair<string, float> item in this.df)
foreach (KeyValuePair<string, decimal> item in this.dd)
{
if (index > 0)
{
sb.Append(",");
}

sb.Append(Json.encode(item.Key) + ":" + item.Value.ToString("R", CultureInfo.InvariantCulture));
sb.Append(Json.encode(item.Key) + ":" + item.Value.ToString(CultureInfo.InvariantCulture));

index++;
}
Expand Down
2 changes: 1 addition & 1 deletion LearnositySDK/Utils/JsonObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static JsonObject fromJToken(JsonObject jsonObject, string key, JToken it
jsonObject.set(key, (int)item);
break;
case JTokenType.Float:
jsonObject.set(key, (float)item);
jsonObject.set(key, (decimal)item);
break;
case JTokenType.String:
jsonObject.set(key, (string)item);
Expand Down
2 changes: 1 addition & 1 deletion LearnositySDK/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="7.0.1" targetFramework="net35" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
</packages>
6 changes: 3 additions & 3 deletions UnitTestProject1/CheckingEqualityUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CheckingEqualityUnitTests
[TestMethod]
public void inputJSONEqualsOutput()
{
string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}";
string JSON = "{\"boolean\":true,\"integer\":1,\"decimal\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}";
JsonObject jo = JsonObjectFactory.fromString(JSON);
string outputJSON = jo.toJson();

Expand Down Expand Up @@ -45,7 +45,7 @@ public void inputJSONEqualsOutput()
[TestMethod]
public void JSONEqualityMethodWorksProperly()
{
string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}";
string JSON = "{\"boolean\":true,\"integer\":1,\"decimal\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}";

// building a structure for comparison purposes
JsonObject jo = new JsonObject();
Expand All @@ -54,7 +54,7 @@ public void JSONEqualityMethodWorksProperly()
JsonObject jaInner = new JsonObject(true);
jo.set("boolean", true);
jo.set("integer", 1);
jo.set("float", 1.2f);
jo.set("decimal", 1.2m);
jo.set("string", "string");
jo.set("object", joInner);
jo.set("array", ja);
Expand Down
4 changes: 3 additions & 1 deletion UnitTestProject1/LearnositySDKUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UnitTestProject1</RootNamespace>
<AssemblyName>UnitTestProject1</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
Expand All @@ -26,6 +26,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -34,6 +35,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand Down
4 changes: 2 additions & 2 deletions UnitTestProject1/MergingObjectsUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void RecursiveMergingJSONObjectsWorksProperly()
jo1a.setNull();
jo1a.set("string");
jo1a.set(1);
jo1a.set(1.2f);
jo1a.set(1.2m);
jo1a.set(jo1ao1);
jo1a.set(jo1ao2);
jo1ao1.set("a", "a1");
Expand All @@ -158,7 +158,7 @@ public void RecursiveMergingJSONObjectsWorksProperly()
jo2a.setNull();
jo2a.set("string");
jo2a.set(1);
jo2a.set(1.2f);
jo2a.set(1.2m);
jo2a.set(jo2ao1);
jo2a.set(jo2ao2);
jo2ao1.set("a", "a2");
Expand Down

0 comments on commit df887b6

Please sign in to comment.