Skip to content

Commit

Permalink
Merge pull request #6 from rhysdh540/master
Browse files Browse the repository at this point in the history
merge everything into 3 classes
  • Loading branch information
Nolij authored Jul 4, 2024
2 parents a5dd296 + f54bee5 commit e1f6631
Show file tree
Hide file tree
Showing 9 changed files with 711 additions and 783 deletions.
57 changes: 27 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,35 @@ 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
import dev.nolij.zson.Zson; // contains static methods for parsing and writing JSON
import dev.nolij.zson.ZsonWriter; // contains methods for writing JSON
import dev.nolij.zson.ZsonParser; // contains static methods for parsing JSON
import dev.nolij.zson.Zson; // static helper/parsing methods, instantiate for a writer
import dev.nolij.zson.ZsonValue; // represents a JSON value with a comment
import java.util.Map;

import static dev.nolij.zson.Zson.*;

public class ZsonExample {
public static void main(String[] args) {
// Parse a JSON string
String json = "{\"key\": \"value\"}";
Map<String, ZsonValue> zson = ZsonParser.parseString(json);
System.out.println(zson.get("key")); // value

// Write a JSON string
ZsonWriter writer = new ZsonWriter().withIndent(" ").withExpandArrays(false);
public static void main(String[] args) {
// Parse a JSON string
String json = "{\"key\": \"value\"}";
Map<String, ZsonValue> zson = Zson.parseString(json);
System.out.println(zson.get("key")); // value

// Write a JSON string
Zson writer = new Zson().withIndent(" ").withExpandArrays(false);
Map<String, ZsonValue> map = object( // Zson.object()
entry("key", "comment", 4),
entry("arr", "look, arrays work too!", array(1, 2, 3)),
entry("obj", "and objects!", object(
entry("key", "value")
)),
entry("null", "comments can also\nbe miltiple lines", null)
);
entry("key", "comment", 4),
entry("arr", "look, arrays work too!", array(1, 2, 3)),
entry("obj", "and objects!", object(
entry("key", "value")
)),
entry("null", "comments can also\nbe multiple lines", null)
);
System.out.println(jsonString);
}
}
Expand Down Expand Up @@ -93,25 +92,23 @@ This prints out:
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.ZsonField;
import dev.nolij.zson.ZsonValue;

public class Example {
@Comment("This is a comment")
@ZsonField(comment = "This is a comment")
public String key = "value";

@Include
private int number = 4;
@ZsonField(include = true)
private int number = 4;

@Exclude
public String excluded = "this won't be included";
@ZsonField(exclude = true)
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));
Map<String, ZsonValue> zson = Zson.obj2map(example);
System.out.println(new Zson().stringify(zson));
}
}
```
Expand All @@ -125,8 +122,8 @@ This prints out:
}
```

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.
Use the `comment` parameter of the `@ZsonField` annotation to add a comment to a field, and the `include` and `exclude` parameters 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 `@ZsonField(include = true)`, static fields will be serialized but not deserialized.

Also see the [tests](src/test/java/ZsonTest.java) for more examples.

Expand Down
4 changes: 1 addition & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ org.gradle.caching = true
org.gradle.configuration-cache = false

maven_group = dev.nolij
project_version = 0.2
project_version = 0.3
project_name = zson

# Dependencies
# https://central.sonatype.com/artifact/com.pkware.jabel/jabel-javac-plugin
jabel_version = 1.0.1-1
# https://central.sonatype.com/artifact/org.jetbrains/annotations
jetbrains_annotations_version = 24.1.0
14 changes: 0 additions & 14 deletions src/main/java/dev/nolij/zson/Exclude.java

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/java/dev/nolij/zson/Include.java

This file was deleted.

Loading

0 comments on commit e1f6631

Please sign in to comment.