Skip to content

Commit

Permalink
2020-10-09T19:18
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-shimizu committed Oct 9, 2020
1 parent 78c6610 commit 614fbcd
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 24 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# json4java8

## Introduction

This library is JSON ([RFC8259](https://tools.ietf.org/html/rfc8259)) parser implementation on Java8.

## Example of use

```
```java
public class POJO {

public int num;
Expand Down Expand Up @@ -39,7 +40,7 @@ public class POJO {

### From POJO (Plain Old Java Objec) to JSON

```
```java
/* to String */
String json = JsonHub.fromPojo(pojo).toJson();

Expand All @@ -48,21 +49,22 @@ Writer writer = new StringWriter()
JsonHub.fromPojo(pojo).toJson(writer);

/* to file */
Path path = Paths.get("path_of_file.json");
Path path = Paths.get("path/of/file.json");
JsonHub.fromPojo(pojo).writeFile(path);
```

#### From POJO conditions

- Field is `public`
- Field is not `static`
- Field is *not* `static`

See also ["/src/examples/example02/PojoParseToJsonString.java"](/src/examples/example02/PojoParseToJsonString.java)
See also ["/src/examples/example03/PojoWriteJsonToFile.java"](/src/examples/example03/PojoWriteJsonToFile.java)


### From JSON to POJO

```
```java
/* from JSON String */
String json = "{\"num\": 100, \"str\": \"STRING\", \"bool\": true}";
Pojo pojo = JsonHub.fromJson(json).toPojo(Pojo.class);
Expand All @@ -72,22 +74,23 @@ Reader reader = new StringReader(json);
Pojo pojo = JsonHub.fromJson(reader).toPojo(Pojo.class);

/* from file */
Path path = Paths.get("path_of_file.json");
Path path = Paths.get("path/of/file.json");
Pojo pojo = JsonHub.fromFile(path).toPojo(Pojo.class);
```

#### To POJO conditions

- Class has `public new()` (arguments is 0)
- Field is `public`
- Field is not `static`
- Field is not `final`
- Field is *not* `static`
- Field is *not* `final`

See also ["/src/examples/example01/JsonStringParseToPojo.java"](/src/examples/example01/JsonStringParseToPojo.java)
See also ["/src/examples/example04/ReadJsonFileParseToPojo.java"](/src/examples/example04/ReadJsonFileParseToPojo.java)

### From POJO to UTF-8 bytes

```
```java
/* to bytes */
byte[] bytes = JsonHub.fromPojo(pojo).getBytes();

Expand All @@ -98,7 +101,7 @@ JsonHub.fromPojo(pojo).writeBytes(strm);

### From UTF-8 bytes to POJO

```
```java
/* from bytes */
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
Pojo pojo = JsonHub.fromBytes(bytes).toPojo(Pojo.class);
Expand All @@ -113,12 +116,12 @@ Pojo pojo = JsonHub.fromBytes(strm).toPojo(Pojo.class);
1. Create JsonHub instance from `#fromPojo`, ...
1. PrettyPrint using `#prettyPrint`

```
```java
/* to String */
String prettyPrintJson = JsonHub.fromPoso(pojo).prettyPrint();

/* write to file */
Path path = Paths.get("path_of_file.json");
Path path = Paths.get("path/of/file.json");
JsonHub.fromPojo(pojo).prettyPrint(path);

/* write to Writer */
Expand All @@ -132,7 +135,7 @@ See also ["/src/examples/example09/ChangePrettyPrintFormat.java"](/src/examples/

## Get value from JsonHub instance

```
```java
String json
= "{ "
+ " \"num\": 100, "
Expand All @@ -151,7 +154,6 @@ String array_0 = jh.get("array").get(0).toString(); /* "a" */

See also ["/src/examples/example08/ForEachJsonHub.java"](/src/examples/example08/ForEachJsonHub.java)


### Methods for seek value in OBJECT or ARRAY

✓ is available.
Expand Down Expand Up @@ -209,12 +211,11 @@ See also ["/src/examples/example08/ForEachJsonHub.java"](/src/examples/example08
|isNull() |||||||||
|nonNull() |||||||||


## Create JsonHub instance by builder

Use JsonHubBuilder

```
```java
JsonHubBuilder jhb = JsonHub.getBuilder();

JsonHub jsonHub = jhb.object(
Expand All @@ -236,4 +237,3 @@ System.out.println(json);
```

See also ["/src/examples/example07/CreateJsonStringByBuilder.java"](/src/examples/example07/CreateJsonStringByBuilder.java)

6 changes: 3 additions & 3 deletions src/main/java/com/shimizukenta/jsonhub/JsonHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public interface JsonHub extends Iterable<JsonHub> {
public JsonHub getOrDefault(CharSequence name, JsonHub defaultValue);

/**
* Returns JsonHub instance, seek deeply in Object by names, If seek failed, return {@code null}.
* Returns JsonHub instance, seek chains in Object by names, If seek failed, return {@code null}.
*
* <p>
* Available if type is OBJECT-chains
Expand All @@ -253,7 +253,7 @@ public interface JsonHub extends Iterable<JsonHub> {
* Returns length if type is STRING or OBJECT or ARRAY.
*
* <p>
* Available if STRING or ARRAY or OBJECT
* Available if type is STRING or ARRAY or OBJECT
* </p>
* <p>
* If type is STRING return length of String.<br />
Expand All @@ -270,7 +270,7 @@ public interface JsonHub extends Iterable<JsonHub> {
* Returns {@code true} if empty.
*
* <p>
* Available if STRING or ARRAY or OBJECT.<br />
* Available if type is STRING or ARRAY or OBJECT.<br />
* <p>
* <p>
* If type is STRING, return {@code true} if length is 0.<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* This class is parser, from POJO (Plain-Old-Java-Object) to JsonHub instance.
*
* <p>
* To get instance, {@link #getInstance()}.<br />
* To get parser instance, {@link #getInstance()}.<br />
* To parse, {@link #parse(Object)}.<br />
* </p>
* <p>
Expand Down Expand Up @@ -66,6 +66,7 @@ public static JsonHubFromPojoParser getInstance() {
*
* @param pojo (Plain-Old-Java-Object)
* @return AbstractJsonHub instance
* @throws JsonHubParseException if parse failed
*/
public AbstractJsonHub parse(Object pojo) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public boolean lineSeparateAfterValueSeparator() {
/**
* Setter of Line-Separate in blank ARRAY or OBJECT.
*
* @param f set true if line-separate in blank ARRAY or OBJECT
* @param f set {@code true} if line-separate in blank ARRAY or OBJECT
*/
public void lineSeparateIfBlank(boolean f) {
synchronized ( this ) {
Expand All @@ -300,7 +300,7 @@ public void lineSeparateIfBlank(boolean f) {
/**
* Line-Separate in blank ARRAY or OBJECT getter.
*
* @return true if line-separate in blank ARRAY or OBJECT
* @return {@code true} if line-separate in blank ARRAY or OBJECT
*/
public boolean lineSeparateIfBlank() {
synchronized ( this ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* This class is parser, from JsonHub instance to POJO(Plain-Old-Java-Object).
*
* <p>
* To get instance, {@link #getInstance()}.<br />
* To get parser instance, {@link #getInstance()}.<br />
* To parse, {@link #parse(JsonHub, Class)}.<br />
* </p>
* <p>
Expand Down Expand Up @@ -68,6 +68,7 @@ public static JsonHubToPojoParser getInstance() {
* @param jh JsonHub instance
* @param classOfT
* @return parsed POJO
* @throws JsonHubParseException if parse failed
*/
public <T> T parse(JsonHub jh, Class<T> classOfT) {

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/shimizukenta/jsonhub/JsonHubType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.shimizukenta.jsonhub;

/**
* JSON value types.
*
* @author kenta-shimizu
*
*/
public enum JsonHubType {

NULL,
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/shimizukenta/jsonhub/JsonLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import java.util.Objects;

/**
* JSON Literals.
*
* @author kenta-shimizu
*
*/
public enum JsonLiteral {

NULL("null"),
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/shimizukenta/jsonhub/JsonStringCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* This class is JSON-String to escape or unescape.
*
* @author kenta-shimizu
*
*/
public class JsonStringCoder {

private JsonStringCoder() {
Expand All @@ -14,6 +20,15 @@ private static class SingletonHolder {
private static final JsonStringCoder inst = new JsonStringCoder();
}

/**
* Returns coder instance.
*
* <p>
* This class is Singleton-pattern.
* </p>
*
* @return coder instance
*/
public static JsonStringCoder getInstance() {
return SingletonHolder.inst;
}
Expand Down Expand Up @@ -65,6 +80,16 @@ public static Byte unescape(byte b) {
}
}

/**
* Retruns escaped JSON-String.
*
* <p>
* Not Accept {@code null}.<br />
* </p>
*
* @param cs unescaped-JSON-Stirng
* @return escaped-JSON-String
*/
public String escape(CharSequence cs) {

String v = cs.toString();
Expand Down Expand Up @@ -97,6 +122,16 @@ public String escape(CharSequence cs) {
}
}

/**
* Returns unescaped JSON-String.
*
* <p>
* Not Accept {@code null}.<br />
* </p>
*
* @param cs escaped-JSON-String
* @return unescaped-JSON-String
*/
public String unescape(CharSequence cs) {

String v = cs.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import java.util.Objects;

/**
* JSON structural characters.
*
* @author kenta-shimizu
*
*/
public enum JsonStructuralChar {

ESCAPE("\\"),
Expand Down

0 comments on commit 614fbcd

Please sign in to comment.