Search in sources :

Example 1 with JsonObject

use of com.grack.nanojson.JsonObject in project netbeans-mmd-plugin by raydac.

the class Mindmup2MindMapImporter method processLinks.

private void processLinks(@Nonnull final MindMap map, @Nonnull final JsonArray links, @Nonnull final Map<Long, Topic> topics) {
    for (int i = 0; i < links.size(); i++) {
        try {
            final JsonObject linkObject = links.getObject(i);
            final Topic fromTopic = topics.get(linkObject.getNumber("ideaIdFrom", Long.MIN_VALUE).longValue());
            final Topic toTopic = topics.get(linkObject.getNumber("ideaIdTo", Long.MIN_VALUE).longValue());
            if (fromTopic != null && toTopic != null) {
                fromTopic.setExtra(ExtraTopic.makeLinkTo(map, toTopic));
            }
        } catch (final Exception ex) {
            LOGGER.error("Can't parse link", ex);
        }
    }
}
Also used : JsonObject(com.grack.nanojson.JsonObject) URISyntaxException(java.net.URISyntaxException)

Example 2 with JsonObject

use of com.grack.nanojson.JsonObject in project netbeans-mmd-plugin by raydac.

the class Mindmup2MindMapImporter method doImport.

@Override
@Nullable
public MindMap doImport(@Nonnull final MindMapPanel panel, @Nonnull final DialogProvider dialogProvider, @Nullable final Topic actionTopic, @Nonnull @MustNotContainNull final Topic[] selectedTopics) throws Exception {
    final File file = this.selectFileForExtension(panel, Texts.getString("MMDImporters.Mindmup2MindMap.openDialogTitle"), "mup", "Mindmup files (.MUP)", Texts.getString("MMDImporters.ApproveImport"));
    if (file == null) {
        return null;
    }
    final JsonObject parsedJson;
    parsedJson = JsonParser.object().from(FileUtils.readFileToString(file, "UTF-8"));
    MindMap resultedMap = null;
    final Number formatVersion = parsedJson.getNumber("formatVersion");
    if (formatVersion == null) {
        dialogProvider.msgError(null, Texts.getString("MMDImporters.Mindmup2MindMap.Error.WrongFormat"));
    } else {
        resultedMap = new MindMap(null, true);
        resultedMap.setAttribute(MindMapPanel.ATTR_SHOW_JUMPS, "true");
        final Topic mindMapRoot = Assertions.assertNotNull(resultedMap.getRoot());
        final Map<Long, Topic> mapTopicId = new HashMap<Long, Topic>();
        parseTopic(resultedMap, null, mindMapRoot, parsedJson, mapTopicId);
        if (!mindMapRoot.getExtras().containsKey(Extra.ExtraType.FILE)) {
            mindMapRoot.setExtra(new ExtraFile(new MMapURI(null, file, null)));
        }
        final JsonArray links = parsedJson.getArray("links");
        if (links != null) {
            processLinks(resultedMap, links, mapTopicId);
        }
    }
    return resultedMap;
}
Also used : JsonArray(com.grack.nanojson.JsonArray) JsonObject(com.grack.nanojson.JsonObject) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 3 with JsonObject

use of com.grack.nanojson.JsonObject in project NewPipe by TeamNewPipe.

the class ImportExportJsonHelper method readFrom.

/**
 * Read a JSON source through the input stream and return the parsed subscription items.
 *
 * @param in            the input stream (e.g. a file)
 * @param eventListener listener for the events generated
 */
public static List<SubscriptionItem> readFrom(InputStream in, @Nullable ImportExportEventListener eventListener) throws InvalidSourceException {
    if (in == null)
        throw new InvalidSourceException("input is null");
    final List<SubscriptionItem> channels = new ArrayList<>();
    try {
        JsonObject parentObject = JsonParser.object().from(in);
        JsonArray channelsArray = parentObject.getArray(JSON_SUBSCRIPTIONS_ARRAY_KEY);
        if (eventListener != null)
            eventListener.onSizeReceived(channelsArray.size());
        if (channelsArray == null) {
            throw new InvalidSourceException("Channels array is null");
        }
        for (Object o : channelsArray) {
            if (o instanceof JsonObject) {
                JsonObject itemObject = (JsonObject) o;
                int serviceId = itemObject.getInt(JSON_SERVICE_ID_KEY, 0);
                String url = itemObject.getString(JSON_URL_KEY);
                String name = itemObject.getString(JSON_NAME_KEY);
                if (url != null && name != null && !url.isEmpty() && !name.isEmpty()) {
                    channels.add(new SubscriptionItem(serviceId, url, name));
                    if (eventListener != null)
                        eventListener.onItemCompleted(name);
                }
            }
        }
    } catch (Throwable e) {
        throw new InvalidSourceException("Couldn't parse json", e);
    }
    return channels;
}
Also used : InvalidSourceException(org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.InvalidSourceException) JsonArray(com.grack.nanojson.JsonArray) SubscriptionItem(org.schabi.newpipe.extractor.subscription.SubscriptionItem) ArrayList(java.util.ArrayList) JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject)

Example 4 with JsonObject

use of com.grack.nanojson.JsonObject in project netbeans-mmd-plugin by raydac.

the class Mindmup2MindMapImporter method parseTopic.

private void parseTopic(@Nonnull MindMap map, @Nullable final Topic parentTopic, @Nullable final Topic pregeneratedTopic, @Nonnull final JsonObject json, @Nonnull final Map<Long, Topic> idTopicMap) {
    final JsonObject ideas = json.getObject("ideas");
    if (ideas != null) {
        final List<OrderableIdea> sortedIdeas = new ArrayList<OrderableIdea>();
        for (final Map.Entry<String, Object> i : ideas.entrySet()) {
            if (i.getValue() instanceof JsonObject) {
                final JsonObject idea = (JsonObject) i.getValue();
                double order = 0.0d;
                try {
                    order = Double.parseDouble(i.getKey().trim());
                } catch (final NumberFormatException ex) {
                    LOGGER.error("Detected unexpected number format in order", ex);
                }
                sortedIdeas.add(new OrderableIdea(order, idea));
            }
        }
        Collections.sort(sortedIdeas);
        for (final OrderableIdea i : sortedIdeas) {
            final JsonObject ideaObject = i.getIdea();
            final String title = ideaObject.getString("title", "");
            final long id = ideaObject.getNumber("id", Long.MIN_VALUE).longValue();
            final Topic topicToProcess;
            if (pregeneratedTopic == null) {
                topicToProcess = Assertions.assertNotNull(parentTopic).makeChild(title.trim(), parentTopic);
                if (Assertions.assertNotNull(topicToProcess.getParent()).isRoot()) {
                    if (i.isLeftBranch()) {
                        AbstractCollapsableElement.makeTopicLeftSided(topicToProcess, true);
                        final Topic firstSibling = Assertions.assertNotNull(parentTopic).getFirst();
                        if (firstSibling != null && firstSibling != topicToProcess) {
                            topicToProcess.moveBefore(firstSibling);
                        }
                    }
                }
            } else {
                topicToProcess = pregeneratedTopic;
                topicToProcess.setText(title.trim());
            }
            if (id != Long.MIN_VALUE) {
                idTopicMap.put(id, topicToProcess);
            }
            final JsonObject attributes = ideaObject.getObject("attr");
            if (attributes != null) {
                for (final Map.Entry<String, Object> a : attributes.entrySet()) {
                    final String name = a.getKey();
                    final Object attrJson = a.getValue();
                    if ("note".equals(name)) {
                        processAttrNote((JsonObject) attrJson, topicToProcess);
                    } else if ("icon".equals(name)) {
                        processAttrIcon((JsonObject) attrJson, topicToProcess);
                    } else if ("style".equals(name)) {
                        processAttrStyle((JsonObject) attrJson, topicToProcess);
                    } else {
                        LOGGER.warn("Detected unsupported attribute '" + name + '\'');
                    }
                }
            }
            if (id >= 0L) {
                idTopicMap.put(id, topicToProcess);
            }
            parseTopic(map, topicToProcess, null, ideaObject, idTopicMap);
        }
    }
}
Also used : JsonObject(com.grack.nanojson.JsonObject) JsonObject(com.grack.nanojson.JsonObject)

Aggregations

JsonObject (com.grack.nanojson.JsonObject)4 JsonArray (com.grack.nanojson.JsonArray)2 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 InvalidSourceException (org.schabi.newpipe.extractor.subscription.SubscriptionExtractor.InvalidSourceException)1 SubscriptionItem (org.schabi.newpipe.extractor.subscription.SubscriptionItem)1