Ejemplo n.º 1
0
public void Can_deserialize_with_changed_context()
{
    // given
    var expanded = JObject.Parse(@"
    {
        '@id': 'http://t-code.pl/#tomasz',
        '@type': 'http://xmlns.com/foaf/0.1/Person',
        'http://xmlns.com/foaf/0.1/name': 'Tomasz',
        'http://xmlns.com/foaf/0.1/familyName': 'Pluskiewicz'
    }");

    var @context = JObject.Parse(@"
    {
        'foaf': 'http://xmlns.com/foaf/0.1/',
        'name': 'foaf:name',
        'lastName': 'foaf:familyName',
        'Person': 'foaf:Person'
    }");

    var contextProvider = new StaticContextProvider();
    contextProvider.SetContext(typeof(Person), @context);

    // when
    IEntitySerializer serializer = new EntitySerializer(contextProvider);
    var person = serializer.Deserialize<Person>(expanded);

    // then
    Assert.That(person.Name, Is.EqualTo("Tomasz"));
    Assert.That(person.LastName, Is.EqualTo("Pluskiewicz"));
    Assert.That(person.Id, Is.EqualTo(new Uri("http://t-code.pl/#tomasz")));
}
Ejemplo n.º 2
0
public void Can_serialize_class_instance_to_literal()
{
    // given
    var entity = new RequestLogItem { Ip = IPAddress.Parse("148.9.20.34") };
    var contextProvider = new StaticContextProvider();
    var context = JObject.Parse(@"
{
    'ip': {
        '@id': 'http://rdfs.org/sioc/ns#ip_address'
    }
}");
    contextProvider.SetContext(typeof(RequestLogItem), context);

    // when
    IEntitySerializer serializer = new EntitySerializer(contextProvider);
    dynamic jsonLd = serializer.Serialize(entity);

    // then
    Assert.That((string)jsonLd.ip, Is.EqualTo("148.9.20.34"));
}
Ejemplo n.º 3
0
public void Can_serialize_object_to_JSON_LD()
{
    // given
    var person = new Person
        {
            Id = new Uri("http://t-code.pl/#tomasz"),
            Name = "Tomasz",
            LastName = "Pluskiewicz"
        };
    var @context = JObject.Parse("{ '@context': 'http://example.org/context/Person' }");

    var contextProvider = new StaticContextProvider();
    contextProvider.SetContext(typeof(Person), @context);

    // when
    IEntitySerializer serializer = new EntitySerializer(contextProvider);
    dynamic json = serializer.Serialize(person);

    // then
    Assert.That((string)json.name, Is.EqualTo("Tomasz"));
    Assert.That((string)json.lastName, Is.EqualTo("Pluskiewicz"));
    Assert.That((string)json["@id"], Is.EqualTo("http://t-code.pl/#tomasz"));
    Assert.That((string)json["@type"][0], Is.EqualTo("http://xmlns.com/foaf/0.1/Person"));
    Assert.That(json["@context"], Is.Not.Null);
}