Format inline JSON objects by hand
This commit is contained in:
parent
e3999c9112
commit
f83f670bf7
|
@ -17,13 +17,27 @@ public class JacksonDeserializationUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenAJsonLdObject_whenCompactIsUsedWithEmptyContext_thenItCanBeDeserializedWithJackson() throws IOException {
|
void givenAJsonLdObject_whenCompactIsUsedWithEmptyContext_thenItCanBeDeserializedWithJackson() throws IOException {
|
||||||
String inputJsonLd = "{\"@context\":{\"@vocab\":\"http://schema.org/\",\"knows\":{\"@type\":\"@id\"}},\"@type\":\"Person\",\"@id\":\"http://example.com/person/1234\",\"name\":\"Example Name\",\"knows\":\"http://example.com/person/2345\"}";
|
String inputJsonLd = "{"
|
||||||
|
+ "\"@context\":{"
|
||||||
|
+ "\"@vocab\":\"http://schema.org/\","
|
||||||
|
+ "\"knows\":{\"@type\":\"@id\"}"
|
||||||
|
+ "},"
|
||||||
|
+ "\"@type\":\"Person\","
|
||||||
|
+ "\"@id\":\"http://example.com/person/1234\","
|
||||||
|
+ "\"name\":\"Example Name\","
|
||||||
|
+ "\"knows\":\"http://example.com/person/2345\""
|
||||||
|
+ "}";
|
||||||
|
|
||||||
Object jsonObject = JsonUtils.fromString(inputJsonLd);
|
Object jsonObject = JsonUtils.fromString(inputJsonLd);
|
||||||
Object compact = JsonLdProcessor.compact(jsonObject, new HashMap<>(), new JsonLdOptions());
|
Object compact = JsonLdProcessor.compact(jsonObject, new HashMap<>(), new JsonLdOptions());
|
||||||
String compactContent = JsonUtils.toString(compact);
|
String compactContent = JsonUtils.toString(compact);
|
||||||
|
|
||||||
assertEquals("{\"@id\":\"http://example.com/person/1234\",\"@type\":\"http://schema.org/Person\",\"http://schema.org/knows\":{\"@id\":\"http://example.com/person/2345\"},\"http://schema.org/name\":\"Example Name\"}", compactContent);
|
assertEquals("{"
|
||||||
|
+ "\"@id\":\"http://example.com/person/1234\","
|
||||||
|
+ "\"@type\":\"http://schema.org/Person\","
|
||||||
|
+ "\"http://schema.org/knows\":{\"@id\":\"http://example.com/person/2345\"},"
|
||||||
|
+ "\"http://schema.org/name\":\"Example Name\""
|
||||||
|
+ "}", compactContent);
|
||||||
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
Person person = objectMapper.readValue(compactContent, Person.class);
|
Person person = objectMapper.readValue(compactContent, Person.class);
|
||||||
|
|
|
@ -29,7 +29,15 @@ public class HydraJsonldSerializationUnitTest {
|
||||||
|
|
||||||
String personJsonLd = objectMapper.writeValueAsString(person);
|
String personJsonLd = objectMapper.writeValueAsString(person);
|
||||||
|
|
||||||
assertEquals("{\"@context\":{\"@vocab\":\"http://example.com/vocab/\",\"name\":\"fullName\"},\"@type\":\"person\",\"name\":\"Example Name\",\"@id\":\"http://example.com/person/1234\"}", personJsonLd);
|
assertEquals("{"
|
||||||
|
+ "\"@context\":{"
|
||||||
|
+ "\"@vocab\":\"http://example.com/vocab/\","
|
||||||
|
+ "\"name\":\"fullName\""
|
||||||
|
+ "},"
|
||||||
|
+ "\"@type\":\"person\","
|
||||||
|
+ "\"name\":\"Example Name\","
|
||||||
|
+ "\"@id\":\"http://example.com/person/1234\""
|
||||||
|
+ "}", personJsonLd);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SimpleModule getJacksonHydraSerializerModule() {
|
static SimpleModule getJacksonHydraSerializerModule() {
|
||||||
|
|
|
@ -18,8 +18,16 @@ public class JacksonJsonLdSerializationUnitTest {
|
||||||
Person person = new Person();
|
Person person = new Person();
|
||||||
String personJsonLd = objectMapper.writeValueAsString(person);
|
String personJsonLd = objectMapper.writeValueAsString(person);
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("{"
|
||||||
"{\"@type\":\"s:Person\",\"@context\":{\"s\":\"http://schema.org/\",\"name\":\"s:name\",\"knows\":{\"@id\":\"s:knows\",\"@type\":\"@id\"}},\"name\":\"Example Name\",\"@id\":\"http://example.com/person/1234\",\"knows\":\"http://example.com/person/2345\"}",
|
+ "\"@type\":\"s:Person\","
|
||||||
personJsonLd);
|
+ "\"@context\":{"
|
||||||
|
+ "\"s\":\"http://schema.org/\","
|
||||||
|
+ "\"name\":\"s:name\","
|
||||||
|
+ "\"knows\":{\"@id\":\"s:knows\",\"@type\":\"@id\"}"
|
||||||
|
+ "},"
|
||||||
|
+ "\"name\":\"Example Name\","
|
||||||
|
+ "\"@id\":\"http://example.com/person/1234\","
|
||||||
|
+ "\"knows\":\"http://example.com/person/2345\""
|
||||||
|
+ "}", personJsonLd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue