-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckApiResult.java
More file actions
46 lines (40 loc) · 1.54 KB
/
Copy pathCheckApiResult.java
File metadata and controls
46 lines (40 loc) · 1.54 KB
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
package com.checkapi;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public record CheckApiResult(
boolean verified,
double confidence,
double detectedAmount,
String detectedCardLast4,
String reason,
int durationMs,
int checksLeft
) {
static CheckApiResult fromJson(String json) {
return new CheckApiResult(
boolVal(json, "verified"),
doubleVal(json, "confidence"),
doubleVal(json, "detectedAmount"),
strVal(json, "detectedCardLast4"),
strVal(json, "reason"),
intVal(json, "durationMs"),
intVal(json, "checksLeft")
);
}
private static boolean boolVal(String json, String key) {
Matcher m = Pattern.compile("\"" + key + "\"\\s*:\\s*(true|false)").matcher(json);
return m.find() && "true".equals(m.group(1));
}
private static double doubleVal(String json, String key) {
Matcher m = Pattern.compile("\"" + key + "\"\\s*:\\s*([\\d.]+)").matcher(json);
return m.find() ? Double.parseDouble(m.group(1)) : 0;
}
private static int intVal(String json, String key) {
Matcher m = Pattern.compile("\"" + key + "\"\\s*:\\s*(\\d+)").matcher(json);
return m.find() ? Integer.parseInt(m.group(1)) : 0;
}
private static String strVal(String json, String key) {
Matcher m = Pattern.compile("\"" + key + "\"\\s*:\\s*\"([^\"]*)\"").matcher(json);
return m.find() ? m.group(1) : "";
}
}