summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/pcj/json/JsonCodec.java
blob: 65f4a5ee2f1451ae33f52656cb5a6427cdfdff9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package moe.nea.pcj.json;

import moe.nea.pcj.Codec;
import moe.nea.pcj.Result;

public interface JsonCodec<T, Format> extends Codec<
		T, Format, JsonLikeOperations<Format>,
		JsonLikeError, JsonLikeError> {

	default MapCodec<T, Format> fieldOf(String key) {
		return new MapCodec<>() {
			@Override
			public Result<T, JsonLikeError> decode(RecordView<Format> record, JsonLikeOperations<Format> ops) {
				return record.get(key)
				             .map(element -> Result.<T, JsonLikeError>cast(
						             JsonCodec.this.decode(element, ops)
						                           .mapError(it -> new AtField(key, it))))
				             .orElseGet(() -> Result.fail(new MissingKey(key)));
			}

			@Override
			public Result<RecordBuilder<Format>, JsonLikeError> encode(T value, JsonLikeOperations<Format> ops) {
				var record = ops.createObject();
				return Result.<Format, JsonLikeError>cast(JsonCodec.this.encode(value, ops))
				             .<JsonLikeError>mapError(it -> new AtField(key, it))
				             .flatMap(json -> Result.cast(record.add(key, json).map(unit -> record)));
			}

		};
	}
}