Search in sources :

Example 1 with JsonArray

use of com.grack.nanojson.JsonArray 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 2 with JsonArray

use of com.grack.nanojson.JsonArray 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)

Aggregations

JsonArray (com.grack.nanojson.JsonArray)2 JsonObject (com.grack.nanojson.JsonObject)2 File (java.io.File)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