Skip to content

Commit e6f722d

Browse files
committed
Fixed booleans in nested objects, updated libraries
1 parent 4d727e1 commit e6f722d

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ repositories {
2828
}
2929

3030
dependencies {
31-
compileOnly('dev.latvian.apps:tiny-java-server:1.0.0-build.16')
31+
compileOnly('dev.latvian.apps:tiny-java-server:1.0.0-build.25')
3232
compileOnly('org.jetbrains:annotations:24.0.1')
33-
testImplementation('dev.latvian.apps:ansi:1.0.0-build.1')
33+
testImplementation('dev.latvian.apps:ansi:1.0.0-build.9')
3434
testImplementation('org.junit.jupiter:junit-jupiter-api:5.10.0')
3535
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.10.0')
3636
testImplementation('junit:junit:4.13.2')

src/main/java/dev/latvian/apps/json/JSON.java

+2
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ public <T> T adapt(Object value, Type genericType) {
319319
return (T) Character.valueOf(String.valueOf(value).charAt(0));
320320
} else if (t == Number.class) {
321321
return (T) value;
322+
} else if (t == Boolean.class || t == Boolean.TYPE) {
323+
return (T) value;
322324
} else if (t == Byte.class || t == Byte.TYPE) {
323325
return (T) Byte.valueOf(((Number) value).byteValue());
324326
} else if (t == Short.class || t == Short.TYPE) {

src/test/java/dev/latvian/apps/json/test/JSONTests.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package dev.latvian.apps.json.test;
22

3-
import dev.latvian.apps.ansi.ObjectANSI;
3+
import dev.latvian.apps.ansi.JavaANSI;
44
import dev.latvian.apps.ansi.log.Log;
55
import dev.latvian.apps.json.JSON;
66
import dev.latvian.apps.json.JSONObject;
77
import org.junit.jupiter.api.Assertions;
88
import org.junit.jupiter.api.Test;
99

10+
import java.util.Arrays;
1011
import java.util.List;
1112
import java.util.Optional;
1213

@@ -30,26 +31,28 @@ public void deserialize() {
3031
@Test
3132
public void adapt() {
3233
var config = JSON.DEFAULT.read("""
33-
{"database":"https://lat:[email protected]:1234/","discord":[{"clientId":"7"}]}""").adapt(TestConfig.class);
34+
{"web":{"required":true,"ids":[1, 2, 3]},"database":"https://lat:[email protected]:1234/","discord":[{"clientId":"7"}]}""").adapt(TestConfig.class);
3435

3536
Assertions.assertEquals(config.database.toString(), "https://lat:[email protected]:1234/");
3637
Assertions.assertEquals(config.discord[0].clientId, "7");
3738
Assertions.assertEquals(config.discord[0].clientSecret, "shh");
39+
Assertions.assertTrue(config.web.required);
40+
Assertions.assertEquals(Arrays.toString(config.web.ids), Arrays.toString(new long[]{1L, 2L, 3L}));
3841
}
3942

4043
@Test
4144
public void readRemoteObject() {
4245
// {"hours":672,"total_launches":7864423,"hourly":11700.028795489174,"ml":[{"ml":1,"fraction":0.9412095458242773,"launches":7402070},{"ml":2,"fraction":0.05879045417572275,"launches":462353}],"mc":[{"mc":2001,"fraction":0.6443314404629558,"launches":5067295},{"mc":1902,"fraction":0.3556624561013567,"launches":2797080},{"mc":2004,"fraction":6.103435687525964E-6,"launches":48}],"mlmc":[{"ml":1,"mc":2001,"fraction":0.6154869848684386,"launches":4840450},{"ml":1,"mc":1902,"fraction":0.3257164575201512,"launches":2561572},{"ml":2,"mc":1902,"fraction":0.029945998581205512,"launches":235508},{"ml":2,"mc":2001,"fraction":0.028844455594517232,"launches":226845},{"ml":1,"mc":2004,"fraction":6.103435687525964E-6,"launches":48}],"launches":[{"version":"2001.6.4-build.114","fraction":0.26559062247796184,"launches":2088717},{"version":"2001.6.4-build.120","fraction":0.21153312836809515,"launches":1663586},{"version":"1902.6.2-build.45","fraction":0.1828411315108559,"launches":1437940},{"version":"2001.6.4-build.127","fraction":0.0810527358459737,"launches":637433},{"version":"1902.6.2-build.3","fraction":0.038601560470488426,"launches":303579},{"version":"2001.6.4-build.95","fraction":0.03645264248883866,"launches":286679},{"version":"1902.6.2-build.15","fraction":0.02442060911525232,"launches":192054},{"version":"1902.6.2-build.50","fraction":0.018523800156731142,"launches":145679},{"version":"2001.6.3-build.83","fraction":0.018382658206457105,"launches":144569},{"version":"1902.6.2-build.27","fraction":0.01564005394928528,"launches":123000},{"version":"","fraction":1.3987040117246999E-6,"launches":11}]}
4346
var content = "{\"hours\":672,\"total_launches\":7864423,\"hourly\":11700.028795489174,\"ml\":[{\"ml\":1,\"fraction\":0.9412095458242773,\"launches\":7402070},{\"ml\":2,\"fraction\":0.05879045417572275,\"launches\":462353}],\"mc\":[{\"mc\":2001,\"fraction\":0.6443314404629558,\"launches\":5067295},{\"mc\":1902,\"fraction\":0.3556624561013567,\"launches\":2797080},{\"mc\":2004,\"fraction\":6.103435687525964E-6,\"launches\":48}],\"mlmc\":[{\"ml\":1,\"mc\":2001,\"fraction\":0.6154869848684386,\"launches\":4840450},{\"ml\":1,\"mc\":1902,\"fraction\":0.3257164575201512,\"launches\":2561572},{\"ml\":2,\"mc\":1902,\"fraction\":0.029945998581205512,\"launches\":235508},{\"ml\":2,\"mc\":2001,\"fraction\":0.028844455594517232,\"launches\":226845},{\"ml\":1,\"mc\":2004,\"fraction\":6.103435687525964E-6,\"launches\":48}],\"launches\":[{\"version\":\"2001.6.4-build.114\",\"fraction\":0.26559062247796184,\"launches\":2088717},{\"version\":\"2001.6.4-build.120\",\"fraction\":0.21153312836809515,\"launches\":1663586},{\"version\":\"1902.6.2-build.45\",\"fraction\":0.1828411315108559,\"launches\":1437940},{\"version\":\"2001.6.4-build.127\",\"fraction\":0.0810527358459737,\"launches\":637433},{\"version\":\"1902.6.2-build.3\",\"fraction\":0.038601560470488426,\"launches\":303579},{\"version\":\"2001.6.4-build.95\",\"fraction\":0.03645264248883866,\"launches\":286679},{\"version\":\"1902.6.2-build.15\",\"fraction\":0.02442060911525232,\"launches\":192054},{\"version\":\"1902.6.2-build.50\",\"fraction\":0.018523800156731142,\"launches\":145679},{\"version\":\"2001.6.3-build.83\",\"fraction\":0.018382658206457105,\"launches\":144569},{\"version\":\"1902.6.2-build.27\",\"fraction\":0.01564005394928528,\"launches\":123000},{\"version\":\"\",\"fraction\":1.3987040117246999E-6,\"launches\":11}]}";
44-
Log.OUT.info(content);
47+
Log.info(content);
4548
var json = JSON.DEFAULT.read(content).readObject();
4649
json.removeDeep((key, index, value) -> key.equals("fraction") && ((Number) value).doubleValue() < 0.5D, true);
4750

48-
Log.OUT.info(ObjectANSI.ofObject(json));
49-
Log.OUT.info(JSON.DEFAULT.writePretty(json));
51+
Log.info(JavaANSI.of(json));
52+
Log.info(JSON.DEFAULT.writePretty(json));
5053

51-
Log.OUT.info(json.asInt("total_launches"));
52-
Log.OUT.info(json.asDouble("hourly"));
54+
Log.info(json.asInt("total_launches"));
55+
Log.info(json.asDouble("hourly"));
5356
}
5457

5558
@Test
@@ -61,14 +64,14 @@ public void record() {
6164
)));
6265

6366
var str = JSON.DEFAULT.write(r);
64-
Log.OUT.info(r);
65-
Log.OUT.info(str);
67+
Log.info(r);
68+
Log.info(str);
6669

6770
var r2 = JSON.DEFAULT.read(str).adapt(RecordTest.class);
6871
var str2 = JSON.DEFAULT.write(r2);
6972

70-
Log.OUT.info(r2);
71-
Log.OUT.info(str2);
73+
Log.info(r2);
74+
Log.info(str2);
7275

7376
Assertions.assertEquals(str, str2);
7477
}

src/test/java/dev/latvian/apps/json/test/TestConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class TestConfig {
1010
public static class WebConfig {
1111
public int port = 12345;
1212
public String title = "Test";
13+
public boolean required = false;
14+
public long[] ids = new long[0];
1315
}
1416

1517
public static class DiscordConfig {

0 commit comments

Comments
 (0)