-
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.
Merge pull request #47 from bowbahdoe/cleanup-march-1
Add json Encoder and bump version
- Loading branch information
Showing
5 changed files
with
182 additions
and
3 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
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
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
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,54 @@ | ||
package dev.mccue.json; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* Object that knows how to encode an object into Json. | ||
* | ||
* <p> | ||
* This is a counterpart to {@link JsonEncodable} which can be provided | ||
* to encode objects in contexts where elements are not, for whatever reason, | ||
* have an intrinsic Json representation. | ||
* </p> | ||
* @param <T> The type of element to encode. | ||
*/ | ||
public interface JsonEncoder<T> { | ||
/** | ||
* Encodes the given value to Json. | ||
* @param value The value to encode. | ||
* @return The encoded value. | ||
*/ | ||
Json encode(T value); | ||
|
||
|
||
/** | ||
* Creates a {@link JsonEncoder} which delegates to an intrinsic implementation | ||
* provided by virtue of being {@link JsonEncodable}. | ||
* @return A {@link JsonEncoder}. | ||
* @param <T> The type to encode. | ||
*/ | ||
static <T extends JsonEncodable> JsonEncoder<T> of() { | ||
return JsonEncodable::toJson; | ||
} | ||
|
||
/** | ||
* Convenience method to target a lambda expression to a {@link JsonEncoder} | ||
* and be able to call methods such as {@link JsonEncoder#map(Function)}. | ||
* @param encoder The expression to wrap as an encoder. | ||
* @return A {@link JsonEncoder}. | ||
* @param <T> The type to encode. | ||
*/ | ||
static <T> JsonEncoder<T> of(JsonEncoder<? super T> encoder) { | ||
return encoder::encode; | ||
} | ||
|
||
/** | ||
* Maps the result of this encoder. | ||
* @param f The function to apply. | ||
* @return An encoder that encodes a different type. | ||
* @param <R> The type the returned encoder will encode. | ||
*/ | ||
default <R> JsonEncoder<R> map(Function<? super R, ? extends T> f) { | ||
return r -> this.encode(f.apply(r)); | ||
} | ||
} |
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