Last active
September 2, 2021 14:39
-
-
Save joshcangit/5be3e87832e7e3d78367f6c546e5eec1 to your computer and use it in GitHub Desktop.
Example on using Jankson for reading and writing to a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usr/bin/env jbang "$0" "$@";exit $? | |
//DEPS blue.endless:jankson:1.2.1 | |
import java.io.IOException; | |
import java.io.Writer; | |
import java.io.BufferedInputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.charset.Charset; | |
import static java.nio.file.StandardOpenOption.CREATE; | |
import static java.nio.file.StandardOpenOption.WRITE; | |
import blue.endless.jankson.Jankson; | |
import blue.endless.jankson.Comment; | |
import blue.endless.jankson.JsonGrammar; | |
import blue.endless.jankson.JsonElement; | |
import blue.endless.jankson.JsonObject; | |
class ConfigObject { | |
transient int bar = 8192; //transient fields are not serialized | |
public static String bux = "a"; //static fields are not serialized | |
//the Comment annotation is picked up by jankson.Comment and added to the json in toJson() | |
@Comment("This is a comment") | |
private int foo = 42; //private fields ARE serialized. | |
private String word = "example"; | |
private float value = 0.125f; | |
private boolean useSomething = true; | |
} | |
public class JanksonExample { | |
public static Charset charset = java.nio.charset.StandardCharsets.UTF_8; | |
public static final JsonGrammar HJSON = JsonGrammar.builder() | |
.withComments(true) | |
.printCommas(false) | |
.bareSpecialNumerics(true) | |
.printUnquotedKeys(true) | |
.build(); | |
public static void main(String[] args) throws IOException { | |
Path dir = Paths.get("testdir"); | |
Path filePath = dir.resolve(Paths.get("config.hjson")).normalize(); | |
if(!Files.exists(dir)) { | |
Files.createDirectories(dir); | |
} | |
Jankson jankson = Jankson.builder().build(); | |
if(!Files.exists(filePath) || Files.size(filePath) == 0) { | |
JsonElement element = jankson.toJson(new ConfigObject()); | |
try(Writer writer = Files.newBufferedWriter(filePath, charset, CREATE, WRITE)) { | |
element.toJson(writer, HJSON, 0); | |
writer.flush(); writer.close(); | |
} catch(Exception e) { | |
e.getStackTrace(); | |
} | |
} | |
try(BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(filePath))) { | |
JsonObject hjson = jankson.load(bis); | |
int numRes = hjson.getInt("foo", 1); | |
String wordRes = hjson.get(String.class, "word"); | |
float flRes = hjson.getFloat("value", 0.5f); | |
boolean boolRes = hjson.getBoolean("useSomething", false); | |
String confRes = ConfigObject.bux; | |
System.out.println(numRes); | |
System.out.println(wordRes); | |
System.out.println(flRes); | |
System.out.println(boolRes); | |
System.out.println(confRes); | |
bis.close(); | |
} catch(Exception e) { | |
e.getStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment