Skip to content

Commit

Permalink
Merge branch 'release/0.14.0'
Browse files Browse the repository at this point in the history
* release/0.14.0:
  Fix package license warning
  Fix DateTimeOffset support.
  Added the ability to remove elements from JsonData objects as well as JsonData arrays.
  Fixed csproj casing
  • Loading branch information
devlead committed Sep 18, 2019
2 parents af47eaf + 033f8bc commit 087029d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 23 deletions.
8 changes: 5 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ string version = null,
semVersion = null,
milestone = null;

FilePath litjsonProjectPath = "./src/LitJson/LitJSON.csproj";

///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -129,17 +131,17 @@ Task("Test-SourceLink")
.IsDependentOn("Build")
.WithCriteria(IsRunningOnWindows())
.Does(() => {
foreach(var asssembly in GetFiles("./src/LitJSON/bin/" + configuration + "/**/*.dll"))
foreach(var asssembly in GetFiles("./src/LitJson/bin/" + configuration + "/**/*.dll"))
{
DotNetCoreTool("./src/LitJSON/LitJSON.csproj", "sourcelink", $"test {asssembly}");
DotNetCoreTool(litjsonProjectPath.FullPath, "sourcelink", $"test {asssembly}");
}
});

Task("Package")
.IsDependentOn("Test")
.IsDependentOn("Test-SourceLink")
.Does(() => {
DotNetCorePack("./src/LitJSON/LitJSON.csproj",
DotNetCorePack(litjsonProjectPath.FullPath,
new DotNetCorePackSettings {
Configuration = configuration,
NoBuild = true,
Expand Down
19 changes: 19 additions & 0 deletions src/LitJson/JsonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,25 @@ public int Add (object value)
return EnsureList ().Add (data);
}

public bool Remove(object obj)
{
json = null;
if(IsObject)
{
JsonData value = null;
if (inst_object.TryGetValue((string)obj, out value))
return inst_object.Remove((string)obj) && object_list.Remove(new KeyValuePair<string, JsonData>((string)obj, value));
else
throw new KeyNotFoundException("The specified key was not found in the JsonData object.");
}
if(IsArray)
{
return inst_array.Remove(ToJsonData(obj));
}
throw new InvalidOperationException (
"Instance of JsonData is not an object or a list.");
}

public void Clear ()
{
if (IsObject) {
Expand Down
5 changes: 5 additions & 0 deletions src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ private static void RegisterBaseExporters ()
delegate (object obj, JsonWriter writer) {
writer.Write ((ulong) obj);
};

base_exporters_table[typeof(DateTimeOffset)] =
delegate (object obj, JsonWriter writer) {
writer.Write(((DateTimeOffset)obj).ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz", datetime_format));
};
}

private static void RegisterBaseImporters ()
Expand Down
2 changes: 1 addition & 1 deletion src/LitJson/LitJSON.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It's quick and lean, without external dependencies.</Description>
<Copyright>The authors disclaim copyright to this source code.</Copyright>
<Authors>Leonardo Boshell, Mattias Karlsson and contributors</Authors>
<Company>Leonardo Boshell, Mattias Karlsson and contributors</Company>
<PackageLicenseUrl>https://github.com/LitJSON/litjson/blob/develop/COPYING</PackageLicenseUrl>
<PackageLicenseExpression>Unlicense</PackageLicenseExpression>
<PackageIconUrl>https://litjson.net/assets/img/logo.png</PackageIconUrl>
<RepositoryUrl>https://github.com/LitJSON/litjson</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
43 changes: 43 additions & 0 deletions test/JsonDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,49 @@ public void NullValue ()
Assert.AreEqual (json, data.ToJson ());
}

[Test]
public void RemoveValueFromObject()
{
string json = "{\"test1\":1}";

JsonData data = new JsonData();
data["test1"] = 1;
data["test2"] = 2;
data.Remove("test2");

Assert.AreEqual(json, data.ToJson());
}

[Test]
public void RemoveValueFromNestedObject()
{
string json = "{\"test1\":{\"test3\":3}}";

JsonData data = new JsonData();
data["test1"] = new JsonData();
data["test1"]["test2"] = 2;
data["test1"]["test3"] = 3;
data["test1"].Remove("test2");

Assert.AreEqual(json, data.ToJson());
}

[Test]
public void RemoveValueFromArray()
{
string json = "[\"test1\",2.0]";

JsonData data = new JsonData();
data.Add("test1");
data.Add("test2");
data.Remove("test2");
data.Add(1);
data.Add(2.0);
data.Remove(1);

Assert.AreEqual(json, data.ToJson());
}

[Test]
public void PropertiesOrderTest ()
{
Expand Down
48 changes: 29 additions & 19 deletions test/JsonMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ public class UiWindow

public class ValueTypesTest
{
public byte TestByte;
public char TestChar;
public DateTime TestDateTime;
public decimal TestDecimal;
public sbyte TestSByte;
public short TestShort;
public ushort TestUShort;
public uint TestUInt;
public ulong TestULong;
public byte TestByte;
public char TestChar;
public DateTime TestDateTime;
public decimal TestDecimal;
public sbyte TestSByte;
public short TestShort;
public ushort TestUShort;
public uint TestUInt;
public ulong TestULong;
public DateTimeOffset TestDateTimeOffset;
}

public class NullableTypesTest
Expand Down Expand Up @@ -399,13 +400,17 @@ public void ExportValueTypesTest ()
test.TestUShort = 30000;
test.TestUInt = 90000000;
test.TestULong = 0xFFFFFFFFFFFFFFFF; // = =18446744073709551615
test.TestDateTimeOffset =
new DateTimeOffset(2019, 9, 18, 16, 47,
50, 123, TimeSpan.FromHours(8)).AddTicks(4567);

string json = JsonMapper.ToJson (test);
string expected =
"{\"TestByte\":200,\"TestChar\":\"P\",\"TestDateTime\":" +
"\"12/22/2012 00:00:00\",\"TestDecimal\":10.333," +
"\"TestSByte\":-5,\"TestShort\":1024,\"TestUShort\":30000" +
",\"TestUInt\":90000000,\"TestULong\":18446744073709551615}";
",\"TestUInt\":90000000,\"TestULong\":18446744073709551615" +
",\"TestDateTimeOffset\":\"2019-09-18T16:47:50.1234567+08:00\"}";

Assert.AreEqual (expected, json);
}
Expand Down Expand Up @@ -825,15 +830,16 @@ public void ImportValueTypesTest ()
{
string json = @"
{
""TestByte"": 200,
""TestChar"": 'P',
""TestDateTime"": ""12/22/2012 00:00:00"",
""TestDecimal"": 10.333,
""TestSByte"": -5,
""TestShort"": 1024,
""TestUShort"": 30000,
""TestUInt"": 90000000,
""TestULong"": 18446744073709551615
""TestByte"": 200,
""TestChar"": 'P',
""TestDateTime"": ""12/22/2012 00:00:00"",
""TestDecimal"": 10.333,
""TestSByte"": -5,
""TestShort"": 1024,
""TestUShort"": 30000,
""TestUInt"": 90000000,
""TestULong"": 18446744073709551615,
""TestDateTimeOffset"": ""2019-09-18T16:47:50.1234567+08:00""
}";

ValueTypesTest test = JsonMapper.ToObject<ValueTypesTest> (json);
Expand All @@ -848,6 +854,10 @@ public void ImportValueTypesTest ()
Assert.AreEqual (30000, test.TestUShort, "A7");
Assert.AreEqual (90000000, test.TestUInt, "A8");
Assert.AreEqual (18446744073709551615L, test.TestULong, "A9");
Assert.AreEqual(
new DateTimeOffset(2019, 9, 18, 16, 47,
50, 123, TimeSpan.FromHours(8)).AddTicks(4567),
test.TestDateTimeOffset, "A10");
}

[Test]
Expand Down

0 comments on commit 087029d

Please sign in to comment.