Java Code Examples for com.github.jsonldjava.utils.JsonUtils

The following examples show how to use com.github.jsonldjava.utils.JsonUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: egeria   Source File: CloudInformationModelParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Step through the content of a property group and extract the property descriptions.
 *
 * @param propertyGroupDirectory location of the property group content
 * @throws IOException file/directory not found - probably a logic error
 */
@SuppressWarnings("unchecked")
private void getPropertyGroupFromDirectory(File propertyGroupDirectory) throws IOException
{
    if (propertyGroupDirectory.isDirectory())
    {
        String propertyGroupDirectoryName = propertyGroupDirectory.toString();
        String propertyGroupName = propertyGroupDirectory.getName();
        System.out.println("Property group name: " + propertyGroupName);

        File conceptsJsonLDFile = new File(propertyGroupDirectoryName + CONCEPTS_FILE_NAME);

        LinkedHashMap<String, Object> conceptsJsonLD = (LinkedHashMap<String, Object>) JsonUtils.fromInputStream(new FileInputStream(conceptsJsonLDFile));

        if (modelLocation.equals(getStringValue(conceptsJsonLD.get(AT_CONTEXT_TAG))))
        {
            getPropertyGroup(conceptsJsonLD);
        }
    }
}
 
Example 2
Source Project: rdf4j   Source File: ConvertTest.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public final void testConvertWorkDir() throws IOException {
	setWorkingDir(cmd);

	File json = LOCATION.newFile("alien.jsonld");
	cmd.execute("convert", from.getName(), json.getName());

	assertTrue("File is empty", json.length() > 0);

	Object o = null;
	try {
		o = JsonUtils.fromInputStream(Files.newInputStream(json.toPath()));
	} catch (IOException ioe) {
		//
	}
	assertTrue("Invalid JSON", o != null);
}
 
Example 3
@Override
protected String getStringRepresentation(UserSecurityDetails obj, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = new HashMap<>();
    List<Map<String, Object>> groupMaps = obj.getGroups()
        .stream()
        .map(group -> {
            Map<String, Object> groupMap = new HashMap<>();
            groupMap.put("id", group.getId());
            groupMap.put("label", group.getLabel());

            return groupMap;
        })
        .collect(Collectors.toList());

    SecurityUser user = obj.getUser();
    jsonMap.put("id", user.getId());
    jsonMap.put("uid", user.getUid());
    jsonMap.put("groups", groupMaps);

    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 4
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotation oaAnnotation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 5
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OABatchOperation oaBatchOperation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaBatchOperation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 6
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationPage oaAnnotationPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 7
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationCollection oaAnnotationCollection, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationCollection.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 8
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAStatisticsPage oaStatisticsPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaStatisticsPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 9
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationHistory oaAnnotationHistory, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationHistory.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    JSONLDProfile.Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(JSONLDProfile.Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(JSONLDProfile.Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(JSONLDProfile.Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 10
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotation w3cAnnotation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 11
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CBatchOperation w3cBatchOperation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cBatchOperation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 12
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationPage w3cAnnotationPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 13
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationCollection w3cAnnotationCollection, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationCollection.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 14
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CStatisticsPage w3cStatisticsPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cStatisticsPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 15
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationHistory w3cAnnotationHistory, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationHistory.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    JSONLDProfile.Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(JSONLDProfile.Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(JSONLDProfile.Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(JSONLDProfile.Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 16
Source Project: elucidate-server   Source File: AnnotationExtractorServiceImpl.java    License: MIT License 6 votes vote down vote up
private void createAnnotationBodies(W3CAnnotation w3cAnnotation) throws IOException {
    int annotationPk = w3cAnnotation.getPk();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();

    LOGGER.info(String.format("Processing `body` values for W3CAnnotation [%s]", w3cAnnotation));
    List<AnnotationBody> annotationBodies = new AnnotationBodyExtractor().extractBodies(jsonMap);
    LOGGER.info(String.format("Got [%s] `body` values for W3CAnnotation [%s]", annotationBodies.size(), w3cAnnotation));
    for (AnnotationBody annotationBody : annotationBodies) {

        String bodyIri = annotationBody.getBodyIri();
        String sourceIri = annotationBody.getSourceIri();
        String bodyJson = JsonUtils.toString(annotationBody.getJsonMap());
        LOGGER.info(String.format("Preparing to create `body` with ID [%s], source IRI [%s] and JSON [%s]", bodyIri, sourceIri, bodyJson));
        annotationBody = annotationBodyStoreRepository.createAnnotationBody(annotationPk, bodyIri, sourceIri, bodyJson);
        LOGGER.info(String.format("Created AnnotationBody [%s] in repository", annotationBody));

        int bodyPk = annotationBody.getPk();
        Map<String, Object> bodyJsonMap = annotationBody.getJsonMap();
        createAnnotationSelectors(bodyPk, null, bodyJsonMap);
        createAnnotationCreators(null, bodyPk, null, bodyJsonMap);
        createAnnotationGenerators(null, bodyPk, null, bodyJsonMap);
        createAnnotationTemporals(null, bodyPk, null, bodyJsonMap);
    }
}
 
Example 17
Source Project: elucidate-server   Source File: AnnotationExtractorServiceImpl.java    License: MIT License 6 votes vote down vote up
private void createAnnotationTargets(W3CAnnotation w3cAnnotation) throws IOException {
    int annotationPk = w3cAnnotation.getPk();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();

    LOGGER.info(String.format("Processing `target` values for W3CAnnotation [%s]", w3cAnnotation));
    List<AnnotationTarget> annotationTargets = new AnnotationTargetExtractor().extractTargets(jsonMap);
    LOGGER.info(String.format("Got [%s] `target` values for W3CAnnotation [%s]", annotationTargets.size(), w3cAnnotation));
    for (AnnotationTarget annotationTarget : annotationTargets) {

        String targetIri = annotationTarget.getTargetIri();
        String sourceIri = annotationTarget.getSourceIri();
        String targetJson = JsonUtils.toString(annotationTarget.getJsonMap());
        LOGGER.info(String.format("Preparing to create `target` with ID [%s], source IRI [%s] and JSON [%s]", targetIri, sourceIri, targetJson));
        annotationTarget = annotationTargetStoreRepository.createAnnotationTarget(annotationPk, targetIri, sourceIri, targetJson);
        LOGGER.info(String.format("Created AnnotationTarget [%s] in repository", annotationTarget));

        int targetPk = annotationTarget.getPk();
        Map<String, Object> targetJsonMap = annotationTarget.getJsonMap();
        createAnnotationSelectors(null, targetPk, targetJsonMap);
        createAnnotationCreators(null, null, targetPk, targetJsonMap);
        createAnnotationGenerators(null, null, targetPk, targetJsonMap);
        createAnnotationTemporals(null, null, targetPk, targetJsonMap);
    }
}
 
Example 18
@Test
@SuppressWarnings("serial")
public void testCreateAnnotationCollection() throws Exception {

    W3CAnnotationCollection w3cAnnotationCollection = generateRandomW3CAnnotationCollection();

    String collectionId = w3cAnnotationCollection.getCollectionId();
    Map<String, Object> jsonMap = w3cAnnotationCollection.getJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    Object[] params = {collectionId, jsonStr};
    int[] sqlTypes = {Types.VARCHAR, Types.OTHER};
    when(jdbcTemplate.query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationCollectionRowMapper) any())).thenReturn(new ArrayList<W3CAnnotationCollection>() {
        {
            add(w3cAnnotationCollection);
        }
    });
    W3CAnnotationCollection returnedW3CAnnotationCollection = annotationCollectionStoreRepository.createAnnotationCollection(collectionId, jsonStr);
    verify(jdbcTemplate, times(1)).query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationCollectionRowMapper) any());

    assertThat(w3cAnnotationCollection, is(equalTo(returnedW3CAnnotationCollection)));
}
 
Example 19
@Test
@SuppressWarnings("serial")
public void testCreateAnnotation() throws Exception {

    W3CAnnotation w3cAnnotation = generateRandomW3CAnnotation();

    String collectionId = w3cAnnotation.getCollectionId();
    String annotationId = w3cAnnotation.getAnnotationId();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    Object[] params = {collectionId, annotationId, jsonStr, null};
    int[] sqlTypes = {Types.VARCHAR, Types.VARCHAR, Types.OTHER, Types.INTEGER};
    when(jdbcTemplate.query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any())).thenReturn(new ArrayList<W3CAnnotation>() {
        {
            add(w3cAnnotation);
        }
    });
    W3CAnnotation returnedW3CAnnotation = annotationStoreRepository.createAnnotation(collectionId, annotationId, jsonStr, null);
    verify(jdbcTemplate, times(1)).query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any());

    assertThat(w3cAnnotation, is(equalTo(returnedW3CAnnotation)));
}
 
Example 20
@Test
@SuppressWarnings("serial")
public void testUpdateAnnotation() throws Exception {

    W3CAnnotation w3cAnnotation = generateRandomW3CAnnotation();

    String collectionId = w3cAnnotation.getCollectionId();
    String annotationId = w3cAnnotation.getAnnotationId();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    Object[] params = {collectionId, annotationId, jsonStr, null};
    int[] sqlTypes = {Types.VARCHAR, Types.VARCHAR, Types.OTHER, Types.INTEGER};
    when(jdbcTemplate.query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any())).thenReturn(new ArrayList<W3CAnnotation>() {
        {
            add(w3cAnnotation);
        }
    });
    W3CAnnotation returnedW3CAnnotation = annotationStoreRepository.createAnnotation(collectionId, annotationId, jsonStr, null);
    verify(jdbcTemplate, times(1)).query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any());

    assertThat(w3cAnnotation, is(equalTo(returnedW3CAnnotation)));
}
 
Example 21
Source Project: elucidate-server   Source File: ResultSetUtils.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static Map<String, Object> getJsonMap(ResultSet rs, String columnName) throws SQLException {

    String jsonStr = getString(rs, columnName);
    if (StringUtils.isNotBlank(jsonStr)) {
        try {
            Object jsonObject = JsonUtils.fromString(jsonStr);
            if (jsonObject instanceof List) {
                return ((List<Map<String, Object>>) jsonObject).get(0);
            } else if (jsonObject instanceof Map) {
                return (Map<String, Object>) jsonObject;
            } else {
                throw new SQLException(String.format("Unexpected JSON Object type [%s]", jsonObject.getClass()));
            }
        } catch (IOException e) {
            throw new SQLException(String.format("Unable to parse JSON String [%s] into Map", jsonStr), e);
        }
    }

    return null;
}
 
Example 22
Source Project: robot   Source File: IOHelper.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Load a map of prefixes from the "@context" of a JSON-LD string.
 *
 * @param jsonString the JSON-LD string
 * @return a map from prefix name strings to prefix IRI strings
 * @throws IOException on any problem
 */
@SuppressWarnings("unchecked")
public static Context parseContext(String jsonString) throws IOException {
  try {
    Object jsonObject = JsonUtils.fromString(jsonString);
    if (!(jsonObject instanceof Map)) {
      throw new IOException(jsonldContextParseError);
    }
    Map<String, Object> jsonMap = (Map<String, Object>) jsonObject;
    if (!jsonMap.containsKey("@context")) {
      throw new IOException(jsonldContextParseError);
    }
    Object jsonContext = jsonMap.get("@context");
    return new Context().parse(jsonContext);
  } catch (Exception e) {
    throw new IOException(jsonldContextParseError, e);
  }
}
 
Example 23
Source Project: rdf4j   Source File: ConvertTest.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public final void testConvert() throws IOException {
	File json = LOCATION.newFile("alien.jsonld");
	cmd.execute("convert", from.getAbsolutePath(), json.getAbsolutePath());

	assertTrue("File is empty", json.length() > 0);

	Object o = null;
	try {
		o = JsonUtils.fromInputStream(Files.newInputStream(json.toPath()));
	} catch (IOException ioe) {
		//
	}
	assertTrue("Invalid JSON", o != null);
}
 
Example 24
@Test
public void triplesTest() throws JsonLdError, IOException {
	// String inputstring =
	// "{\"@id\":{\"@id\":\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/machine/DVC-1_8\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceElement\":\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceelement/DET-1_8\",\"http://igreen-projekt.de/ontologies/isoxml#deviceID\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"DVC-1\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceLocalizationLabel\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"FF000000406564\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceProcessData\":[\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/13_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/6_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/14_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/11_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/8_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/4_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/5_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/10_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/2_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/21_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/15_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/16_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/19_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/17_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/3_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/12_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/7_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/18_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/9_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/22_8\",\"http://pc-4107.kl.dfki.de:38080/onlinebox/resource/deviceprocessdata/20_8\"],\"http://igreen-projekt.de/ontologies/isoxml#deviceSerialNumber\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"12345\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceSoftwareVersion\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"01.009\"},\"http://igreen-projekt.de/ontologies/isoxml#deviceStructureLabel\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"31303030303030\"},\"http://igreen-projekt.de/ontologies/isoxml#workingSetMasterNAME\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"A000860020800001\"},\"http://www.w3.org/1999/02/22-rdf-syntax-ns#type\":{\"@iri\":\"http://www.agroxml.de/rdfs#Machine\"},\"http://www.w3.org/2000/01/rdf-schema#label\":{\"@datatype\":\"http://www.w3.org/2001/XMLSchema#string\",\"@literal\":\"Krone
	// Device\"}}";
	final String inputstring = "{ \"@id\":\"http://nonexistent.com/abox#Document1823812\", \"@type\":\"http://nonexistent.com/tbox#Document\" }";
	final String expectedString = "(http://nonexistent.com/abox#Document1823812, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://nonexistent.com/tbox#Document) [null]";
	final Object input = JsonUtils.fromString(inputstring);

	final Model graph = new LinkedHashModel();
	final ParseErrorCollector parseErrorListener = new ParseErrorCollector();
	final ParserConfig parserConfig = new ParserConfig();
	final JSONLDInternalTripleCallback callback = new JSONLDInternalTripleCallback(new StatementCollector(graph),
			SimpleValueFactory.getInstance(), parserConfig, parseErrorListener,
			nodeID -> SimpleValueFactory.getInstance().createBNode(nodeID),
			() -> SimpleValueFactory.getInstance().createBNode());

	JsonLdProcessor.toRDF(input, callback);

	final Iterator<Statement> statements = graph.iterator();

	// contains only one statement (type)
	while (statements.hasNext()) {
		final Statement stmt = statements.next();

		System.out.println(stmt.toString());
		assertEquals("Output was not as expected", stmt.toString(), expectedString);
	}

	assertEquals(0, parseErrorListener.getFatalErrors().size());
	assertEquals(0, parseErrorListener.getErrors().size());
	assertEquals(0, parseErrorListener.getWarnings().size());
}
 
Example 25
@Before 
public void createCard() throws JsonGenerationException, IOException {
 LinkedHashSet<Field> realAnswer = new LinkedHashSet<Field>();
 
 Field f1 = new Field("doctoral advisor", null);
 LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();
 values.put("http://dbpedia.org/resource/Alfred_Kleiner", "Alfred Kleiner");
 f1.setValues(values);
 realAnswer.add(f1);
 	 
 Field f2 = new Field("birth place", null , true);
 values = new LinkedHashMap<String, String>();
 values.put("http://dbpedia.org/resource/German_Empire", "German Empire");
 values.put("http://dbpedia.org/resource/Kingdom_of_Württemberg", "Kingdom of Württemberg");
 values.put("http://dbpedia.org/resource/Ulm", "Ulm");
 f2.setValues(values);
 realAnswer.add(f2);
 
 Field f3 = new Field("death date", "1955-04-18");
 f3.setValues(null);
 realAnswer.add(f3);
 
 Field f4 = new Field("birth date", "1879-03-14");
 f4.setValues(null);
 realAnswer.add(f4);
 		 
 Field f5 = new Field("death place", null);
 values = new LinkedHashMap<String, String>();
 values.put("http://dbpedia.org/resource/Princeton,_New_Jersey", "Princeton, New Jersey");
 f5.setValues(values);
 realAnswer.add(f5);
 
 realCard = JsonUtils.toPrettyString(realAnswer);
}
 
Example 26
@Override
protected String getStringRepresentation(SecurityUserReferenceCollection obj, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = new HashMap<>();
    List<Map<String, String>> userJsonMaps = obj.getUsers().stream()
        .map(user -> ImmutableMap.of(
            "uid", user.getUid(),
            "id", user.getId()
        ))
        .collect(Collectors.toList());

    jsonMap.put("users", userJsonMaps);
    return JsonUtils.toString(jsonMap);
}
 
Example 27
Source Project: elucidate-server   Source File: SecurityGroupMessageConverter.java    License: MIT License 5 votes vote down vote up
@Override
protected String getStringRepresentation(SecurityGroup obj, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("id", obj.getId());
    jsonMap.put("label", obj.getLabel());

    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 28
Source Project: elucidate-server   Source File: SecurityGroupMessageConverter.java    License: MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected SecurityGroup getObjectRepresentation(String str, MediaType contentType) throws Exception {
    Map<String, ?> jsonMap = (Map<String, ?>) JsonUtils.fromString(str);
    SecurityGroup group = new SecurityGroup();
    group.setId((String) jsonMap.get("id"));
    group.setLabel((String) jsonMap.get("label"));

    return group;
}
 
Example 29
@Override
protected String getStringRepresentation(AnnotationReferenceCollection obj, MediaType contentType)
    throws Exception {
    Map<String, Object> jsonMap = new HashMap<>();
    List<String> iris = obj.getAnnotations().stream()
        .map(ref -> iriBuilder.buildW3CAnnotationIri(ref.getCollectionId(), ref.getId()))
        .collect(Collectors.toList());

    jsonMap.put("annotations", iris);
    return JsonUtils.toString(jsonMap);
}
 
Example 30
@Override
@SuppressWarnings("unchecked")
protected OAAnnotation getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    OAAnnotation oaAnnotation = new OAAnnotation();
    oaAnnotation.setJsonMap(jsonMap);
    return oaAnnotation;
}