use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.
the class ProjectDescriptionLoader method copy.
private JSONValue copy(JsonElement jsonElement) {
if (jsonElement.isJsonNull()) {
return JSONFactory.eINSTANCE.createJSONNullLiteral();
}
if (jsonElement.isJsonPrimitive()) {
JsonPrimitive primitive = jsonElement.getAsJsonPrimitive();
if (primitive.isBoolean()) {
JSONBooleanLiteral result = JSONFactory.eINSTANCE.createJSONBooleanLiteral();
result.setBooleanValue(primitive.getAsBoolean());
return result;
}
if (primitive.isNumber()) {
JSONNumericLiteral result = JSONFactory.eINSTANCE.createJSONNumericLiteral();
result.setValue(primitive.getAsBigDecimal());
return result;
}
if (primitive.isString()) {
JSONStringLiteral result = JSONFactory.eINSTANCE.createJSONStringLiteral();
result.setValue(primitive.getAsString());
return result;
}
throw new IllegalArgumentException(jsonElement.toString());
}
if (jsonElement.isJsonObject()) {
JsonObject object = jsonElement.getAsJsonObject();
JSONObject result = JSONFactory.eINSTANCE.createJSONObject();
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
NameValuePair pair = JSONFactory.eINSTANCE.createNameValuePair();
pair.setName(entry.getKey());
pair.setValue(copy(entry.getValue()));
result.getNameValuePairs().add(pair);
}
return result;
}
if (jsonElement.isJsonArray()) {
JsonArray array = jsonElement.getAsJsonArray();
JSONArray result = JSONFactory.eINSTANCE.createJSONArray();
for (JsonElement arrayElement : array) {
result.getElements().add(copy(arrayElement));
}
return result;
}
throw new IllegalArgumentException(jsonElement.toString());
}
use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.
the class PackageJsonValidatorExtension method getModuleFilterInformation.
/**
* Validates the structure of the given {@code value} as module filter specifier and returns the information that
* can be extracted from it.
*
* Returns the module filter specifier information of {@code value} in terms of a
* {@link ValidationModuleFilterSpecifier}.
*
* Returns {@code null} if the given {@code value} is not a valid representation of a module filter specifier.
*
* Similar to {@link PackageJsonUtils#asModuleFilterSpecifierOrNull(JSONValue)} but also validates the structure
* along the way.
*/
private ValidationModuleFilterSpecifier getModuleFilterInformation(JSONValue value, ModuleFilterType type) {
// 1st variant:
if (value instanceof JSONStringLiteral) {
return new ValidationModuleFilterSpecifier(((JSONStringLiteral) value).getValue(), null, type, value);
}
// 2nd variant:
if (value instanceof JSONObject) {
final List<NameValuePair> pairs = ((JSONObject) value).getNameValuePairs();
final NameValuePair sourceContainerPair = pairs.stream().filter(p -> NV_SOURCE_CONTAINER.name.equals(p.getName())).findFirst().orElse(null);
final NameValuePair moduleFilterPair = pairs.stream().filter(p -> NV_MODULE.name.equals(p.getName())).findFirst().orElse(null);
// make sure the pairs are of correct type (or null in case of sourceContainerPair)
if ((sourceContainerPair == null || checkIsType(sourceContainerPair.getValue(), JSONPackage.Literals.JSON_STRING_LITERAL)) && (moduleFilterPair != null && checkIsType(moduleFilterPair.getValue(), JSONPackage.Literals.JSON_STRING_LITERAL))) {
final String sourceContainer = sourceContainerPair != null ? ((JSONStringLiteral) sourceContainerPair.getValue()).getValue() : null;
final String moduleFilter = ((JSONStringLiteral) moduleFilterPair.getValue()).getValue();
return new ValidationModuleFilterSpecifier(moduleFilter, sourceContainer, type, value);
}
}
// otherwise 'value' does not represent a valid module filter specifier
addIssue(IssueCodes.getMessageForPKGJ_INVALID_MODULE_FILTER_SPECIFIER(), value, IssueCodes.PKGJ_INVALID_MODULE_FILTER_SPECIFIER);
return null;
}
use of org.eclipse.n4js.json.JSON.JSONObject in project n4js by eclipse.
the class PackageJsonValidatorExtension method doGetSourceContainers.
/**
* Validates the correct structure of a {@link PackageJsonProperties#SOURCES} section and returns a map between the
* declared source container types and corresponding {@link JSONStringLiteral}s that specify the various source
* container paths.
*/
private Multimap<SourceContainerType, List<JSONStringLiteral>> doGetSourceContainers() {
final Collection<JSONValue> sourcesValues = getDocumentValues(SOURCES);
// first check whether n4js.sources section has been defined at all
if (sourcesValues.isEmpty()) {
// return an empty map
return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
}
// first check type of all occuring 'sources' sections
if (!checkIsType(sourcesValues, JSONPackage.Literals.JSON_OBJECT, "as source container section")) {
// return an empty map
return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
}
// only consider the first n4js.sources section for further validation (in case of duplicates)
final JSONValue sourcesValue = sourcesValues.iterator().next();
final JSONObject sourceContainerObject = (JSONObject) sourcesValue;
final Multimap<SourceContainerType, List<JSONStringLiteral>> sourceContainerValues = HashMultimap.create();
for (NameValuePair pair : sourceContainerObject.getNameValuePairs()) {
final String sourceContainerType = pair.getName();
// compute type of source container sub-section
final SourceContainerType containerType = PackageJsonUtils.parseSourceContainerType(pair.getName());
// check that sourceContainerType represents a valid source container type
if (containerType == null) {
addIssue(IssueCodes.getMessageForPKGJ_INVALID_SOURCE_CONTAINER_TYPE(sourceContainerType), pair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_INVALID_SOURCE_CONTAINER_TYPE);
continue;
}
// check type of RHS (list of source paths)
if (!checkIsType(pair.getValue(), JSONPackage.Literals.JSON_ARRAY, "as source container list")) {
continue;
}
final JSONArray sourceContainerSpecifiers = (JSONArray) pair.getValue();
// collect all source container paths in this list
final List<JSONStringLiteral> specifierLiterals = new ArrayList<>();
for (JSONValue specifier : sourceContainerSpecifiers.getElements()) {
if (!checkIsType(specifier, JSONPackage.Literals.JSON_STRING_LITERAL, "as source container specifier")) {
continue;
}
specifierLiterals.add((JSONStringLiteral) specifier);
}
// This may override a value in case of a duplicate containerType (e.g. two external sections).
// However, this will also issue an appropriate warning for a duplicate key and
// is therefore not handled here.
sourceContainerValues.put(containerType, specifierLiterals);
}
return sourceContainerValues;
}
Aggregations