29 lines
1.3 KiB
Java
29 lines
1.3 KiB
Java
package io.gitlab.jfronny.muscript.gson;
|
|
|
|
import io.gitlab.jfronny.commons.serialize.gson.api.v2.GsonHolders;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal;
|
|
|
|
public class GsonLib {
|
|
public static Scope addTo(Scope scope) {
|
|
return scope
|
|
.set("toJson", GsonLib::toJson)
|
|
.set("fromJson", GsonLib::fromJson);
|
|
}
|
|
|
|
public static DString toJson(DList args) {
|
|
if (args.isEmpty() || args.size() > 2) throw new IllegalArgumentException("Invalid number of arguments for toJson: expected 1 or 2 but got " + args.size());
|
|
Dynamic source = args.get(0);
|
|
boolean lenient = args.size() > 1 && args.get(1).asBool().getValue();
|
|
return DFinal.of((lenient ? GsonHolders.CONFIG : GsonHolders.API).getGson().toJson(source));
|
|
}
|
|
|
|
public static Dynamic fromJson(DList args) {
|
|
if (args.isEmpty() || args.size() > 2) throw new IllegalArgumentException("Invalid number of arguments for fromJson: expected 1 or 2 but got " + args.size());
|
|
String source = args.get(0).asString().getValue();
|
|
boolean lenient = args.size() > 1 && args.get(1).asBool().getValue();
|
|
return (lenient ? GsonHolders.CONFIG : GsonHolders.API).getGson().fromJson(source, Dynamic.class);
|
|
}
|
|
}
|