Skip to content

Commit

Permalink
Improve support for is standard with constructors and factories
Browse files Browse the repository at this point in the history
If exact name is used, map it as-is, otherwise use nice name.
This might not be ideal as it will not work out of the box in languages which use is name in the ctor instead of expected name, but it seems more consistent.
  • Loading branch information
zapov committed Jan 4, 2023
1 parent fabeaa2 commit be9f8f1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
Expand Up @@ -1978,7 +1978,8 @@ private Map<String, AccessElements> getBeanProperties(
method,
annotation);
} else if (!getters.containsKey(property)) {
getters.put(property, method);
String nameToUse = arguments.containsKey(name) ? name : property;
getters.put(nameToUse, method);
}
} else if (name.startsWith("get")
&& method.getParameters().size() == 0
Expand Down
Expand Up @@ -11,9 +11,12 @@ public class ImmutableClassWithGetter {
public final String getE() { return e; }
private final List<Integer> list;
public final List<Integer> getList() { return list; }
private final boolean isLocked;
public final boolean isLocked() { return isLocked; }

public ImmutableClassWithGetter(String e, List<Integer> list) {
public ImmutableClassWithGetter(String e, List<Integer> list, boolean isLocked) {
this.e = e;
this.list = list;
this.isLocked = isLocked;
}
}
Expand Up @@ -3,6 +3,7 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

Expand Down Expand Up @@ -43,4 +44,52 @@ public void isBooleanGetterUsedForSerialization() throws IOException {
Assert.assertEquals("{\"immutablePrimitive\":true,\"mutablePrimitive\":true}", os.toString("UTF-8"));
}

@CompiledJson
public static final class WithIs {
private final boolean isLocked;
private final String token;
private final boolean isJustLocked;
private final boolean isAnnotation;

public WithIs(boolean isLocked, String token, boolean justLocked, boolean isAnnotation) {
this.isLocked = isLocked;
this.token = token;
this.isJustLocked = justLocked;
this.isAnnotation = isAnnotation;
}

public final boolean isLocked() {
return isLocked;
}

public final String getToken() {
return token;
}

public final boolean isJustLocked() {
return isJustLocked;
}

@JsonAttribute(name = "annotation")
public final boolean isAnnotation() {
return isAnnotation;
}
}

@Test
public void isAndConstructor() throws IOException {
WithIs wi = new WithIs(true, "abc", true, true);

DslJson<Object> dslJson = new DslJson<>();
ByteArrayOutputStream os = new ByteArrayOutputStream();
dslJson.serialize(wi, os);
Assert.assertEquals("{\"isLocked\":true,\"token\":\"abc\",\"justLocked\":true,\"annotation\":true}", os.toString("UTF-8"));

ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
WithIs result = dslJson.deserialize(WithIs.class, is);
Assert.assertTrue(result.isLocked());
Assert.assertEquals("abc", result.getToken());
Assert.assertTrue(result.isJustLocked());
Assert.assertTrue(result.isAnnotation());
}
}
8 changes: 8 additions & 0 deletions tests-kotlin/src/main/kotlin/com/dslplatform/json/WithIs.kt
@@ -0,0 +1,8 @@
package com.dslplatform.json

@CompiledJson
data class WithIs(
val isLocked: Boolean,
val id: String? = null,
val token: String? = null,
)
Expand Up @@ -67,4 +67,20 @@ public void classCanBeEmpty() throws IOException {
EmptyClass deser = (EmptyClass) dslJson.deserialize(EmptyClass.class, input);
Assert.assertNotNull(deser);
}

@Test
public void isNamingWithoutDefault() throws IOException {
final DslJson dslJson = new DslJson(Settings.basicSetup());

WithIs wi = new WithIs(true, null, null);
ByteArrayOutputStream output = new ByteArrayOutputStream();
dslJson.serialize(wi, output);

Assert.assertEquals("{\"isLocked\":true,\"id\":null,\"token\":null}", output.toString("UTF-8"));

ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
WithIs deser = (WithIs) dslJson.deserialize(WithIs.class, input);
Assert.assertNotNull(deser);
Assert.assertTrue(deser.isLocked());
}
}

0 comments on commit be9f8f1

Please sign in to comment.