Search in sources :

Example 1 with GsonDocument

use of net.anweisen.utilities.common.config.document.GsonDocument in project Utility by anweisen.

the class Document method readJsonArrayFile.

@Nonnull
@CheckReturnValue
static List<Document> readJsonArrayFile(@Nonnull Path file) {
    try {
        JsonArray array = GsonDocument.GSON.fromJson(FileUtils.newBufferedReader(file), JsonArray.class);
        if (array == null)
            return new ArrayList<>();
        List<Document> documents = new ArrayList<>(array.size());
        array.forEach(element -> documents.add(new GsonDocument(element.getAsJsonObject())));
        return documents;
    } catch (IOException ex) {
        throw new WrappedException(ex);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) WrappedException(net.anweisen.utilities.common.collection.WrappedException) ArrayList(java.util.ArrayList) GsonDocument(net.anweisen.utilities.common.config.document.GsonDocument) PropertiesDocument(net.anweisen.utilities.common.config.document.PropertiesDocument) EmptyDocument(net.anweisen.utilities.common.config.document.EmptyDocument) GsonDocument(net.anweisen.utilities.common.config.document.GsonDocument) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 2 with GsonDocument

use of net.anweisen.utilities.common.config.document.GsonDocument in project Utility by anweisen.

the class DocumentTypeAdapter method write.

@Override
public void write(@Nonnull Gson gson, @Nonnull JsonWriter writer, @Nonnull Document document) throws IOException {
    if (document instanceof GsonDocument) {
        GsonDocument gsonDocument = (GsonDocument) document;
        TypeAdapters.JSON_ELEMENT.write(writer, gsonDocument.getJsonObject());
        return;
    }
    Document copiedDocument = document.copyJson();
    if (copiedDocument instanceof GsonDocument) {
        GsonDocument gsonDocument = (GsonDocument) copiedDocument;
        TypeAdapters.JSON_ELEMENT.write(writer, gsonDocument.getJsonObject());
        return;
    }
    GsonDocument gsonDocument = new GsonDocument(document.values());
    TypeAdapters.JSON_ELEMENT.write(writer, gsonDocument.getJsonObject());
}
Also used : GsonDocument(net.anweisen.utilities.common.config.document.GsonDocument) Document(net.anweisen.utilities.common.config.Document) GsonDocument(net.anweisen.utilities.common.config.document.GsonDocument)

Aggregations

GsonDocument (net.anweisen.utilities.common.config.document.GsonDocument)2 JsonArray (com.google.gson.JsonArray)1 ArrayList (java.util.ArrayList)1 CheckReturnValue (javax.annotation.CheckReturnValue)1 Nonnull (javax.annotation.Nonnull)1 WrappedException (net.anweisen.utilities.common.collection.WrappedException)1 Document (net.anweisen.utilities.common.config.Document)1 EmptyDocument (net.anweisen.utilities.common.config.document.EmptyDocument)1 PropertiesDocument (net.anweisen.utilities.common.config.document.PropertiesDocument)1