Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat - handle multiple record constructors by using canonical constru…
…ctor by default (#12)
  • Loading branch information
mxyns committed Mar 4, 2022
commit 149b796d93f7229b30a29ceeb620d893105aa84b
11 changes: 5 additions & 6 deletions src/main/java/com/jsoniter/spi/ClassDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,13 @@ private static ConstructorDescriptor getCtor(Class clazz) {
return cctor;
}

private static ConstructorDescriptor getRecordCtor(Class clazz) {
private static ConstructorDescriptor getRecordCtor(Class<?> clazz) {
ConstructorDescriptor cctor = new ConstructorDescriptor();
try {
Constructor<?> ctor = clazz.getDeclaredConstructors()[0];
cctor.ctor = ctor;
for (Type parameter : ctor.getParameterTypes()) {
ClassInfo info = new ClassInfo(parameter);
}
Class<?>[] canonicalParameterTypes = Arrays.stream(clazz.getRecordComponents()).map(RecordComponent::getType).toArray(Class<?>[]::new);
System.out.println("Canonical Parameter Types : ");
System.out.println(Arrays.toString(canonicalParameterTypes));
cctor.ctor = clazz.getDeclaredConstructor(canonicalParameterTypes);
} catch (Exception e) {
cctor.ctor = null;
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/com/jsoniter/TestRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.jsoniter.annotation.JsonProperty;
import com.jsoniter.any.Any;
import com.jsoniter.spi.ClassInfo;
import com.jsoniter.spi.EmptyExtension;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.JsoniterSpi;
import junit.framework.TestCase;

import java.io.IOException;
Expand Down Expand Up @@ -103,7 +101,6 @@ public void test_record_error() throws IOException {
}
}


public void test_record_withOnlyFieldDecoder() throws IOException {

assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord1.class)).getClass());
Expand Down Expand Up @@ -200,7 +197,7 @@ public TestRecord6(int valInt) {

assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord6.class)).getClass());

JsonIterator iter = JsonIterator.parse("{ 'valInt' : 1 }".replace('\'', '"'));
JsonIterator iter = JsonIterator.parse("{ 'val' : 1 }".replace('\'', '"'));
TestRecord6 record = iter.read(TestRecord6.class);

assertNotNull(record);
Expand Down