RestUtils { public static JSONObject getObjectFromJsonld(String json) { JSONArray array = JSONArray.fromObject(json); JSONObject firstObject = Optional.ofNullable(array.optJSONObject(0)).orElse(new JSONObject()); if (firstObject.containsKey("@graph")) { firstObject = Optional.ofNullable(firstObject.getJSONArray("@graph").optJSONObject(0)) .orElse(new JSONObject()); } return firstObject; }  static RDFFormat getRDFFormat(String format); static String modelToString(Model model, RDFFormat format, SesameTransformer transformer); static String modelToString(Model model, String format, SesameTransformer transformer); static String modelToSkolemizedString(Model model, RDFFormat format, SesameTransformer transformer,
                                                 BNodeService service); static String modelToSkolemizedString(Model model, String format, SesameTransformer transformer,
                                                 BNodeService service); static String groupedModelToString(Model model, RDFFormat format, SesameTransformer transformer); static String groupedModelToString(Model model, String format, SesameTransformer transformer); static String groupedModelToSkolemizedString(Model model, RDFFormat format, SesameTransformer transformer,
                                                        BNodeService service); static String groupedModelToSkolemizedString(Model model, String format, SesameTransformer transformer,
                                                        BNodeService service); static Model jsonldToModel(String jsonld, SesameTransformer transformer); static Model jsonldToDeskolemizedModel(String jsonld, SesameTransformer transformer, BNodeService service); static String modelToJsonld(Model model, SesameTransformer transformer); static String modelToSkolemizedJsonld(Model model, SesameTransformer transformer, BNodeService service); static String modelToTrig(Model model, SesameTransformer transformer); static String getRDFFormatFileExtension(String format); static String getRDFFormatMimeType(String format); static User getActiveUser(ContainerRequestContext context, EngineManager engineManager); static String getActiveUsername(ContainerRequestContext context); static Optional<User> optActiveUser(ContainerRequestContext context, EngineManager engineManager); static Optional<String> optActiveUsername(ContainerRequestContext context); static void checkStringParam(@Nullable String param, String errorMessage); static JSONObject getObjectFromJsonld(String json); static ObjectNode getObjectNodeFromJsonld(String json); static JSONObject getTypedObjectFromJsonld(String json, String type); static JsonNode getTypedObjectNodeFromJsonld(String json, String type); static IRI createIRI(String requestId, ValueFactory vf); static JSONObject thingToJsonObject(Thing thing, String type, SesameTransformer transformer); static JsonNode thingToObjectNode(Thing thing, String type, SesameTransformer transformer); static JSONObject thingToSkolemizedJsonObject(Thing thing, String type, SesameTransformer transformer,
                                                         BNodeService bNodeService); static JsonNode thingToSkolemizedObjectNode(Thing thing, String type, SesameTransformer transformer,
                                                         BNodeService bNodeService); static Response createPaginatedThingResponse(UriInfo uriInfo, Set<T> things,
                                                                          IRI sortIRI, int offset, int limit,
                                                                          boolean asc,
                                                                          Function<T, Boolean> filterFunction,
                                                                          String type, SesameTransformer transformer,
                                                                          BNodeService bNodeService); static Response createPaginatedThingResponseJackson(UriInfo uriInfo, Set<T> things,
                                                                          IRI sortIRI, int offset, int limit,
                                                                          boolean asc,
                                                                          Function<T, Boolean> filterFunction,
                                                                          String type, SesameTransformer transformer,
                                                                          BNodeService bNodeService); static Response createPaginatedResponse(UriInfo uriInfo, Collection<T> items,
                                                                     int totalSize, int limit, int offset, String type,
                                                                     SesameTransformer transformer); static Response createPaginatedResponse(UriInfo uriInfo, Collection<T> items,
                                                                     int totalSize, int limit, int offset,
                                                                     String type, SesameTransformer transformer,
                                                                      BNodeService bNodeService); static Response createPaginatedResponseJackson(UriInfo uriInfo, Collection<T> items,
                                                                            int totalSize, int limit,
                                                                            int offset, String type,
                                                                            SesameTransformer transformer,
                                                                            BNodeService bNodeService); static Response createPaginatedResponseWithJson(UriInfo uriInfo, JSONArray items, int totalSize, int limit,
                                                           int offset); static Response createPaginatedResponseWithJsonNode(UriInfo uriInfo, ArrayNode items,
                                                               int totalSize, int limit, int offset); static void validatePaginationParams(String sortIRI, Set<String> sortResources, int limit, int offset);  }
@Test public void getObjectFromJsonldNoContextTest() { JSONObject expected = JSONObject.fromObject("{'@id': 'test'}"); String jsonld = "[" + expected.toString() + "]"; assertEquals(expected, RestUtils.getObjectFromJsonld(jsonld)); }
@Test public void getObjectFromJsonldWithContextTest() { JSONObject expected = JSONObject.fromObject("{'@id': 'test'}"); String jsonld = "[{'@graph':[" + expected.toString() + "]}]"; assertEquals(expected, RestUtils.getObjectFromJsonld(jsonld)); }
@Test public void getObjectFromJsonldThatDoesNotExistTest() { assertEquals(new JSONObject(), RestUtils.getObjectFromJsonld("[]")); assertEquals(new JSONObject(), RestUtils.getObjectFromJsonld("[{'@graph': []}]")); }