Format inline JSON objects by hand

This commit is contained in:
mdhtr 2020-07-26 09:44:17 +02:00
parent e3999c9112
commit f83f670bf7
3 changed files with 36 additions and 6 deletions

View File

@ -17,13 +17,27 @@ public class JacksonDeserializationUnitTest {
@Test
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 compact = JsonLdProcessor.compact(jsonObject, new HashMap<>(), new JsonLdOptions());
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();
Person person = objectMapper.readValue(compactContent, Person.class);

View File

@ -29,7 +29,15 @@ public class HydraJsonldSerializationUnitTest {
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() {

View File

@ -18,8 +18,16 @@ public class JacksonJsonLdSerializationUnitTest {
Person person = new Person();
String personJsonLd = objectMapper.writeValueAsString(person);
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\"}",
personJsonLd);
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\""
+ "}", personJsonLd);
}
}