-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[json-node] Add NodeMapper API for reading/writing options using all …
…options like InputStream, OutputStream etc
- Loading branch information
Showing
7 changed files
with
387 additions
and
12 deletions.
There are no files selected for viewing
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
78 changes: 78 additions & 0 deletions
78
json-node/src/main/java/io/avaje/json/node/NodeMapper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.avaje.json.node; | ||
|
||
import io.avaje.json.JsonReader; | ||
import io.avaje.json.JsonWriter; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
|
||
/** | ||
* Mappers for JsonNode, JsonArray and JsonObject. | ||
* <p> | ||
* This supports more options for reading and writing json content | ||
* such as InputStream, OutputStream, Reader, Writer etc. | ||
* | ||
* @see JsonNodeMapper#arrayMapper() | ||
* @see JsonNodeMapper#objectMapper() | ||
* @see JsonNodeMapper#nodeMapper() | ||
*/ | ||
public interface NodeMapper<T extends JsonNode> { | ||
|
||
/** | ||
* Read the return the value from the json content. | ||
*/ | ||
T fromJson(String content); | ||
|
||
/** | ||
* Read the return the value from the reader. | ||
*/ | ||
T fromJson(JsonReader reader); | ||
|
||
/** | ||
* Read the return the value from the json content. | ||
*/ | ||
T fromJson(byte[] content); | ||
|
||
/** | ||
* Read the return the value from the reader. | ||
*/ | ||
T fromJson(Reader reader); | ||
|
||
/** | ||
* Read the return the value from the inputStream. | ||
*/ | ||
T fromJson(InputStream inputStream); | ||
|
||
/** | ||
* Return as json string. | ||
*/ | ||
String toJson(T value); | ||
|
||
/** | ||
* Return as json string in pretty format. | ||
*/ | ||
String toJsonPretty(T value); | ||
|
||
/** | ||
* Return the value as json content in bytes form. | ||
*/ | ||
byte[] toJsonBytes(T value); | ||
|
||
/** | ||
* Write to the given writer. | ||
*/ | ||
void toJson(T value, JsonWriter writer); | ||
|
||
/** | ||
* Write to the given writer. | ||
*/ | ||
void toJson(T value, Writer writer); | ||
|
||
/** | ||
* Write to the given outputStream. | ||
*/ | ||
void toJson(T value, OutputStream outputStream); | ||
|
||
} |
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
116 changes: 116 additions & 0 deletions
116
json-node/src/main/java/io/avaje/json/node/adapter/DMapper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package io.avaje.json.node.adapter; | ||
|
||
import io.avaje.json.JsonAdapter; | ||
import io.avaje.json.JsonException; | ||
import io.avaje.json.JsonReader; | ||
import io.avaje.json.JsonWriter; | ||
import io.avaje.json.node.JsonNode; | ||
import io.avaje.json.node.NodeMapper; | ||
import io.avaje.json.stream.BufferedJsonWriter; | ||
import io.avaje.json.stream.BytesJsonWriter; | ||
import io.avaje.json.stream.JsonStream; | ||
|
||
import java.io.*; | ||
|
||
final class DMapper<T extends JsonNode> implements NodeMapper<T> { | ||
|
||
private final JsonAdapter<T> adapter; | ||
private final JsonStream jsonStream; | ||
|
||
DMapper(JsonAdapter<T> adapter, JsonStream jsonStream) { | ||
this.adapter = adapter; | ||
this.jsonStream = jsonStream; | ||
} | ||
|
||
@Override | ||
public T fromJson(JsonReader reader) { | ||
return adapter.fromJson(reader); | ||
} | ||
|
||
@Override | ||
public T fromJson(String content) { | ||
try (JsonReader reader = jsonStream.reader(content)) { | ||
return adapter.fromJson(reader); | ||
} | ||
} | ||
|
||
@Override | ||
public T fromJson(byte[] content) { | ||
try (JsonReader reader = jsonStream.reader(content)) { | ||
return adapter.fromJson(reader); | ||
} | ||
} | ||
|
||
@Override | ||
public T fromJson(Reader content) { | ||
try (JsonReader reader = jsonStream.reader(content)) { | ||
return adapter.fromJson(reader); | ||
} | ||
} | ||
|
||
@Override | ||
public T fromJson(InputStream content) { | ||
try (JsonReader reader = jsonStream.reader(content)) { | ||
return adapter.fromJson(reader); | ||
} | ||
} | ||
|
||
@Override | ||
public String toJson(T value) { | ||
try (BufferedJsonWriter writer = jsonStream.bufferedWriter()) { | ||
toJson(value, writer); | ||
return writer.result(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toJsonPretty(T value) { | ||
try (BufferedJsonWriter writer = jsonStream.bufferedWriter()) { | ||
writer.pretty(true); | ||
toJson(value, writer); | ||
return writer.result(); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] toJsonBytes(T value) { | ||
try (BytesJsonWriter writer = jsonStream.bufferedWriterAsBytes()) { | ||
toJson(value, writer); | ||
return writer.result(); | ||
} | ||
} | ||
|
||
@Override | ||
public void toJson(T value, JsonWriter writer) { | ||
try { | ||
adapter.toJson(writer, value); | ||
} catch (RuntimeException e) { | ||
writer.markIncomplete(); | ||
throw new JsonException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void toJson(T value, Writer writer) { | ||
try (JsonWriter jsonWriter = jsonStream.writer(writer)) { | ||
toJson(value, jsonWriter); | ||
} | ||
} | ||
|
||
@Override | ||
public void toJson(T value, OutputStream outputStream) { | ||
try (JsonWriter writer = jsonStream.writer(outputStream)) { | ||
toJson(value, writer); | ||
} | ||
close(outputStream); | ||
} | ||
|
||
private void close(Closeable outputStream) { | ||
try { | ||
outputStream.close(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Error closing stream", e); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.