Skip to content

Commit 087029d

Browse files
committed
Merge branch 'release/0.14.0'
* 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
2 parents af47eaf + 033f8bc commit 087029d

File tree

6 files changed

+102
-23
lines changed

6 files changed

+102
-23
lines changed

build.cake

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ string version = null,
1818
semVersion = null,
1919
milestone = null;
2020

21+
FilePath litjsonProjectPath = "./src/LitJson/LitJSON.csproj";
22+
2123
///////////////////////////////////////////////////////////////////////////////
2224
// SETUP / TEARDOWN
2325
///////////////////////////////////////////////////////////////////////////////
@@ -129,17 +131,17 @@ Task("Test-SourceLink")
129131
.IsDependentOn("Build")
130132
.WithCriteria(IsRunningOnWindows())
131133
.Does(() => {
132-
foreach(var asssembly in GetFiles("./src/LitJSON/bin/" + configuration + "/**/*.dll"))
134+
foreach(var asssembly in GetFiles("./src/LitJson/bin/" + configuration + "/**/*.dll"))
133135
{
134-
DotNetCoreTool("./src/LitJSON/LitJSON.csproj", "sourcelink", $"test {asssembly}");
136+
DotNetCoreTool(litjsonProjectPath.FullPath, "sourcelink", $"test {asssembly}");
135137
}
136138
});
137139

138140
Task("Package")
139141
.IsDependentOn("Test")
140142
.IsDependentOn("Test-SourceLink")
141143
.Does(() => {
142-
DotNetCorePack("./src/LitJSON/LitJSON.csproj",
144+
DotNetCorePack(litjsonProjectPath.FullPath,
143145
new DotNetCorePackSettings {
144146
Configuration = configuration,
145147
NoBuild = true,

src/LitJson/JsonData.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,25 @@ public int Add (object value)
819819
return EnsureList ().Add (data);
820820
}
821821

822+
public bool Remove(object obj)
823+
{
824+
json = null;
825+
if(IsObject)
826+
{
827+
JsonData value = null;
828+
if (inst_object.TryGetValue((string)obj, out value))
829+
return inst_object.Remove((string)obj) && object_list.Remove(new KeyValuePair<string, JsonData>((string)obj, value));
830+
else
831+
throw new KeyNotFoundException("The specified key was not found in the JsonData object.");
832+
}
833+
if(IsArray)
834+
{
835+
return inst_array.Remove(ToJsonData(obj));
836+
}
837+
throw new InvalidOperationException (
838+
"Instance of JsonData is not an object or a list.");
839+
}
840+
822841
public void Clear ()
823842
{
824843
if (IsObject) {

src/LitJson/JsonMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ private static void RegisterBaseExporters ()
603603
delegate (object obj, JsonWriter writer) {
604604
writer.Write ((ulong) obj);
605605
};
606+
607+
base_exporters_table[typeof(DateTimeOffset)] =
608+
delegate (object obj, JsonWriter writer) {
609+
writer.Write(((DateTimeOffset)obj).ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz", datetime_format));
610+
};
606611
}
607612

608613
private static void RegisterBaseImporters ()

src/LitJson/LitJSON.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ It's quick and lean, without external dependencies.</Description>
2323
<Copyright>The authors disclaim copyright to this source code.</Copyright>
2424
<Authors>Leonardo Boshell, Mattias Karlsson and contributors</Authors>
2525
<Company>Leonardo Boshell, Mattias Karlsson and contributors</Company>
26-
<PackageLicenseUrl>https://github.com/LitJSON/litjson/blob/develop/COPYING</PackageLicenseUrl>
26+
<PackageLicenseExpression>Unlicense</PackageLicenseExpression>
2727
<PackageIconUrl>https://litjson.net/assets/img/logo.png</PackageIconUrl>
2828
<RepositoryUrl>https://github.com/LitJSON/litjson</RepositoryUrl>
2929
<RepositoryType>git</RepositoryType>

test/JsonDataTest.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,49 @@ public void NullValue ()
231231
Assert.AreEqual (json, data.ToJson ());
232232
}
233233

234+
[Test]
235+
public void RemoveValueFromObject()
236+
{
237+
string json = "{\"test1\":1}";
238+
239+
JsonData data = new JsonData();
240+
data["test1"] = 1;
241+
data["test2"] = 2;
242+
data.Remove("test2");
243+
244+
Assert.AreEqual(json, data.ToJson());
245+
}
246+
247+
[Test]
248+
public void RemoveValueFromNestedObject()
249+
{
250+
string json = "{\"test1\":{\"test3\":3}}";
251+
252+
JsonData data = new JsonData();
253+
data["test1"] = new JsonData();
254+
data["test1"]["test2"] = 2;
255+
data["test1"]["test3"] = 3;
256+
data["test1"].Remove("test2");
257+
258+
Assert.AreEqual(json, data.ToJson());
259+
}
260+
261+
[Test]
262+
public void RemoveValueFromArray()
263+
{
264+
string json = "[\"test1\",2.0]";
265+
266+
JsonData data = new JsonData();
267+
data.Add("test1");
268+
data.Add("test2");
269+
data.Remove("test2");
270+
data.Add(1);
271+
data.Add(2.0);
272+
data.Remove(1);
273+
274+
Assert.AreEqual(json, data.ToJson());
275+
}
276+
234277
[Test]
235278
public void PropertiesOrderTest ()
236279
{

test/JsonMapperTest.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,16 @@ public class UiWindow
162162

163163
public class ValueTypesTest
164164
{
165-
public byte TestByte;
166-
public char TestChar;
167-
public DateTime TestDateTime;
168-
public decimal TestDecimal;
169-
public sbyte TestSByte;
170-
public short TestShort;
171-
public ushort TestUShort;
172-
public uint TestUInt;
173-
public ulong TestULong;
165+
public byte TestByte;
166+
public char TestChar;
167+
public DateTime TestDateTime;
168+
public decimal TestDecimal;
169+
public sbyte TestSByte;
170+
public short TestShort;
171+
public ushort TestUShort;
172+
public uint TestUInt;
173+
public ulong TestULong;
174+
public DateTimeOffset TestDateTimeOffset;
174175
}
175176

176177
public class NullableTypesTest
@@ -399,13 +400,17 @@ public void ExportValueTypesTest ()
399400
test.TestUShort = 30000;
400401
test.TestUInt = 90000000;
401402
test.TestULong = 0xFFFFFFFFFFFFFFFF; // = =18446744073709551615
403+
test.TestDateTimeOffset =
404+
new DateTimeOffset(2019, 9, 18, 16, 47,
405+
50, 123, TimeSpan.FromHours(8)).AddTicks(4567);
402406

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

410415
Assert.AreEqual (expected, json);
411416
}
@@ -825,15 +830,16 @@ public void ImportValueTypesTest ()
825830
{
826831
string json = @"
827832
{
828-
""TestByte"": 200,
829-
""TestChar"": 'P',
830-
""TestDateTime"": ""12/22/2012 00:00:00"",
831-
""TestDecimal"": 10.333,
832-
""TestSByte"": -5,
833-
""TestShort"": 1024,
834-
""TestUShort"": 30000,
835-
""TestUInt"": 90000000,
836-
""TestULong"": 18446744073709551615
833+
""TestByte"": 200,
834+
""TestChar"": 'P',
835+
""TestDateTime"": ""12/22/2012 00:00:00"",
836+
""TestDecimal"": 10.333,
837+
""TestSByte"": -5,
838+
""TestShort"": 1024,
839+
""TestUShort"": 30000,
840+
""TestUInt"": 90000000,
841+
""TestULong"": 18446744073709551615,
842+
""TestDateTimeOffset"": ""2019-09-18T16:47:50.1234567+08:00""
837843
}";
838844

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

853863
[Test]

0 commit comments

Comments
 (0)