Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand literals by default #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ nuget Costura.Fody
nuget Rdf.Vocabularies
nuget OpenCover
nuget Newtonsoft.Json ~> 9
nuget JsonDiffPatch.Net

gist tpluscode/a285267d2543466fc35c3a168c846f9f
4 changes: 3 additions & 1 deletion paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ NUGET
ImpromptuInterface (6.2.2)
InfoOf.Fody (1.0.4)
Fody (>= 1.29.2)
JsonDiffPatch.Net (1.0.5)
Newtonsoft.Json (>= 8.0.2)
json-ld.net (1.0.5)
Newtonsoft.Json (>= 6.0.4)
MethodTimer.Fody (1.16)
Expand All @@ -32,4 +34,4 @@ NUGET
VDS.Common (1.6.4)
GIST
remote: tpluscode/a285267d2543466fc35c3a168c846f9f
FULLPROJECT (eac8af5adfdcfc43b63a5d5edf88c082e83761e0)
FULLPROJECT (c9b7a831e53e929f2ecce09b14a61e28007b669f)
20 changes: 14 additions & 6 deletions src/JsonLD.Entities.Tests/Bindings/SerializingSteps.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using FakeItEasy;
using ImpromptuInterface;
using JsonDiffPatchDotNet;
using JsonLD.Entities.Tests.Entities;
using JsonLD.Entities.Tests.SpecflowHelpers;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using TechTalk.SpecFlow;
Expand All @@ -23,11 +25,10 @@ public SerializingSteps(SerializerTestContext context)
public void GivenAPersonWithoutId()
{
this.context.Object = new Person
{
Name = "Tomasz",
Surname = "Pluskiewicz",
BirthDate = new DateTime(1972, 9, 4)
};
{
Name = "Tomasz",
Surname = "Pluskiewicz"
};
}

[Given(@"model of type '(.*)'")]
Expand All @@ -37,6 +38,13 @@ public void GivenModelOfType(string typeName)
this.context.Object = Activator.CreateInstance(model);
}

[Given(@"model of type '(.*)'")]
public void GivenModelOfType(string typeName, Table table)
{
var modelType = Type.GetType(typeName, true);
this.context.Object = table.CreateInstance(modelType);
}

[Given(@"model has interest '(.*)'")]
public void GivenModelInterestsRDF(string value)
{
Expand All @@ -53,7 +61,7 @@ public void WhenTheObjectIsSerialized()
public void ThenTheResultingJsonLdShouldBe(string jObject)
{
var expected = JObject.Parse(jObject);
Assert.That(JToken.DeepEquals(this.context.JsonLdObject, expected), "Actual object was: {0}", this.context.JsonLdObject);
Assert.That(JToken.DeepEquals(this.context.JsonLdObject, expected), "Diff: {0}", new JsonDiffPatch().Diff(expected, this.context.JsonLdObject));
}
}
}
39 changes: 39 additions & 0 deletions src/JsonLD.Entities.Tests/Entities/AllPrimitives.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ReSharper disable InconsistentNaming
#pragma warning disable SA1300 // Element must begin with upper-case letter
using System;

namespace JsonLD.Entities.Tests.Entities
{
public class AllPrimitives
{
public string @string { get; set; }

public bool? @bool { get; set; }

public double? @double { get; set; }

public DateTime? date { get; set; }

public decimal? @decimal { get; set; }

public long? @long { get; set; }

public ulong? @ulong { get; set; }

public int? @int { get; set; }

public uint? @uint { get; set; }

public short? @short { get; set; }

public ushort? @ushort { get; set; }

public byte? @byte { get; set; }

public sbyte? @sbyte { get; set; }

public float? @float { get; set; }

public TimeSpan? timeSpan { get; set; }
}
}
2 changes: 0 additions & 2 deletions src/JsonLD.Entities.Tests/Entities/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ public class Person
public string Name { get; set; }

public string Surname { get; set; }

public DateTime BirthDate { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/JsonLD.Entities.Tests/JsonLD.Entities.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>DeserializingRDF.feature</DependentUpon>
</Compile>
<Compile Include="Entities\AllPrimitives.cs" />
<Compile Include="Entities\Book.cs" />
<Compile Include="Entities\ClassWithSomeUris.cs" />
<Compile Include="Entities\HasInterestsArray.cs" />
Expand All @@ -91,6 +92,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Serializing.feature</DependentUpon>
</Compile>
<Compile Include="SpecflowHelpers\TableExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down Expand Up @@ -202,6 +204,17 @@
</ItemGroup>
</When>
</Choose>
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.5'">
<ItemGroup>
<Reference Include="JsonDiffPatchDotNet">
<HintPath>..\..\packages\JsonDiffPatch.Net\lib\net45\JsonDiffPatchDotNet.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
</Choose>
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.0'">
<ItemGroup>
Expand Down
50 changes: 47 additions & 3 deletions src/JsonLD.Entities.Tests/Serializing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Scenario: Serialize simple model with blank id
"""
{
"name": "Tomasz",
"surname": "Pluskiewicz",
"birthDate": "1972-09-04T00:00:00"
"surname": "Pluskiewicz"
}
"""

Expand Down Expand Up @@ -69,4 +68,49 @@ Scenario: Serialize model with prefixed name in ClassAttribute
{
"@type": "ex:Person"
}
"""
"""

Scenario: Serializing primitive values should produce short literals for Boolean, Double and String
Given model of type 'JsonLD.Entities.Tests.Entities.AllPrimitives'
| Property | Value |
| string | Hello |
| double | 3.14 |
| bool | true |
When the object is serialized
Then the resulting JSON-LD should be:
"""
{
"string": "Hello",
"double": 3.14,
"bool": true
}
"""

Scenario Outline: Serializing primitive values should produce typed literals
Given model of type 'JsonLD.Entities.Tests.Entities.AllPrimitives'
| Property | Value |
| <Property> | <Value> |
When the object is serialized
Then the resulting JSON-LD should be:
"""
{
"<Property>": {
"@value": "<JsonValue>",
"@type": "http://www.w3.org/2001/XMLSchema#<XsdType>"
}
}
"""
Examples:
| Property | Value | JsonValue | XsdType |
| date | 2016-01-03 | 2016-01-03T00:00:00 | dateTime |
| decimal | 3.4 | 3.4 | decimal |
| long | 100 | 100 | long |
| ulong | 100 | 100 | unsignedLong |
| int | 1567 | 1567 | int |
| uint | 15 | 15 | unsignedInt |
| short | 17 | 17 | short |
| ushort | 3 | 3 | unsignedShort |
| byte | 20 | 20 | unsignedByte |
| sbyte | -3 | -3 | byte |
| float | 2.3456 | 2.3456 | float |
| timeSpan | 50.08:20:17 | P50DT8H20M17S | duration |
Loading