diff --git a/README.md b/README.md index 2c93682..506addb 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ dependencies { } ``` +Replace `version` with the version of the library you want to use. +You can find the latest version on the [releases page](https://github.com/Nolij/ZSON/releases). Then, you can use the library like so: ```java @@ -87,7 +89,49 @@ This prints out: } ``` - +## Serializing objects +ZSON can serialize objects to JSON using reflection. Here's an example: +```java +import dev.nolij.zson.Zson; +import dev.nolij.zson.Comment; +import dev.nolij.zson.Include; +import dev.nolij.zson.Exclude; +import dev.nolij.zson.ZsonValue; + +public class Example { + @Comment("This is a comment") + public String key = "value"; + + @Include + private int number = 4; + + @Exclude + public String excluded = "this won't be included"; + + public static void main(String[] args) { + Example example = new Example(); + Map zson = Zson.obj2map(example); + System.out.println(new ZsonWriter().stringify(zson)); + } +} +``` + +This prints out: +```json5 +{ + // This is a comment + "key": "value", + "number": 4, +} +``` + +Use the `@Comment` annotation to add a comment to a field, and the `@Include` and `@Exclude` annotations to include or exclude fields from serialization. +By default, all public fields are included, and all private fields are excluded. If they are annotated with `@Include`, static fields will be serialized but not deserialized. + +Also see the [tests](src/test/java/ZsonTest.java) for more examples. + +## Building +To build the project, run `./gradlew build` on Unix or `gradlew.bat build` on Windows. ## License diff --git a/settings.gradle.kts b/settings.gradle.kts index 06ccb6e..b9d9957 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -20,7 +20,7 @@ plugins { id("org.gradle.toolchains.foojay-resolver-convention") version("0.7.0") id("org.ajoberstar.grgit") version("5.2.2") apply(false) id("com.github.breadmoirai.github-release") version("2.4.1") apply(false) - id("xyz.wagyourtail.jvmdowngrader") version("0.3.0") apply(false) + id("xyz.wagyourtail.jvmdowngrader") version("0.4.1-SNAPSHOT") apply(false) } rootProject.name = "zson" diff --git a/src/main/java/dev/nolij/zson/Zson.java b/src/main/java/dev/nolij/zson/Zson.java index d0f1af2..069c1f9 100644 --- a/src/main/java/dev/nolij/zson/Zson.java +++ b/src/main/java/dev/nolij/zson/Zson.java @@ -206,7 +206,7 @@ public static Map obj2Map(@Nullable Object object) { Comment comment = field.getAnnotation(Comment.class); String commentValue = comment == null ? null : comment.value(); try { - boolean accessible = field.canAccess(Modifier.isStatic(field.getModifiers()) ? null : object); + boolean accessible = field.isAccessible(); if (!accessible) field.setAccessible(true); map.put(field.getName(), new ZsonValue(commentValue, field.get(object))); if (!accessible) field.setAccessible(false); @@ -276,7 +276,7 @@ private static boolean shouldInclude(Field field, boolean forDeserialization) { private static void setField(Field field, Object object, Object value) { @SuppressWarnings("unchecked") Class type = (Class) field.getType(); - boolean accessible = field.canAccess(Modifier.isStatic(field.getModifiers()) ? null : object); + boolean accessible = field.isAccessible(); if(!accessible) field.setAccessible(true); try { if(type.isPrimitive()) { diff --git a/src/main/java/dev/nolij/zson/ZsonWriter.java b/src/main/java/dev/nolij/zson/ZsonWriter.java index 2ed3588..0392d4c 100644 --- a/src/main/java/dev/nolij/zson/ZsonWriter.java +++ b/src/main/java/dev/nolij/zson/ZsonWriter.java @@ -16,11 +16,14 @@ @SuppressWarnings("UnstableApiUsage") public final class ZsonWriter { - public String indent = "\t"; - public boolean expandArrays = false; - public boolean quoteKeys = true; + public String indent; + public boolean expandArrays; + public boolean quoteKeys; public ZsonWriter() { + this.indent = "\t"; + this.expandArrays = false; + this.quoteKeys = true; } /** @@ -140,19 +143,21 @@ private String value(Object value) { throw new IllegalArgumentException("Unsupported value type: " + value.getClass().getName()); } + @Contract(value = "_ -> this", mutates = "this") public ZsonWriter withIndent(String indent) { this.indent = indent; return this; } + @Contract(value = "_ -> this", mutates = "this") public ZsonWriter withExpandArrays(boolean expandArrays) { this.expandArrays = expandArrays; return this; } + @Contract(value = "_ -> this", mutates = "this") public ZsonWriter withQuoteKeys(boolean quoteKeys) { this.quoteKeys = quoteKeys; return this; } - }