Search in sources :

Example 1 with JsonLocation

use of com.fasterxml.jackson.core.JsonLocation in project knime-core by knime.

the class SubnodeLayoutJSONEditorPage method isJSONValid.

/**
 * @return true, if current JSON layout structure is valid
 */
protected boolean isJSONValid() {
    ObjectMapper mapper = JSONLayoutPage.getConfiguredObjectMapper();
    ObjectReader reader = mapper.readerForUpdating(new JSONLayoutPage());
    try {
        String json = isWindows() ? m_textArea.getText() : m_jsonDocument;
        JSONLayoutPage page = reader.readValue(json);
        m_documentNodeIDs.clear();
        if (page.getRows() != null) {
            for (JSONLayoutRow row : page.getRows()) {
                populateDocumentNodeIDs(row);
            }
            compareNodeIDs();
        }
        return true;
    } catch (IOException | NumberFormatException e) {
        String errorMessage;
        if (e instanceof JsonProcessingException) {
            JsonProcessingException jsonException = (JsonProcessingException) e;
            Throwable cause = null;
            Throwable newCause = jsonException.getCause();
            while (newCause instanceof JsonProcessingException) {
                if (cause == newCause) {
                    break;
                }
                cause = newCause;
                newCause = cause.getCause();
            }
            if (cause instanceof JsonProcessingException) {
                jsonException = (JsonProcessingException) cause;
            }
            errorMessage = jsonException.getOriginalMessage().split("\n")[0];
            JsonLocation location = jsonException.getLocation();
            if (location != null) {
                errorMessage += " at line: " + (location.getLineNr() + 1) + " column: " + location.getColumnNr();
            }
        } else {
            String message = e.getMessage();
            errorMessage = message;
        }
        if (m_statusLine != null && !m_statusLine.isDisposed()) {
            m_statusLine.setForeground(new Color(Display.getCurrent(), 255, 0, 0));
            m_statusLine.setText(errorMessage);
            int textWidth = isWindows() ? m_textArea.getSize().width : m_text.getSize().x;
            Point newSize = m_statusLine.computeSize(textWidth, m_statusLine.getSize().y, true);
            m_statusLine.setSize(newSize);
        }
    }
    return false;
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation) JSONLayoutPage(org.knime.js.core.layout.bs.JSONLayoutPage) Color(org.eclipse.swt.graphics.Color) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) JSONLayoutRow(org.knime.js.core.layout.bs.JSONLayoutRow) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JsonLocation

use of com.fasterxml.jackson.core.JsonLocation in project immutables by immutables.

the class JsonParserReader method getLocationString.

private String getLocationString() {
    JsonLocation l = parser.getCurrentLocation();
    List<String> parts = new ArrayList<>(4);
    parts.add("line: " + l.getLineNr());
    parts.add("column: " + l.getColumnNr());
    if (l.getByteOffset() >= 0) {
        parts.add("byte offset: " + l.getByteOffset());
    }
    return parts.toString();
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation) ArrayList(java.util.ArrayList)

Example 3 with JsonLocation

use of com.fasterxml.jackson.core.JsonLocation in project dropbox-sdk-java by dropbox.

the class DbxEntry method _read.

/**
 * @return
 *     {@code null} if the entry is an 'is_deleted' entry.
 */
private static <C> /*@Nullable*/
WithChildrenC<C> _read(JsonParser parser, /*@Nullable*/
Collector<DbxEntry, ? extends C> collector, boolean allowDeleted) throws IOException, JsonReadException {
    JsonLocation top = JsonReader.expectObjectStart(parser);
    String size = null;
    long bytes = -1;
    String path = null;
    Boolean is_dir = null;
    Boolean is_deleted = null;
    String rev = null;
    Boolean thumb_exists = null;
    String icon = null;
    Date modified = null;
    Date client_mtime = null;
    String hash = null;
    C contents = null;
    File.PhotoInfo photo_info = null;
    File.VideoInfo video_info = null;
    while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
        String fieldName = parser.getCurrentName();
        JsonReader.nextToken(parser);
        int fi = FM.get(fieldName);
        try {
            switch(fi) {
                case -1:
                    JsonReader.skipValue(parser);
                    break;
                case FM_size:
                    size = JsonReader.StringReader.readField(parser, fieldName, size);
                    break;
                case FM_bytes:
                    bytes = JsonReader.readUnsignedLongField(parser, fieldName, bytes);
                    break;
                case FM_path:
                    path = JsonReader.StringReader.readField(parser, fieldName, path);
                    break;
                case FM_is_dir:
                    is_dir = JsonReader.BooleanReader.readField(parser, fieldName, is_dir);
                    break;
                case FM_is_deleted:
                    is_deleted = JsonReader.BooleanReader.readField(parser, fieldName, is_deleted);
                    break;
                case FM_rev:
                    rev = JsonReader.StringReader.readField(parser, fieldName, rev);
                    break;
                case FM_thumb_exists:
                    thumb_exists = JsonReader.BooleanReader.readField(parser, fieldName, thumb_exists);
                    break;
                case FM_icon:
                    icon = JsonReader.StringReader.readField(parser, fieldName, icon);
                    break;
                case FM_modified:
                    modified = JsonDateReader.Dropbox.readField(parser, fieldName, modified);
                    break;
                case FM_client_mtime:
                    client_mtime = JsonDateReader.Dropbox.readField(parser, fieldName, client_mtime);
                    break;
                case FM_hash:
                    if (collector == null)
                        throw new JsonReadException("not expecting \"hash\" field, since we didn't ask for children", parser.getCurrentLocation());
                    hash = JsonReader.StringReader.readField(parser, fieldName, hash);
                    break;
                case FM_contents:
                    if (collector == null)
                        throw new JsonReadException("not expecting \"contents\" field, since we didn't ask for children", parser.getCurrentLocation());
                    contents = JsonArrayReader.mk(Reader, collector).readField(parser, fieldName, contents);
                    break;
                case FM_photo_info:
                    photo_info = PendingReader.mk(File.PhotoInfo.Reader, File.PhotoInfo.PENDING).readField(parser, fieldName, photo_info);
                    break;
                case FM_video_info:
                    video_info = PendingReader.mk(File.VideoInfo.Reader, File.VideoInfo.PENDING).readField(parser, fieldName, video_info);
                    break;
                default:
                    throw new AssertionError("bad index: " + fi + ", field = \"" + fieldName + "\"");
            }
        } catch (JsonReadException ex) {
            throw ex.addFieldContext(fieldName);
        }
    }
    JsonReader.expectObjectEnd(parser);
    if (path == null)
        throw new JsonReadException("missing field \"path\"", top);
    if (icon == null)
        throw new JsonReadException("missing field \"icon\"", top);
    if (is_deleted == null)
        is_deleted = Boolean.FALSE;
    if (is_dir == null)
        is_dir = Boolean.FALSE;
    if (thumb_exists == null)
        thumb_exists = Boolean.FALSE;
    if (is_dir && (contents != null || hash != null)) {
        if (hash == null)
            throw new JsonReadException("missing \"hash\", when we asked for children", top);
        if (contents == null)
            throw new JsonReadException("missing \"contents\", when we asked for children", top);
    }
    DbxEntry e;
    if (is_dir) {
        e = new Folder(path, icon, thumb_exists);
    } else {
        // Normal File
        if (size == null)
            throw new JsonReadException("missing \"size\" for a file entry", top);
        if (bytes == -1)
            throw new JsonReadException("missing \"bytes\" for a file entry", top);
        if (modified == null)
            throw new JsonReadException("missing \"modified\" for a file entry", top);
        if (client_mtime == null)
            throw new JsonReadException("missing \"client_mtime\" for a file entry", top);
        if (rev == null)
            throw new JsonReadException("missing \"rev\" for a file entry", top);
        e = new File(path, icon, thumb_exists, bytes, size, modified, client_mtime, rev, photo_info, video_info);
    }
    if (is_deleted) {
        if (allowDeleted) {
            return null;
        } else {
            throw new JsonReadException("not expecting \"is_deleted\" entry here", top);
        }
    }
    return new WithChildrenC<C>(e, hash, contents);
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation) Date(java.util.Date)

Example 4 with JsonLocation

use of com.fasterxml.jackson.core.JsonLocation in project dropbox-sdk-java by dropbox.

the class JsonReader method expectArrayEnd.

public static JsonLocation expectArrayEnd(JsonParser parser) throws IOException, JsonReadException {
    if (parser.getCurrentToken() != JsonToken.END_ARRAY) {
        throw new JsonReadException("expecting the end of an array (\"[\")", parser.getTokenLocation());
    }
    JsonLocation loc = parser.getTokenLocation();
    nextToken(parser);
    return loc;
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation)

Example 5 with JsonLocation

use of com.fasterxml.jackson.core.JsonLocation in project dropbox-sdk-java by dropbox.

the class JsonReader method expectArrayStart.

public static JsonLocation expectArrayStart(JsonParser parser) throws IOException, JsonReadException {
    if (parser.getCurrentToken() != JsonToken.START_ARRAY) {
        throw new JsonReadException("expecting the start of an array (\"[\")", parser.getTokenLocation());
    }
    JsonLocation loc = parser.getTokenLocation();
    nextToken(parser);
    return loc;
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation)

Aggregations

JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Color (org.eclipse.swt.graphics.Color)1 Point (org.eclipse.swt.graphics.Point)1 JSONLayoutPage (org.knime.js.core.layout.bs.JSONLayoutPage)1 JSONLayoutRow (org.knime.js.core.layout.bs.JSONLayoutRow)1