Skip to content

Commit

Permalink
Merge pull request #4 from rhysdh540/master
Browse files Browse the repository at this point in the history
make java 8 compatible
  • Loading branch information
Nolij authored May 22, 2024
2 parents c957061 + c71946f commit b2fec3c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ dependencies {
}
```
</details>
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
Expand Down Expand Up @@ -87,7 +89,49 @@ This prints out:
}
```

<!--- TODO: make a tutorial for serializing objects and the annotations that help in doing so --->
## 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<String, ZsonValue> 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

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static Map<String, ZsonValue> 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);
Expand Down Expand Up @@ -276,7 +276,7 @@ private static boolean shouldInclude(Field field, boolean forDeserialization) {
private static <T> void setField(Field field, Object object, Object value) {
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>) 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()) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/dev/nolij/zson/ZsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

}

0 comments on commit b2fec3c

Please sign in to comment.