summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/pcj/json/JsonCodec.java
blob: 6195bee042b57779fb9f9fe5f32198c7ca4d6423 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package moe.nea.pcj.json;

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

import java.util.ArrayList;
import java.util.List;

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

	default JsonCodec<List<T>, Format> listOf() {
		return new JsonCodec<>() {
			@Override
			public Result<Format, JsonLikeError> encode(List<T> data, JsonLikeOperations<Format> ops) {
				var list = ops.createList();
				var erros = new ArrayList<JsonLikeError>();
				for (int i = 0; i < data.size(); i++) {
					var datum = data.get(i);
					final var index = i;
					var result = JsonCodec.this.encode(datum, ops)
					                           .mapError(it -> new AtIndex(index, it));
					erros.addAll(result.errors());
					result.valueOrPartial().ifPresent(list::add);
				}
				return Result.<Format, JsonLikeError>ok(list.complete()).appendErrors(erros);
			}

			@Override
			public Result<List<T>, JsonLikeError> decode(Format format, JsonLikeOperations<Format> ops) {
				var view = Result.<ListView<Format>, JsonLikeError>cast(ops.getList(format));
				return view.flatMap(elements -> {
					var acc = new ArrayList<T>(elements.length());
					var errors = new ArrayList<JsonLikeError>();
					for (int i = 0; i < elements.length(); i++) {
						final var index = i;
						var result = JsonCodec.this.decode(elements.getUnsafe(i), ops)
						                           .mapError(it -> new AtIndex(index, it));
						errors.addAll(result.errors());
						result.valueOrPartial().ifPresent(acc::add);
					}
					return Result.<List<T>, JsonLikeError>ok(acc).appendErrors(errors);
				});
			}
		};
	}

	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)));
			}

		};
	}
}