Search in sources :

Example 1 with JsonString

use of org.apache.jena.atlas.json.JsonString in project jena by apache.

the class TestJsonWriter method test.

private static void test(String str, String expected) {
    JsonValue v = new JsonString(str);
    str = v.toString();
    assertEquals(expected, str);
}
Also used : JsonValue(org.apache.jena.atlas.json.JsonValue) JsonString(org.apache.jena.atlas.json.JsonString)

Example 2 with JsonString

use of org.apache.jena.atlas.json.JsonString in project jena by apache.

the class TestJsonLDWriter method testSubstitutingContext.

/**
     * Test using a context to compute the output, and replacing the @context with a given value
     */
@Test
public void testSubstitutingContext() {
    Model m = ModelFactory.createDefaultModel();
    String ns = "http://schema.org/";
    Resource person = m.createResource(ns + "Person");
    Resource s = m.createResource();
    m.add(s, m.createProperty(ns + "name"), "Jane Doe");
    m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
    m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
    m.add(s, RDF.type, person);
    // change @context to a URI
    JsonLDWriteContext jenaCtx = new JsonLDWriteContext();
    jenaCtx.setJsonLDContextSubstitution((new JsonString(ns)).toString());
    String jsonld;
    jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
    String c = "\"@context\":\"http://schema.org/\"";
    assertTrue(jsonld.contains(c));
    // change @context to a given ctx
    String ctx = "{\"jobTitle\":{\"@id\":\"http://ex.com/jobTitle\"},\"url\":{\"@id\":\"http://ex.com/url\"},\"name\":{\"@id\":\"http://ex.com/name\"}}";
    jenaCtx.setJsonLDContextSubstitution(ctx);
    jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
    assertTrue(jsonld.contains("http://ex.com/name"));
}
Also used : JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) JsonString(org.apache.jena.atlas.json.JsonString) JsonString(org.apache.jena.atlas.json.JsonString) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Aggregations

JsonString (org.apache.jena.atlas.json.JsonString)2 JsonValue (org.apache.jena.atlas.json.JsonValue)1 BaseTest (org.apache.jena.atlas.junit.BaseTest)1 Model (org.apache.jena.rdf.model.Model)1 Resource (org.apache.jena.rdf.model.Resource)1 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)1 Test (org.junit.Test)1