Skip to content

Commit df887b6

Browse files
authored
Add JsonObject support to decimal (#14)
* Add JsonObject support to decimal * Update unit tests to handle decimal numbers
1 parent 5150577 commit df887b6

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
lines changed

LearnositySDK.sln

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.40629.0
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnositySDK", "LearnositySDK\LearnositySDK.csproj", "{4A1F8BE2-02A2-424D-AE33-11F742E18EED}"
77
EndProject
@@ -17,6 +17,10 @@ Global
1717
{4A1F8BE2-02A2-424D-AE33-11F742E18EED}.Debug|Any CPU.Build.0 = Debug|Any CPU
1818
{4A1F8BE2-02A2-424D-AE33-11F742E18EED}.Release|Any CPU.ActiveCfg = Release|Any CPU
1919
{4A1F8BE2-02A2-424D-AE33-11F742E18EED}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{1A1FFC7A-6BBA-4B37-8734-25A4AB46E0C2}.Release|Any CPU.Build.0 = Release|Any CPU
2024
EndGlobalSection
2125
GlobalSection(SolutionProperties) = preSolution
2226
HideSolutionNode = FALSE

LearnositySDK/LearnositySDK.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
55
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -10,7 +10,7 @@
1010
<AppDesignerFolder>Properties</AppDesignerFolder>
1111
<RootNamespace>LearnositySDK</RootNamespace>
1212
<AssemblyName>LearnositySDK</AssemblyName>
13-
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
13+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
1414
<FileAlignment>512</FileAlignment>
1515
<TargetFrameworkProfile />
1616
</PropertyGroup>
@@ -22,6 +22,7 @@
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25+
<Prefer32Bit>false</Prefer32Bit>
2526
</PropertyGroup>
2627
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2728
<DebugType>pdbonly</DebugType>
@@ -30,6 +31,7 @@
3031
<DefineConstants>TRACE</DefineConstants>
3132
<ErrorReport>prompt</ErrorReport>
3233
<WarningLevel>4</WarningLevel>
34+
<Prefer32Bit>false</Prefer32Bit>
3335
</PropertyGroup>
3436
<ItemGroup>
3537
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">

LearnositySDK/Utils/JsonObject.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class JsonObject : ICloneable
1414
private bool array;
1515
private int arrayIndex;
1616
private Dictionary<string, int> di;
17-
private Dictionary<string, float> df;
17+
private Dictionary<string, decimal> dd;
1818
private Dictionary<string, string> ds;
1919
private Dictionary<string, JsonObject> dj;
2020
private Dictionary<string, bool> db;
@@ -27,13 +27,13 @@ public JsonObject(bool isArray = false)
2727
this.array = isArray;
2828
this.arrayIndex = 0; // lastInsertedIndex
2929
this.di = new Dictionary<string, int>();
30-
this.df = new Dictionary<string, float>();
30+
this.dd = new Dictionary<string, decimal>();
3131
this.ds = new Dictionary<string, string>();
3232
this.dj = new Dictionary<string, JsonObject>();
3333
this.db = new Dictionary<string, bool>();
3434
this.da = new Dictionary<string, JsonObject>();
3535
this.dt = new Dictionary<string, JToken>();
36-
this.types = new string[7] { "int", "string", "JsonObject", "bool", "JsonArray", "float", "NULL" };
36+
this.types = new string[7] { "int", "string", "JsonObject", "bool", "JsonArray", "decimal", "NULL" };
3737
}
3838

3939
public bool isArray()
@@ -55,7 +55,7 @@ public void set(int value)
5555
/// Sets/adds the value
5656
/// </summary>
5757
/// <param name="value"></param>
58-
public void set(float value)
58+
public void set(decimal value)
5959
{
6060
this.set(this.arrayIndex, value);
6161
this.arrayIndex++;
@@ -138,7 +138,7 @@ public void set(int key, int value)
138138
/// </summary>
139139
/// <param name="key"></param>
140140
/// <param name="value"></param>
141-
public void set(int key, float value)
141+
public void set(int key, decimal value)
142142
{
143143
this.set(key.ToString(), value);
144144
}
@@ -208,9 +208,9 @@ public void set(string key, int value)
208208
/// </summary>
209209
/// <param name="key"></param>
210210
/// <param name="value"></param>
211-
public void set(string key, float value)
211+
public void set(string key, decimal value)
212212
{
213-
this.set("float", key, value);
213+
this.set("decimal", key, value);
214214
}
215215

216216
/// <summary>
@@ -268,7 +268,7 @@ public void remove(string key)
268268
{
269269
this.db.Remove(key);
270270
this.di.Remove(key);
271-
this.df.Remove(key);
271+
this.dd.Remove(key);
272272
this.ds.Remove(key);
273273
this.dj.Remove(key);
274274
this.da.Remove(key);
@@ -325,7 +325,7 @@ public void set(string type, string key, Object value)
325325
break;
326326
case 4: this.da.Add(key, (JsonObject)value);
327327
break;
328-
case 5: this.df.Add(key, (float)value);
328+
case 5: this.dd.Add(key, (decimal)value);
329329
break;
330330
case 6: this.dt.Add(key, (JToken)value);
331331
break;
@@ -453,10 +453,10 @@ public Object get(string key, ref string type)
453453
return this.da[key];
454454
}
455455

456-
if (this.df.ContainsKey(key))
456+
if (this.dd.ContainsKey(key))
457457
{
458-
type = "float";
459-
return this.df[key];
458+
type = "decimal";
459+
return this.dd[key];
460460
}
461461

462462
if (this.dt.ContainsKey(key))
@@ -530,7 +530,7 @@ public string[] getKeys()
530530
l.Add(item.Key);
531531
}
532532

533-
foreach (KeyValuePair<string, float> item in this.df)
533+
foreach (KeyValuePair<string, decimal> item in this.dd)
534534
{
535535
l.Add(item.Key);
536536
}
@@ -578,7 +578,7 @@ public int count()
578578
count += this.db.Count;
579579
count += this.dj.Count;
580580
count += this.da.Count;
581-
count += this.df.Count;
581+
count += this.dd.Count;
582582
count += this.dt.Count;
583583

584584
return count;
@@ -625,9 +625,9 @@ public string toJson()
625625
{
626626
sb.Append(this.di[key].ToString());
627627
}
628-
else if (this.df.ContainsKey(key))
628+
else if (this.dd.ContainsKey(key))
629629
{
630-
sb.Append(this.df[key].ToString("R", CultureInfo.InvariantCulture));
630+
sb.Append(this.dd[key].ToString(CultureInfo.InvariantCulture));
631631
}
632632
else if (this.ds.ContainsKey(key))
633633
{
@@ -671,14 +671,14 @@ public string toJson()
671671
index++;
672672
}
673673

674-
foreach (KeyValuePair<string, float> item in this.df)
674+
foreach (KeyValuePair<string, decimal> item in this.dd)
675675
{
676676
if (index > 0)
677677
{
678678
sb.Append(",");
679679
}
680680

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

683683
index++;
684684
}

LearnositySDK/Utils/JsonObjectFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static JsonObject fromJToken(JsonObject jsonObject, string key, JToken it
9696
jsonObject.set(key, (int)item);
9797
break;
9898
case JTokenType.Float:
99-
jsonObject.set(key, (float)item);
99+
jsonObject.set(key, (decimal)item);
100100
break;
101101
case JTokenType.String:
102102
jsonObject.set(key, (string)item);

LearnositySDK/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net35" />
3+
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
44
</packages>

UnitTestProject1/CheckingEqualityUnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CheckingEqualityUnitTests
1414
[TestMethod]
1515
public void inputJSONEqualsOutput()
1616
{
17-
string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}";
17+
string JSON = "{\"boolean\":true,\"integer\":1,\"decimal\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}";
1818
JsonObject jo = JsonObjectFactory.fromString(JSON);
1919
string outputJSON = jo.toJson();
2020

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

5050
// building a structure for comparison purposes
5151
JsonObject jo = new JsonObject();
@@ -54,7 +54,7 @@ public void JSONEqualityMethodWorksProperly()
5454
JsonObject jaInner = new JsonObject(true);
5555
jo.set("boolean", true);
5656
jo.set("integer", 1);
57-
jo.set("float", 1.2f);
57+
jo.set("decimal", 1.2m);
5858
jo.set("string", "string");
5959
jo.set("object", joInner);
6060
jo.set("array", ja);

UnitTestProject1/LearnositySDKUnitTests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<AppDesignerFolder>Properties</AppDesignerFolder>
99
<RootNamespace>UnitTestProject1</RootNamespace>
1010
<AssemblyName>UnitTestProject1</AssemblyName>
11-
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1414
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
@@ -26,6 +26,7 @@
2626
<DefineConstants>DEBUG;TRACE</DefineConstants>
2727
<ErrorReport>prompt</ErrorReport>
2828
<WarningLevel>4</WarningLevel>
29+
<Prefer32Bit>false</Prefer32Bit>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugType>pdbonly</DebugType>
@@ -34,6 +35,7 @@
3435
<DefineConstants>TRACE</DefineConstants>
3536
<ErrorReport>prompt</ErrorReport>
3637
<WarningLevel>4</WarningLevel>
38+
<Prefer32Bit>false</Prefer32Bit>
3739
</PropertyGroup>
3840
<ItemGroup>
3941
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">

UnitTestProject1/MergingObjectsUnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void RecursiveMergingJSONObjectsWorksProperly()
140140
jo1a.setNull();
141141
jo1a.set("string");
142142
jo1a.set(1);
143-
jo1a.set(1.2f);
143+
jo1a.set(1.2m);
144144
jo1a.set(jo1ao1);
145145
jo1a.set(jo1ao2);
146146
jo1ao1.set("a", "a1");
@@ -158,7 +158,7 @@ public void RecursiveMergingJSONObjectsWorksProperly()
158158
jo2a.setNull();
159159
jo2a.set("string");
160160
jo2a.set(1);
161-
jo2a.set(1.2f);
161+
jo2a.set(1.2m);
162162
jo2a.set(jo2ao1);
163163
jo2a.set(jo2ao2);
164164
jo2ao1.set("a", "a2");

0 commit comments

Comments
 (0)