Search in sources :

Example 6 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class Validator method validateType.

private void validateType(IJSONNode node, Member member, JSONValidationInfo valinfo) {
    if (IJSONSchemaNode.TYPE.equals(member.getName())) {
        Set<String> types = new HashSet<String>();
        if (member.getValue().isString()) {
            types.add(member.getValue().asString());
        } else if (member.getValue().isArray()) {
            JsonArray array = (JsonArray) member.getValue();
            for (JsonValue item : array) {
                types.add(item.asString());
            }
        }
        boolean valid = false;
        for (String type : types) {
            if (node.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
                valid = true;
                break;
            }
            if (node.getNodeType() == IJSONNode.PAIR_NODE) {
                IJSONValue value = ((IJSONPair) node).getValue();
                if (value == null && JSONSchemaType.Null.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value == null) {
                    valid = false;
                    break;
                }
                if (value.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && JSONSchemaType.String.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.ARRAY_NODE && JSONSchemaType.Array.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_BOOLEAN_NODE && JSONSchemaType.Boolean.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NULL_NODE && JSONSchemaType.Null.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Number.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Integer.getName().equals(type)) {
                    valid = true;
                    break;
                }
            }
        }
        if (!valid) {
            int offset = node.getStartOffset();
            int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
            StringBuffer buffer = new StringBuffer();
            Iterator<String> iter = types.iterator();
            buffer.append(OPEN_BRACKET);
            while (iter.hasNext()) {
                buffer.append(iter.next());
                if (iter.hasNext()) {
                    buffer.append(COMMA);
                }
            }
            buffer.append(CLOSE_BRACKET);
            valinfo.addMessage("Incorrect type. Expected " + buffer.toString(), line, 0, offset == 0 ? 1 : offset);
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) HashSet(java.util.HashSet)

Example 7 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class JSONSchemaNode method getType.

public static JSONSchemaType[] getType(JsonValue value) {
    if (value == null) {
        return JSONSchemaType.EMPTY_TYPES;
    }
    JSONSchemaType t = null;
    List<JSONSchemaType> types = new ArrayList<JSONSchemaType>();
    if (value.isString()) {
        t = JSONSchemaType.getType(value.asString());
        if (t != null) {
            types.add(t);
        }
    } else if (value.isArray()) {
        JsonArray array = (JsonArray) value;
        for (JsonValue item : array) {
            t = JSONSchemaType.getType(item.asString());
            if (t != null) {
                types.add(t);
            }
        }
    }
    return types.toArray(JSONSchemaType.EMPTY_TYPES);
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JSONSchemaType(org.eclipse.json.schema.JSONSchemaType)

Example 8 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class JSONSchemaNode method resolveReferences.

private void resolveReferences(JsonObject parent, String name, JsonValue value) {
    if (value instanceof JsonObject) {
        JsonObject json = value.asObject();
        String ref = json.getString(REF, null);
        if (ref != null && ref.startsWith(DEFINITIONS)) {
            String r = ref.substring(DEFINITIONS.length());
            JsonValue v = definitions.get(r);
            parent.set(name, v);
        // json.remove(REF);
        } else {
            Iterator<Member> members = json.iterator();
            while (members.hasNext()) {
                Member member = members.next();
                JsonValue v = member.getValue();
                resolveReferences(json, member.getName(), v);
            }
        }
    } else if (value instanceof JsonArray) {
        JsonArray jsonArray = (JsonArray) value;
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonValue item = jsonArray.get(i);
            if (item instanceof JsonObject) {
                JsonObject json = item.asObject();
                String ref = json.getString(REF, null);
                if (ref != null && ref.startsWith(DEFINITIONS)) {
                    String r = ref.substring(DEFINITIONS.length());
                    JsonValue v = definitions.get(r);
                    jsonArray.set(i, v);
                } else {
                    resolveReferences(json);
                }
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)

Example 9 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class BowerDependencyCompletionProposalCollector method addProposals.

protected void addProposals(JsonValue json, ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
    if (json.isArray()) {
        // Ex :
        // [{"name":"angular-mocks","url":"git://github.com/angular/bower-angular-mocks.git"},
        // {"name":"angular-moment","url":"git://github.com/urish/angular-moment.git"}]
        String dependency = null;
        String replacementString = null;
        JsonArray values = (JsonArray) json;
        for (JsonValue value : values) {
            if (value.isObject()) {
                dependency = ((JsonObject) value).get("name").asString();
                replacementString = ContentAssistHelper.getRequiredName(dependency, JSONSchemaType.String);
                Image icon = BowerEditorPluginImageHelper.getInstance().getImage(BowerEditorPluginImages.IMG_OBJ_BOWER);
                JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, dependency, null, null, JSONRelevanceConstants.R_OBJECT_KEY);
                contentAssistRequest.addProposal(proposal);
            }
        }
    }
// System.err.println(json);
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) Image(org.eclipse.swt.graphics.Image)

Example 10 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class NPMDependencyCompletionProposalCollector method addProposals.

protected void addProposals(JsonValue json, ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
    if (json.isArray()) {
        // Ex :
        // [{"value":"express"},{"value":"lodash"},{"value":"gulp"}]
        String dependency = null;
        String replacementString = null;
        JsonArray values = (JsonArray) json;
        for (JsonValue value : values) {
            if (value.isObject()) {
                dependency = ((JsonObject) value).get("value").asString();
                replacementString = ContentAssistHelper.getRequiredName(dependency, JSONSchemaType.String);
                Image icon = NPMEditorPluginImageHelper.getInstance().getImage(NPMEditorPluginImages.IMG_OBJ_NPM);
                JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, dependency, null, null, JSONRelevanceConstants.R_OBJECT_KEY);
                contentAssistRequest.addProposal(proposal);
            }
        }
    }
// System.err.println(json);
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) Image(org.eclipse.swt.graphics.Image)

Aggregations

JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)10 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)10 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)5 HashSet (java.util.HashSet)3 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)3 JSONSchemaType (org.eclipse.json.schema.JSONSchemaType)2 Image (org.eclipse.swt.graphics.Image)2 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)2 JSONKeyCompletionProposal (org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 JSONValidationInfo (org.eclipse.wst.json.core.internal.validation.JSONValidationInfo)1 ICatalogElement (org.eclipse.wst.json.core.schema.catalog.ICatalogElement)1 ICatalogEntry (org.eclipse.wst.json.core.schema.catalog.ICatalogEntry)1