Search in sources :

Example 6 with JsonValue

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

the class Validator method validateItems.

private void validateItems(IJSONNode node, JsonObject schema, IJSONValue value, JsonValue memberValue, JSONValidationInfo valinfo) {
    JsonValue additionalItems = schema.get(IJSONSchemaNode.ADDITIONAL_ITEMS);
    if (additionalItems != null && additionalItems.isBoolean() && !additionalItems.asBoolean()) {
        if (memberValue != null && memberValue.isArray()) {
            if (value instanceof IJSONArray) {
                int instanceSize = getSize((IJSONArray) value);
                int size = memberValue.asArray().size();
                if (instanceSize > size) {
                    int offset = node.getStartOffset();
                    int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
                    valinfo.addMessage("Array has too many items. Expected " + size + " or fewer", line, 0, offset == 0 ? 1 : offset);
                }
            }
        }
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)

Example 7 with JsonValue

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue 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 8 with JsonValue

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

the class HttpHelper method makeRequest.

public static JsonValue makeRequest(String url) throws IOException {
    long startTime = 0;
    HttpClient httpClient = new DefaultHttpClient();
    try {
        // Post JSON Tern doc
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        InputStream in = entity.getContent();
        // Check the status
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
        // node.js server throws error
        /*
				 * String message = IOUtils.toString(in); if
				 * (StringUtils.isEmpty(message)) { throw new
				 * TernException(statusLine.toString()); } throw new
				 * TernException(message);
				 */
        }
        try {
            JsonValue response = JsonValue.readFrom(new InputStreamReader(in));
            return response;
        } catch (ParseException e) {
            throw new IOException(e);
        }
    } catch (Exception e) {
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new IOException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) ParseException(org.eclipse.json.provisonnal.com.eclipsesource.json.ParseException) StatusLine(org.apache.http.StatusLine) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) ParseException(org.eclipse.json.provisonnal.com.eclipsesource.json.ParseException)

Example 9 with JsonValue

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue 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 10 with JsonValue

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

the class JSONSchemaNode method addDefinitions.

private void addDefinitions() {
    // $NON-NLS-1$
    JsonValue defs = jsonObject.get("definitions");
    if (defs instanceof JsonObject) {
        Iterator<Member> members = ((JsonObject) defs).iterator();
        while (members.hasNext()) {
            Member member = members.next();
            JsonValue value = member.getValue();
            if (value instanceof JsonObject) {
                definitions.put(member.getName(), member.getValue());
            }
        }
    }
}
Also used : JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)

Aggregations

JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)16 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)10 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)7 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)3 InputStreamReader (java.io.InputStreamReader)2 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 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1