Skip to content

Commit

Permalink
Fixed booleans in nested objects, updated libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Nov 21, 2024
1 parent 4d727e1 commit e6f722d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ repositories {
}

dependencies {
compileOnly('dev.latvian.apps:tiny-java-server:1.0.0-build.16')
compileOnly('dev.latvian.apps:tiny-java-server:1.0.0-build.25')
compileOnly('org.jetbrains:annotations:24.0.1')
testImplementation('dev.latvian.apps:ansi:1.0.0-build.1')
testImplementation('dev.latvian.apps:ansi:1.0.0-build.9')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.10.0')
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.10.0')
testImplementation('junit:junit:4.13.2')
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/latvian/apps/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public <T> T adapt(Object value, Type genericType) {
return (T) Character.valueOf(String.valueOf(value).charAt(0));
} else if (t == Number.class) {
return (T) value;
} else if (t == Boolean.class || t == Boolean.TYPE) {
return (T) value;
} else if (t == Byte.class || t == Byte.TYPE) {
return (T) Byte.valueOf(((Number) value).byteValue());
} else if (t == Short.class || t == Short.TYPE) {
Expand Down
25 changes: 14 additions & 11 deletions src/test/java/dev/latvian/apps/json/test/JSONTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package dev.latvian.apps.json.test;

import dev.latvian.apps.ansi.ObjectANSI;
import dev.latvian.apps.ansi.JavaANSI;
import dev.latvian.apps.ansi.log.Log;
import dev.latvian.apps.json.JSON;
import dev.latvian.apps.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

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

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

@Test
public void readRemoteObject() {
// {"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}]}
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}]}";
Log.OUT.info(content);
Log.info(content);
var json = JSON.DEFAULT.read(content).readObject();
json.removeDeep((key, index, value) -> key.equals("fraction") && ((Number) value).doubleValue() < 0.5D, true);

Log.OUT.info(ObjectANSI.ofObject(json));
Log.OUT.info(JSON.DEFAULT.writePretty(json));
Log.info(JavaANSI.of(json));
Log.info(JSON.DEFAULT.writePretty(json));

Log.OUT.info(json.asInt("total_launches"));
Log.OUT.info(json.asDouble("hourly"));
Log.info(json.asInt("total_launches"));
Log.info(json.asDouble("hourly"));
}

@Test
Expand All @@ -61,14 +64,14 @@ public void record() {
)));

var str = JSON.DEFAULT.write(r);
Log.OUT.info(r);
Log.OUT.info(str);
Log.info(r);
Log.info(str);

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

Log.OUT.info(r2);
Log.OUT.info(str2);
Log.info(r2);
Log.info(str2);

Assertions.assertEquals(str, str2);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/dev/latvian/apps/json/test/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class TestConfig {
public static class WebConfig {
public int port = 12345;
public String title = "Test";
public boolean required = false;
public long[] ids = new long[0];
}

public static class DiscordConfig {
Expand Down

0 comments on commit e6f722d

Please sign in to comment.